2016-08-05 83 views
6

我发现导入Python模块复杂,所以我正在做实验来清除它。这里是我的文件结构:ImportError:没有模块命名包

内容 __init__.py
PythonTest/ 
    package/ 
    __init__.py 
    test.py 

内容的 test.py
package = 'Variable package in __init__.py' 
from package import test 

from package import package 
print package 

当我留出package(在PythonTest),并执行python package/test.py,我得到:

Traceback (most recent call last): 
    File "package/test.py", line 1, in <module> 
    from package import package 
ImportError: No module named package 

期望的输出是Variable package in __init__.py。我究竟做错了什么?


不过,我可以得到预期的输出在交互模式:

sunqingyaos-MacBook-Air:PythonTest sunqingyao$ python 
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import package 
Package in __init__.py 
+0

它看起来并不像模块搜索路径设置为'package'包是可找到的。 – user2357112

+0

@ user2357112所以我应该设置'PYTHONPATH'或修改'sys.path'?但为什么在交互模式下一切都好? –

+0

我假设你从与你的文件相同的目录开始交互模式,以便路径自动成为搜索路径的一部分。尝试从完全不同的目录中启动python – WombatPM

回答

5

首先让我们看一下Python的搜索包和模块。 sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH , plus an installation-dependent default.

这是搜索路径。因此,如果你的模块/软件包位于sys.path之一,Python解释器能够找到并导入它。该文档说更多:

As initialized upon program startup, the first item of this list, path[0] , is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

我修改test.py作为一个例子。

import sys; import pprint 
pprint.pprint(sys.path) 

from package import package 
print package 

有两种情况:

$ python package/test.py 
['/Users/laike9m/Dev/Python/TestPython/package', 
'/usr/local/lib/python2.7/site-packages/doc2dash-2.1.0.dev0-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/zope.interface-4.1.3-py2.7-macosx-10.10-x86_64.egg', 
'/usr/local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/colorama-0.3.3-py2.7.egg', 

正如你看到的,path[0]/Users/laike9m/Dev/Python/TestPython/package,它包含用于调用Python解释器脚本test.py的目录。

$ python           
Python 2.7.12 (default, Jun 29 2016, 14:05:02) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import package 
['', 
'/usr/local/lib/python2.7/site-packages/doc2dash-2.1.0.dev0-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/zope.interface-4.1.3-py2.7-macosx-10.10-x86_64.egg', 
'/usr/local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/colorama-0.3.3-py2.7.egg', 
... 

下面是第二个情况下,当交互方式调用,“path[0]是空字符串,其引导Python来首先搜索在当前目录中的模块。”目前的目录是什么? 。/Users/laike9m/Dev/Python/TestPython/(看这是我的机器上的路径,它在你的情况是相当于路径PythonTest

现在你知道答案:

  1. 为什么python package/test.pyImportError: No module named package

    因为解释器不“看”包装。为了让翻译知道包裹packagePythonTest必须在sys.path,但它不是。

  2. 为什么它在互动模式下工作?

    因为现在PythonTestsys.path,所以解释器能够找到包package

相关问题