pythonpython-3.xpackage

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?


Solution

  • 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: