Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ImportError while module is installed [Ubuntu]

I'd like to make a switch from Windows to Linux (Ubuntu) writing my python programs but I just can't get things to work. Here's the problem: I can see that there are quite the number of modules pre-installed (like numpy, pandas, matplotlib, etc.) in Ubuntu. They sit nicely in the /host/Python27/Lib/site-packages directory. But when I write a test python script and try to execute it, it gives me an ImportError whenever I try to import a module (for instance import numpy as np gives me ImportError: No module named numpy). When I type which python in the commandline I get the /usr/bin/python path. I think I might need to change things related to the python path, but I don't know how to do that.

like image 779
MPA Avatar asked Jun 13 '13 09:06

MPA


People also ask

How do I fix the ImportError in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

How do I resolve ImportError no module name?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

How do I use Python ImportError?

So to do this we use “import” keyword such as import statement with the module name. When writing this statement and the specified module is not written properly or the imported module is not found in the Python library then the Python interpreter throws an error known as ImportError. If the module does not exist.

Why is Python not importing module?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.


2 Answers

You can use the following command in your terminal to see what folders are in your PYTHONPATH.

python -c "import sys, pprint; pprint.pprint(sys.path)"

I'm guessing /host/Python27/Lib/site-packages wont be in there (it doesn't sound like a normal python path. How did you install these packages?).

If you want to add folders to your PYTHONPATH then use the following:

export PYTHONPATH=$PYTHONPATH:/host/Python27/Lib/site-packages

Personally here are some recommendations for developing with Python:

  1. Use virtualenv. It is a very powerful tool that creates sandboxed python environments so you can install modules and keep them separate from the main interpreter.

  2. Use pip - When you've created a virtualenv, and activated it you can use pip install to install packages for you. e.g. pip install numpy will install numpy into your virtual environment and will be accessible from only this virtualenv. This means you can also install different versions for testing etc. Very powerful. I would recommend using pip to install your python packages over using ubuntu apt-get install as you are more likely to get the newer versions of modules (apt-get relies on someone packaging the latest versions of your python libraries and may not be available for as many libraries as pip).

  3. When writing python scripts that you will make executable (chmod +x my_python_script.py) make sure you put #!/usr/bin/env python at the top as this will pick up the python interpreter in your virtual environment. If you don't (and put #!/usr/bin/python) then running ./my_python_script.py will always use the system python interpreter.

like image 153
Ewan Avatar answered Sep 29 '22 22:09

Ewan


/host/Python27/Lib/site-packages is not a default python directory on linux installations as far as I am aware.

The normal python installation (and python packages) should be found under /usr/lib or /usr/lib64 depending on your processor architecture.

If you want to check where python is searching in addition to these directories you can use a terminal with the following command:

echo $PYTHONPATH

If the /host/Python27/Lib/site-packages path is not listed, attempt to use the following command and try it again:

export PYTHONPATH=$PYTHONPATH:host/Python27/Lib/site-packages

If this should work and you do not want to write this in a terminal every time you want to use these packages, simply put it into a file called .bashrc in your home folder (normally /home/<username>).

like image 43
BergmannF Avatar answered Sep 29 '22 21:09

BergmannF