python - 从另一个 Python 文件获取 Python 文件

标签 python

我的问题可能重复,但我找不到答案。

如何从另一个 python 文件获取 python 文件。像这样的东西:

def fun_1 (arg):
   ...
   ...
   fun_2 (arg):
     ...
     ...

#### MAIN ####

Source the file that contains the function `fun_2`

fun_1('hello')

最佳答案

规范的方法是将第一个文件变成 module并将其导入到第二个文件中:

$ cat file1.py
def fun():
   print('in fun1')

$ cat file2.py
import file1

file1.fun()

关于python - 从另一个 Python 文件获取 Python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655688/

相关文章:

python - 将 Python 更新到 3.8,模块不起作用

python - 在Python中确定Wave文件中允许的最大样本值

python - 如何在Django中查看为测试模型创建的SQL?

Python - 类型(名称、基础、字典)

python - 异常评估顺序

Python strip() 多个字符?

Python + CGI脚本无法访问环境变量

Python - 取每个第 n 个元素(就地)

python - BERT分类器中每个标签的预测概率

python - 如何在不转换数据的情况下压缩/合并多行