2016-08-05 86 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

相關問題