gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:32 25 4
gpt4 key购买 nike

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

如何从另一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com