2017-03-13 3 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 스크립트를 직접 실행 그것은 나에게 오류를 제공하기 때문에 :

내가 pip install -U .

로를 설치할 때

» 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 . 
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

기본적으로, 당신은 파이썬의 상대적 import gotcha에 갇혀 있습니다. Python 가져 오기 시스템은 상대적인 가져 오기에서 약간 복잡합니다. 따라서, 상대적인 수입은주의해서 사용해야한다. (이를 위해, 표준 모듈/패키지와 충돌하지 않는 그러한 이름을 모듈에 제공한다.) 파이썬 패키지에 파일을 작성할 때 항상이 문제에 직면하게됩니다. 당신은 두 가지 시나리오를해야합니다 : -

1) 실행 모듈과 같은 파일 일반 시나리오에서는 스크립트

# cd package 
python module.py 

로 파일을 실행 해

python -m package.module 

2), 일이 잘 될 것입니다,하지만 때를 스크립트의 경우 상대 수입을 __name__ 모듈 변수에 대해 해결하므로 문제가 발생합니다. 스크립트의 경우에는 '__main__'이 될 것이므로 상대적인 수입을 해결할 때 문제가됩니다.

은 다음 문서를 참조하십시오 : - http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html