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