2017-03-13 46 views
4

我的文件夾/文件structrue是:導入另一個文件錯誤

testpkg/test/__init__.py; 
testpkg/test/test1.py 
testpkg/test/test2.py 
testpkg/setup.py 

testpkg/test/__init__.py文件是空的。
testpkg/test/test1.py文件內容:

class Test1: 
    def __init__(self, name): 
     self.name = name 

    def what_is_your_name(self): 
     print(f'My name is {self.name}') 

testpkg/test/test2.py文件內容:

from .test1 import Test1 


def main(): 
    t = Test1('me') 
    t.what_is_your_name() 

if __name__ == '__main__': 
    main() 

/testpkg/setup.py內容:

from setuptools import setup 

setup(name='test', 
     version='0.1', 
     packages=['test'], 
     entry_points={ 
      'console_scripts': [ 
       'test_exec = test.test2:main' 
      ] 
     } 
    ) 

我無法調試/運行test2.py腳本直接,因爲它給了我錯誤:

» python test/test2.py 
Traceback (most recent call last): 
    File "test/test2.py", line 1, in <module> 
    from .test1 import Test1 
ModuleNotFoundError: No module named '__main__.test1'; '__main__' is not a package 

但是當我pip install -U .

安裝它,它的工作原理:

» pip install -U . 
Processing /home/kossak/Kossak/files_common/PythonProjects/testpkg 
Installing collected packages: test 
    Found existing installation: test 0.1 
    Uninstalling test-0.1: 
     Successfully uninstalled test-0.1 
    Running setup.py install for test ... done 
Successfully installed test-0.1 

» test_exec 
My name is me 

的問題是:如何寫test2.py正確因此它可以以兩種方式 - 直接(這樣我就可以對它進行調試在PyCharm中運行,或者只需運行python test2.py)並安裝test包後?我試圖改變這一行:

from .test1 import Test1 

from test1 import Test1 

(刪除點)

,我可以運行命令行test2.py,但隨後在安裝後,我的劇本 「test_exec」 給出我的錯誤:

Traceback (most recent call last): 
    File "/home/kossak/anaconda3/bin/test_exec", line 11, in <module> 
    load_entry_point('test==0.1', 'console_scripts', 'test_exec')() 
    File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 565, in load_entry_point 
    File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2598, in load_entry_point 
    File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2258, in load 
    File "/home/kossak/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2264, in resolve 
    File "/home/kossak/anaconda3/lib/python3.6/site-packages/test/test2.py", line 1, in <module> 
    from test1 import Test1 
ModuleNotFoundError: No module named 'test1' 
+1

由於相對於進口應該運行'蟒蛇-m test.test2'作爲模塊 –

回答

2

嘗試匯入這樣的:from test.test1 import Test1

0

基本上,你陷入了python的相對導入陷阱。涉及到相對導入時,Python導入系統有點複雜。因此,必須謹慎使用相對導入(爲此,請嘗試將這些名稱提供給您的模塊,這不會與標準模塊/軟件包相沖突)。當你在python包中編寫任何文件時,你總是會遇到這個問題。您將有兩種方案: -

1)運行文件模塊

python -m package.module 

2)捉迷藏文件作爲腳本

# cd package 
python module.py 

在正常情況下,事情會好起來的,但是當你執行相關的導入操作,然後將文件作爲腳本運行,將會導致問題,因爲相對導入將在__name__模塊變量中得到解決,如果是腳本,它將爲'__main__',因此在解析相對導入時將面臨問題。

請參閱下面的文章: - http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html