无法从同一项目中的不同模块导入功能
Can't import functions from different module in the same project
我有两个脚本:
脚本 1:
from test import script2
if __name__ == '__main__':
foo()
脚本 2:
def foo():
print 'hello'
在结构中:
test:
│ A
│-----B:
│ script2.py
│script1.py
我正在尝试从 script1.py 中的 script2.py 调用函数 foo()
。
我收到错误:
This inspection detect names that should resolve but don't. Due to
dynamic dispatch and duck typing, this is possible in a limited but
useful number of cases. Top-level and class-level itesm are supported
better than instance items.
我阅读了很多类似的案例,但它们对我没有帮助:
Call a function from another file in Python
Import Script from a Parent Directory
要使 Python 个包正常工作,文件夹中需要 __init__.py
个文件。这些可以是空的。
那么,您是从子文件夹中导入的,所以您需要import script2 from A.B
届时,您可以使用 script2.foo()
另外值得一提的是 Python3 应该针对任何新项目。
我有两个脚本:
脚本 1:
from test import script2
if __name__ == '__main__':
foo()
脚本 2:
def foo():
print 'hello'
在结构中:
test:
│ A
│-----B:
│ script2.py
│script1.py
我正在尝试从 script1.py 中的 script2.py 调用函数 foo()
。
我收到错误:
This inspection detect names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level itesm are supported better than instance items.
我阅读了很多类似的案例,但它们对我没有帮助:
Call a function from another file in Python
Import Script from a Parent Directory
要使 Python 个包正常工作,文件夹中需要 __init__.py
个文件。这些可以是空的。
那么,您是从子文件夹中导入的,所以您需要import script2 from A.B
届时,您可以使用 script2.foo()
另外值得一提的是 Python3 应该针对任何新项目。