为什么会出现ModuleNotFoundError?

时间:2017-08-03 06:41:32

标签: python python-3.x

以下是Github中有问题代码的链接:https://github.com/ddmee/moduleerrorpython

我正在使用Python 3.6。我有一组看起来像这样的目录。

root/
....lib/
........__init__.py
........shared/
..............___init__.py
..............file1.py
..............file2.py

从根目录开始,当我尝试使用解释器(python lib / shared / file1.py)运行file1.py时,出现此错误:

Traceback (most recent call last):
  File "lib/shared/file1.py", line 1, in <module>
    import lib.shared.file2 as file2
ModuleNotFoundError: No module named 'lib.shared'

但是,如果我启动解释器并只导入file1,则不存在此类错误。 E.g。

> python                                                                                                                                         
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32                                                   
Type "help", "copyright", "credits" or "license" for more information.                                                                           
>>> import lib.shared.file1                                                                                                                    
>>> import lib.shared.file1 as file1                                                                                                          
>>> dir(file1)                                                                                                                                  
['Hello', 'Server', 'TrioII', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

0 个答案:

没有答案