It’s not uncommon for new Pythonistas to have trouble installing packages and using their modules. Frustrating errors like this often arise, even if you think you’ve installed a package properly:
ImportError: No module named <package_name>
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.
In general, each Python installation comes bundled with its own pip
executable, used for installing packages. By default, that pip
executable will install packages in a location where that specific Python installation can find them.
The problem is that it’s very common to have multiple Python interpreters installed (and by extension, multiple pip
executables.) Depending on your shell’s PATH, running pip
may invoke the pip
executable linked to the version of Python you’re using, or to a different one. If the wrong pip
is invoked, then the packages it installs will likely not be visible to the Python interpreter you’re using, causing the ImportError
.
To use the version of pip
specific to your desired Python version, you can use python -m pip
. Here, python
is the path to the desired Python interpreter, so something like /usr/local/bin/python3.7 -m pip
will use the pip
executable for /usr/local/bin/python3.7
. However, this still has its limitations.
There are also other ways to get around this issue. You can modify your shell’s PATH so it uses the correct pip
executable, or change the PYTHONPATH
so that your desired version of Python can find the packages located in a different directory. But these can all get messy fast.
Instead, virtual environments are often used to isolate Python installations from one another. A virtual environment contains, among other things, a Python interpreter, a pip
executable, and a site-packages directory, which is the standard location for most packages downloaded with pip
.
By activating a virtual environment within your shell, you expose it to only the pip
and Python executables installed within your virtual environments, ensuring that the right versions of both applications are invoked and that packages are always installed to the correct location. Virtual environments also allow you to run different versions of the same package with different projects, something not possible if you are using a global Python installation.
There are many different virtual environments to choose from. This course uses Conda, bundled with Anaconda. You can learn more about virtual environments in Working With Python Virtual Environments.
Gerold Csendes on Jan. 20, 2021
I guess it should be pointed out that using
pip
in aconda
environment is not necessarily a good practice: anaconda docs