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()