在同一个文件夹下

调用函数:

A.py文件:

[python] view plain copy
  1. def add(x,y):  
  2.     print('和为:%d'%(x+y))  


B.py文件:

[python] view plain copy
  1. import A  
  2. A.add(1,2)  


[python] view plain copy
  1. from A import add  
  2. add(1,2)  

 

调用类:

A.py文件:

[python] view plain copy
  1. class A:  
  2.     def __init__(self,xx,yy):  
  3.         self.x=xx  
  4.         self.y=yy  
  5.     def add(self):  
  6.         print("x和y的和为:%d"%(self.x+self.y))  

 

B.py文件:

[python] view plain copy
  1. from A import A  
  2. a=A(2,3)  
  3. a.add()  


[python] view plain copy
  1. import A  
  2. a=A.A(2,3)  
  3. a.add()  



在不同文件夹下

A.py文件的文件路径:E:\PythonProject\winycg
 
B.py文件:
[python] view plain copy
  1. import sys  
  2. sys.path.append(r'E:\PythonProject\winycg')  
  3. '''''python import模块时, 是在sys.path里按顺序查找的。 
  4. sys.path是一个列表,里面以字符串的形式存储了许多路径。 
  5. 使用A.py文件中的函数需要先将他的文件路径放到sys.path中'''  
  6. import A  
  7.   
  8. a=A.A(2,3)  
  9. a.add()