2017-11-03 73 views
0

我有两个脚本:
SCRIPT1:不能导入来自不同模块的功能在同一个项目

from test import script2 

if __name__ == '__main__': 
    foo() 

SCRIPT2:

def foo(): 
    print 'hello' 

在结构:

test: 
│ A 
│-----B: 
│  script2.py 
│script1.py 

我正在试图在脚本中调用script2.py中的函数foo() t1.py.

我收到的错误:

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

+0

你需要用'__init __。py'文件将它打包。 – Artyer

+1

您需要从'test.A.B'导入。你还需要每个文件夹中的__init __。py'文件 –

+0

他们上面说的**或**,你将B添加到你的'PYTHONPATH'环境变量中。 – GPhilo

回答

0

对于Python包需要工作,你需要__init__.py文件夹中。这些可以是空的。

然后,您从一个子文件夹导入,所以你需要import script2 from A.B

在这一点上,你可以使用script2.foo()

另外值得一提的是,Python3应针对任何新项目。

相关问题