当我尝试通过Interpreter运行我的Python脚本时,为什么会出现“ImportError:No module named”?

时间:2017-09-06 08:28:58

标签: python python-3.x automation

我对Python比较陌生,已经在我的公司开发自动化框架。

第一次,在开发过程中,我遇到了需要同时运行多个测试的情况。直到这一点,我总是使用Eclipse,不仅要开发我的框架,还要运行测试(当然它一次只有一个)。 当我试图通过CMD屏幕“python”运行我的一个测试(我设法通过Eclipse运行)时,它甚至没有开始运行,我得到的结果是“ImportError:没有模块命名”(与我从我开发的包中做的进口。)

Eclipse如何设法与我的所有Imports相处并运行我的测试,而python解释器却不能?

为了设法通过解释器运行我的测试,我需要做些什么?

修改

以下是我尝试导入专有模块的示例

 C:\Users\alpha_2.PL\workspace\PacketLight Automation\Test_Cases>python
 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900
 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or
 "license" for more information.
 >>> from Utilities.Browser_Utils import Open_Browser Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError:
 No module named 'Utilities'
 >>> quit()

 C:\Users\alpha_2.PL\workspace\PacketLight Automation\Test_Cases>cd..

 C:\Users\alpha_2.PL\workspace\PacketLight Automation>cd Utilities

 C:\Users\alpha_2.PL\workspace\PacketLight Automation\Utilities>python
 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900
 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or
 "license" for more information.
 >>> from Utilities.Browser_Utils import Open_Browser Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError:
 No module named 'Utilities'
 >>> quit()

 C:\Users\alpha_2.PL\workspace\PacketLight Automation\Utilities>dir 
 Volume in drive C has no label.  Volume Serial Number is 1E0C-6554

  Directory of C:\Users\alpha_2.PL\workspace\PacketLight Automation\Utilities

 08/07/2017  03:05 PM    <DIR>          .
 08/07/2017  03:05 PM    <DIR>          ..
 08/10/2017  10:28 AM               455 Browser_Utils.py 
 09/05/2017  05:00 PM             7,007 General_Utils.py
 07/31/2017  01:49 PM                 0 __init__.py
 09/05/2017  06:07 PM    <DIR>          __pycache__
                3 File(s)          7,462 bytes
                3 Dir(s)  394,707,480,576 bytes free

 C:\Users\alpha_2.PL\workspace\PacketLight Automation\Utilities>python
 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900
 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or
 "license" for more information.
 >>> from Utilities.Browser_Utils import Open_Browser 
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module> ImportError:
 No module named 'Utilities'
 >>>

3 个答案:

答案 0 :(得分:2)

我可以想象的是Eclipse会从项目目录运行你的命令,这会使相对导入工作。

如果我在/ home / jpic中运行python,并且foo.py存在,那么我可以import foo。但是,如果我在/ home中运行python,那么import foo将失败。因此,请确保在调用python之前进入正确的目录。

如果我想让foo可以从任何地方导入,我将不得不将它变成一个python包,这可能不是你想要对你的项目做的事情(但我会,我的所有项目,因为它&#39;让进口到处都工作的方式很酷^。)。

如果要调试导入,使用python运行-vv,您将获得导入调试输出:

# trying /home/jpic/foo.cpython-36m-x86_64-linux-gnu.so
# trying /home/jpic/foo.abi3.so
# trying /home/jpic/foo.so
# trying /home/jpic/foo.py
# trying /home/jpic/foo.pyc
# trying /usr/lib/python3.6/foo.cpython-36m-x86_64-linux-gnu.so
# trying /usr/lib/python3.6/foo.abi3.so
# trying /usr/lib/python3.6/foo.so
# trying /usr/lib/python3.6/foo.py
# trying /usr/lib/python3.6/foo.pyc
# trying /usr/lib/python3.6/lib-dynload/foo.cpython-36m-x86_64-linux-gnu.so
# trying /usr/lib/python3.6/lib-dynload/foo.abi3.so
# trying /usr/lib/python3.6/lib-dynload/foo.so
# trying /usr/lib/python3.6/lib-dynload/foo.py
# trying /usr/lib/python3.6/lib-dynload/foo.pyc
# trying /home/jpic/.local/lib/python3.6/site-packages/foo.cpython-36m-x86_64-linux-gnu.so
# trying /home/jpic/.local/lib/python3.6/site-packages/foo.abi3.so
# trying /home/jpic/.local/lib/python3.6/site-packages/foo.so
# trying /home/jpic/.local/lib/python3.6/site-packages/foo.py
# trying /home/jpic/.local/lib/python3.6/site-packages/foo.pyc
# trying /home/jpic/src/django-cities-light/foo.cpython-36m-x86_64-linux-gnu.so
# trying /home/jpic/src/django-cities-light/foo.abi3.so
# trying /home/jpic/src/django-cities-light/foo.so
# trying /home/jpic/src/django-cities-light/foo.py
# trying /home/jpic/src/django-cities-light/foo.pyc
# trying /home/jpic/src/django-autocomplete-light/src/foo.cpython-36m-x86_64-linux-gnu.so
# trying /home/jpic/src/django-autocomplete-light/src/foo.abi3.so
# trying /home/jpic/src/django-autocomplete-light/src/foo.so
# trying /home/jpic/src/django-autocomplete-light/src/foo.py
# trying /home/jpic/src/django-autocomplete-light/src/foo.pyc
# trying /home/jpic/src/django-autocomplete-light/test_project/src/genericm2m/foo.cpython-36m-x86_64-linux-gnu.so
# trying /home/jpic/src/django-autocomplete-light/test_project/src/genericm2m/foo.abi3.so
# trying /home/jpic/src/django-autocomplete-light/test_project/src/genericm2m/foo.so
# trying /home/jpic/src/django-autocomplete-light/test_project/src/genericm2m/foo.py
# trying /home/jpic/src/django-autocomplete-light/test_project/src/genericm2m/foo.pyc
# trying /home/jpic/work/mez/src/foo.cpython-36m-x86_64-linux-gnu.so
# trying /home/jpic/work/mez/src/foo.abi3.so
# trying /home/jpic/work/mez/src/foo.so
# trying /home/jpic/work/mez/src/foo.py
# trying /home/jpic/work/mez/src/foo.pyc
# trying /usr/lib/python3.6/site-packages/foo.cpython-36m-x86_64-linux-gnu.so
# trying /usr/lib/python3.6/site-packages/foo.abi3.so
# trying /usr/lib/python3.6/site-packages/foo.so
# trying /usr/lib/python3.6/site-packages/foo.py
# trying /usr/lib/python3.6/site-packages/foo.pyc
# trying /usr/lib/python3.6/site-packages/gtk-2.0/foo.cpython-36m-x86_64-linux-gnu.so
# trying /usr/lib/python3.6/site-packages/gtk-2.0/foo.abi3.so
# trying /usr/lib/python3.6/site-packages/gtk-2.0/foo.so
# trying /usr/lib/python3.6/site-packages/gtk-2.0/foo.py
# trying /usr/lib/python3.6/site-packages/gtk-2.0/foo.pyc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'foo'

请注意,上述调试输出是基于标准错误而非标准输入。这意味着您应该使用&>代替>将其重定向到bash4上的文件,或者在我认为上的2>后阅读{{ 3}}

我不知道日食,所以我无法谈论它的作用,但这个技巧应该会给你足够的翻译反馈来破解你自己的解决方案。

另一种可能性是Eclipse使用Python3并且您在CLI中使用Python2。在这种情况下,您需要在模块目录中添加__init__.py个文件,或者更确切地说,确保您的CLI是python 3,请与python --version一起检查。例如:

07/09 2017 12:03:11 jpic@mez /tmp 
$ mkdir foo

07/09 2017 12:03:13 jpic@mez /tmp 
$ touch foo/bar.py

07/09 2017 12:03:16 jpic@mez /tmp 
$ python3
Python 3.6.2 (default, Jul 20 2017, 03:52:27) 
[GCC 7.1.1 20170630] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo.bar
>>> 

07/09 2017 12:03:20 jpic@mez /tmp 
$ python2
Python 2.7.13 (default, Jul 21 2017, 03:24:34) 
[GCC 7.1.1 20170630] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named foo.bar
>>> 

07/09 2017 12:03:28 jpic@mez /tmp 
$ touch foo/__init__.py

07/09 2017 12:03:33 jpic@mez /tmp 
$ python2
Python 2.7.13 (default, Jul 21 2017, 03:24:34) 
[GCC 7.1.1 20170630] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo.bar

答案 1 :(得分:1)

我终于设法解决了这个问题。当这里的一些答案指向PYTHONPATH我用PATH(环境变量)误解了它,因此仅通过编辑sys.path没有解决我的问题。

当我意识到我需要自己创建PYTHONPATH并添加自己的路径时,我设法导入了我的包。

感谢所有在没有您的情况下回答的人,我对此问题的解决方案没有任何指示

答案 2 :(得分:0)

您不应该依赖IDE来使模块可导入。

使用虚拟环境时(或者,使用sudo在系统Python中安装时,您自担风险),使用pip实用程序可以轻松安装正确打包的软件。

在学习如何操作之前,请注意PYTHONPATH环境变量应该是分号(Windows)或冒号(Unix)分隔的目录列表,每个目录都附加在{{1}之前在翻译启动时。因此,您可以确保您的软件即使不是sys.path可安装的,也可以使用。