在Python中,调用另一个文件中的函数通常有以下几种方法:
使用`import`语句
```python
from other_file import func1, func2
func1()
func2()
使用`from...import`语句
```python
from other_file import *
func1()
func2()
使用`import`语句后跟`*`
```python
from other_file import *
result1 = func1(3, 5)
result2 = func2(7, 2)
print(result1)
print(result2)
使用`sys.path.append()`
```python
import sys
sys.path.append('/path/to/application/app/folder')
from application.app.folder.file import func_name
在文件目录下新建`__init__.py`文件
```python
在文件目录下新建__init__.py文件
from application.app.folder.file import func_name
确保`other_file`或`application.app.folder.file`中的函数或类是可以被访问的,并且文件路径是正确的。如果文件不在当前工作目录下,需要将文件所在路径添加到`sys.path`中,或者将包含函数的目录标记为Python包(通过在该目录下创建`__init__.py`文件)。
请根据你的具体需求选择合适的方法。