在Python中,文件之间可以互相调用的方法有以下几种:
# file1.py def func1(): print("Hello from file1") # file2.py import file1 file1.func1() # 调用file1.py中的func1函数
# file1.py def func1(): print("Hello from file1") # file2.py from file1 import func1 func1() # 调用file1.py中的func1函数
# file1.py def func1(): print("Hello from file1") # file2.py from . import file1 file1.func1() # 调用file1.py中的func1函数
# file1.py def func1(): print("Hello from file1") # file2.py import sys sys.path.append('/path/to/directory') # 添加文件所在目录到sys.path import file1 file1.func1() # 调用file1.py中的func1函数
这些方法可以在不同的文件中进行相互调用,从而实现代码的模块化和重用。