简体   繁体   中英

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.

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). 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).

How does Eclipse manage to get along with all my Imports and run my tests, while the python interpreter couldn't ?

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.

If I'm running python in /home/jpic, and foo.py exists there, then I can import foo . However, if i'm running python in /home, then import foo will fail. So, make sure you cd into the right directory before calling 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 ^^).

If you want to debug imports, run python with -vv , you'll get import debug output as such:

# 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 .

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.

Another possibility is that Eclipse uses Python3 and you are using Python2 in the CLI. 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 . 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.

When I realized that I need to create PYTHONPATH myself and add my own paths then I managed to import my packages.

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.

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.

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. You can therefore ensure that your software, even if not pip -installable, can be made available for use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM