Skip to content
Advertisement

Python – package not found although it is installed

I have the following version of python

import sys
print(sys.version)

3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]

I installed a package with the following command

pip install wfdb

It is succesfully installed because when I then write the command:

pip show wfdb

The following information appears Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

However, when I type the command import wfdb in Python notebook or the version of python in terminal, I get the following message

No module named 'wfdb'

Does it have to do with the path on which python is checking where the packages are? How to check this and how to change it?

Advertisement

Answer

You have (at least) 2 Python installations, one managed by Anaconda, the other what appears to be an official Python.org Mac build installed system-wide. The pip command on the command-line is the one tied to the Python.org Mac build.

pip is a script that is tied to a specific Python installation, and there can be multiple versions of the script installed in different locations, and is usually also installed with pipX and pipX.Y to match the X.Y version indicator of the Python version it is tied to. For Python 3.6, that means the same script would also be available as pip3 and pip3.6. (This also means that pip can be connected to Python 2 or Python 3, depending on your exact OS setup. It is not a given that pip, without a version number, installs into Python 2.x as some answers may claim).

Note that when you run a command without a path in your shell, (such as pip as opposed to /usr/bin/pip), you are asking your shell to find the command for you in a number of locations, listed in the PATH environment variable. The first location in the PATH list with that command is then fixed. which -a <command> would tell you all possible PATH-registered locations that the command can be found in. You can always use the full path to a command to bypass the PATH search path.

You can always verify what Python version the pip command is connected to with:

pip -V

which will output the version of pip and the location it is installed with. It’ll print something like

pip pipX.pipY path/to/pythonX.Y/site-packages/pip (python X.Y)

where pipX.pipY is the pip version number and path/to/pythonX.Y tells you what Python installation this is for.

You can try to match this with the Python version by running

python -m site

which outputs the Python module search path for that Python version. Python can be run with python, pythonX and pythonX.Y too, and is subject to the same PATH search.

Note the -m switch there, that instructs Python to find a module in it’s module search path and execute it as a script. Loads of modules support being run that way, including pip. This is important as that helps avoid having to search for a better pip command if you already can start the right Python version.

You have several good options here:

  • Since you are using Anaconda, you could look for a conda package for the same project. There is such a package for wfdb. Install it with

      conda install wfdb
    

    Anaconda aims to give you a wider software management experience that includes a broader set of software options than just the Python PyPI ecosystem, and conda packages usually manage more things than just the Python package.

    Conda packages are usually maintained by a different set of developers from the package itself, so there may be a newer version available on PyPI (requiring pip install) than there is on Conda.

    This is not an option for all Python packages, if there is no conda package you have to use pip. See Installing non-conda packages.

  • you can use the conda command to create a conda environment. Once you have an environment created, you can activate it with

      source activate <name_of_cenv>
    

    to alter your PATH settings. With the envirnoment ‘active’ the first directory listed on your PATH is the one for the conda environment and the pip command will be the one tied to that environment.

    Note that a conda environment gives you an isolated environment for a specific project, keeping the library installation separate from the rest of your Python packages in the central site-packages location. If you want to install a package for all of your Anaconda Python projects, don’t use a conda environment.

  • Use the Anaconda Python binary to run pip as a module; when you can run /path/to/python or pythoncommand to open the right Python version, you can use that same path to run /path/to/python -m pip ... instead of pip ... to be absolutely certain you are installing into the correct Python version.

  • Look for a better pip command, with which -a pip or which -a pip3.6, etc. But if you already know the Python binary, look in the same bin location for pip. If you have anaconda/bin/python, then there probably is a anaconda/bin/pip too.

Advertisement