不能从同一项目的不同模块导入函数

2024-09-26 18:17:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个脚本:
脚本1:

from test import script2

if __name__ == '__main__':
    foo()

脚本2:

^{pr2}$

在结构中:

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.

enter image description here

我读过很多类似的案例,但他们都帮不了我:
Call a function from another file in Python
How to import a function from parent folder in python?
Import Script from a Parent Directory


Tags: andtoinfrompytestimport脚本
1条回答
网友
1楼 · 发布于 2024-09-26 18:17:31

要使Python包正常工作,您需要文件夹中的__init__.py个文件。这些可以是空的。在

然后,从子文件夹导入,因此需要import script2 from A.B

此时,您可以使用script2.foo()

另外值得一提的是,Python3应该是任何新项目的目标。在

相关问题 更多 >

    热门问题