in the same folder
calls the function:
A.py file:
def add(x,y):
print('和为:%d'%(x+y))
B.py file:
import A
A.add(1,2)
or
from A import add
add(1,2)
call class:
Amy polumbo y file: p>
class A:
def __init__(self,xx,yy):
self.x=xx
self.y=yy
def add(self):
print("x和y的和为:%d"%(self.x+self.y))
B.py file:
from A import A
a=A(2,3)
a.add()
or
import A
a=A.A(2,3)
a.add()
in different folders
The file path for the a.python file: E:\PythonProject\winycg
P. y file:
import sys
sys.path.append(r'E:\PythonProject\winycg')
'''python import模块时, 是在sys.path里按顺序查找的。
sys.path是一个列表,里面以字符串的形式存储了许多路径。
使用A.py文件中的函数需要先将他的文件路径放到sys.path中'''
import A
a=A.A(2,3)
a.add()
p>
div>