1.两个py文件放到同一目录下
[root@iZ2ze1vz1brexxqdo6gmftZ pythonproject]# pwd
/wei_tmp/pythonproject
2.
test1.py
import os
import requests
class Test():
def __init__(self):
print("This is a test")
def myprint(self):
print("自定义的打印")
def mynew_print():
print("这个新的打印,不是类的方法")
if __name__=="__main__":
my=Test()
my.myprint()
par={}
url="http://www.baidu.com"
res=requests.get(url=url,params=par)
print("res: ",res)
#print(res.text)
mynew_print()
test2.py引用test1.py中的方法和类
test2.py
import sys
sys.path.append('/wei_tmp/pythonproject')
from test1 import Test
from test1 import *
if __name__=="__main__":
m=Test()
m.myprint()
mynew_print()
本文通过两个Python文件的实例,演示了如何在同一目录下进行模块的导入和使用,包括自定义类和函数的调用,以及requests库的网络请求操作。
827

被折叠的 条评论
为什么被折叠?



