[英]Why do I get “ImportError: No module named” when I try to run my Python script via the Interpreter?
I am relatively new to Python and already developing automation framework in my company. 我对Python比较陌生,并且已经在公司中开发自动化框架。
For the first time, during the development process, I got into a situation that I need to run several tests simultaneously. 在开发过程中,我第一次遇到了需要同时运行多个测试的情况。 Till this point I always used Eclipse, not only to develop my framework but also run tests (of course it is only one at a time).
到此为止,我一直使用Eclipse,不仅开发我的框架,还运行测试(当然,一次只测试一次)。 When I tried to run one of my tests (one that I managed to run through Eclipse) through the CMD screen "python " it didn't even start running and the result that I got was "ImportError: No module named" (relates to the imports that I do from packages that I developed).
当我尝试通过CMD屏幕“ python”运行我的一项测试(我设法通过Eclipse运行)时,它甚至没有开始运行,而我得到的结果是“ ImportError:No module named”(与从我开发的软件包中导入的内容)。
How does Eclipse manage to get along with all my Imports and run my tests, while the python interpreter couldn't ? Eclipse如何与所有Imports相处并运行测试,而python解释器却不能?
What do i need to do in order to manage to run my tests via the interpreter ? 为了通过解释器运行测试,我需要做什么?
Edit 编辑
The following are examples to what I tried in order to import my proprietary modules 以下是为导入我的专有模块而尝试的示例
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'
>>>
All I can imagine is that Eclipse would run your command from the project directory, which make relative imports work. 我只能想象Eclipse将在项目目录中运行命令,从而使相对导入起作用。
If I'm running python in /home/jpic, and foo.py exists there, then I can import foo
. 如果我在/ home / jpic中运行python,并且foo.py存在,那么我可以
import foo
。 However, if i'm running python in /home, then import foo
will fail. 但是,如果我在/ home中运行python,则
import foo
将失败。 So, make sure you cd into the right directory before calling python. 因此,在调用python之前,请确保您已进入正确的目录。
If I wanted to make foo importable from everywhere, I would have to make it a python package, which might not be something you want to do with your project (but I would, with all my projects, because it's way cool to have imports working everywhere ^^). 如果我想使foo可以从任何地方导入,则必须将其制作为python包,这可能不是您要对项目执行的操作(但是我对所有项目都应这样做,因为导入可以正常工作到处都^^)。
If you want to debug imports, run python
with -vv
, you'll get import debug output as such: 如果要调试导入,请使用
-vv
运行python
,您将获得导入调试输出,如下所示:
# 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'
Note that the above debug output is made on standard error, not standard input. 请注意,以上调试输出是基于标准错误而不是标准输入进行的。 This means that you should use
&>
instead of >
to redirect it to a file, on bash4, or 2>
on windows i think , after reading their docs . 这意味着你应该使用
&>
来代替>
将其重定向到一个文件,在bash4,或2>
在Windows上,我认为 ,看完之后他们的文档 。
I don't know eclipse so I'm unable to talk about what it does, but this trick should give you sufficient feedback from the interpreter to hack your own solution. 我不知道eclipse,所以我无法谈论它的作用,但是这个技巧应该为您提供足够的解释器反馈,以破解您自己的解决方案。
Another possibility is that Eclipse uses Python3 and you are using Python2 in the CLI. 另一种可能性是Eclipse使用Python3,而您在CLI中使用Python2。 In this case, you would need to add
__init__.py
files in your module directories, or rather, make sure your CLI is python 3, check with python --version
. 在这种情况下,您需要在模块目录中添加
__init__.py
文件,或者确保CLI是python 3,并使用python --version
检查。 Example: 例:
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
I finally managed to solve this issue. 我终于设法解决了这个问题。 When some answers here pointed PYTHONPATH I misinterpreted it with the PATH (environment variables) and therefore didn't solve my problem by only editing sys.path.
当这里的一些答案指向PYTHONPATH时,我用PATH(环境变量)误解了它,因此仅通过编辑sys.path并不能解决我的问题。
When I realized that I need to create PYTHONPATH myself and add my own paths then I managed to import my packages. 当我意识到需要自己创建PYTHONPATH并添加自己的路径时,便设法导入了我的包。
Thanks for all the people who answered without you I didn't have any direction regarding the solution of this issue 感谢所有在没有您的情况下回答的人,对于此问题的解决,我没有任何指导
You should not rely on your IDE to make modules importable. 您不应该依赖IDE使模块可导入。
When using virtual environments (or, at your own peril, using sudo
to install in the system Python), it's easy to install correctly-packaged software using the pip
utility. 使用虚拟环境时(或冒着风险,使用
sudo
在系统Python中安装),使用pip
实用程序安装正确包装的软件很容易。
Until you learn how to do those, note that the PYTHONPATH
environment variable should be a semicolon-(Windows) or colon-(Unix) separated list of directories, each of which is prepended to sys.path
at interpreter startup time. 在学习这些方法之前,请注意
PYTHONPATH
环境变量应该是用分号(Windows)或冒号(Unix)分隔的目录列表,每个目录都在解释器启动时添加到sys.path
之前。 You can therefore ensure that your software, even if not pip
-installable, can be made available for use. 因此,即使没有
pip
安装的软件,也可以确保使用该软件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.