导入程序封装调用函数

问题描述 投票:0回答:1

包能否在导入它的文件中调用函数?

例如:

file1.py的代码

import file2

def onstart():
    print('Hello World!')

file2.py的代码

# ... something
onstart()

然后如果我运行 file1,我希望输出是

你好世界!

我试着在 file2.py 中调用它,但是出现了错误

NameError: name 'onstart' is not defined

python python-3.x import package python-import
1个回答
0
投票

如果你把这个放在file2

from file1 import *

onstart()

运行 file1 它应该输出

你好世界!

© www.soinside.com 2019 - 2024. All rights reserved.