2017-03-13 5 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

による相対インポートにあなたは 'のpython -m test.test2'モジュールとして実行する必要があります –

答えて

2

from test.test1 import Test1

0

基本的に、あなたは、pythonの相対インポートのgotchaにトラップされています。 Pythonのインポートシステムは、相対的なインポートに関しては少し複雑です。したがって、相対的なインポートは慎重に使用する必要があります(そのために、モジュールにそのような名前をつけてください。標準的なモジュールやパッケージには衝突しません)。 Pythonパッケージにファイルを書くときには常にこの問題に直面します。あなたは、2つのシナリオがあります: -

1)ランニングモジュールとしてファイルの通常のシナリオでは、スクリプト

# cd package 
python module.py 

としてファイルを走っ

python -m package.module 

2)は、物事は罰金になりますが、ときにスクリプトの場合は'__main__'となり、相対インポートを解決する際に問題に直面するので、相対インポートは__name__モジュール変数に対して解決されるため、ファイルとしてスクリプトを実行すると、相対インポートが行われます。

は、次の資料を参照してください: - http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

関連する問題