2017-11-03 139 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應針對任何新項目。

相關問題