10

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?

ecjb
  • 5,169
  • 12
  • 43
  • 79
  • Are you using Win / Mac / Linux? – Pitto Nov 05 '18 at 10:04
  • Thank you @neophlegm and @Mathieu. I tried the command `sys.path` and I get ['', '/anaconda3/lib/python36.zip', '/anaconda3/lib/python3.6', '/anaconda3/lib/python3.6/lib-dynload', '/anaconda3/lib/python3.6/site-packages', '/anaconda3/lib/python3.6/site-packages/aeosa', '/anaconda3/lib/python3.6/site-packages/cycler-0.10.0-py3.6.egg', '/anaconda3/lib/python3.6/site-packages/IPython/extensions', '/Users/mymac/.ipython']. Isn't it possible to "merge" the 2 locations where python is searching for packages? – ecjb Nov 05 '18 at 10:05
  • @ecjb: while it may be possible to add a shared path to all your Python installations, I would **not** do that. It's easy enough to pick the right Python binary to install packages with. – Martijn Pieters Nov 05 '18 at 10:06
  • 1
    @Pitto: clearly a Mac. – Martijn Pieters Nov 05 '18 at 10:08
  • Thank you for your answer @MartijnPieters. So you mean I should ALWAYS install packages with `pip3` and NEVER with `conda install`? – ecjb Nov 05 '18 at 10:09
  • @ecjb: `conda` is a manager for a wider environment *around* Python. `pip` is the installer for Python-specific packages. If there is a `conda` package, then that's preferable, but for general Python packages, use `pip`. – Martijn Pieters Nov 05 '18 at 10:35
  • 1
    @ecjb: in this case there is a conda package for your project too, so using `conda install` clearly is an option here. – Martijn Pieters Nov 05 '18 at 10:46
  • I have a similar issue but my pkg is installed with conda 100%, I see it in conda list. But python cannot find it although it CAN find other libaries that conda says it has installed. How do I force conda to reveal to me where it installed it? – Charlie Parker Sep 24 '20 at 18:34
  • @CharlieParker, Since I asked the question, I could gain (I hope to think at least) some more understanding of the problem: I strongly advise you not to go through conda solution but rather install environment for each projects you made, always activate this environnment and install the packages you need in it (I rarely need more than numpy, pandas, scipy and few others). Thus you really gain control of what is being done under the hood. For this look at this webpage: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/ – ecjb Sep 24 '20 at 19:28

4 Answers4

15

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • thank you @MartijnPieters for this extensive answer. Package managing in Python is complicated! I'm trying to process all that information. One question: what exactly are you referring when you write `PATH`. Is it a command to access the path where the packages are install. If this is a piece of command, what would be the full command (or maybe I misunderstood) – ecjb Nov 05 '18 at 10:56
  • I have a similar issue but my pkg is installed with conda 100%, I see it in conda list. But python cannot find it although it CAN find other libaries that conda says it has installed. How do I force conda to reveal to me where it installed it? – Charlie Parker Sep 24 '20 at 18:13
2

which python

gives the you the PATH to python

and then /path/to/python -m pip install thepackagetobeinstalled

Many thanks @MartijnPieters

ecjb
  • 5,169
  • 12
  • 43
  • 79
  • 1
    I have a similar issue but my pkg is installed with conda 100%, I see it in conda list. But python cannot find it although it CAN find other libaries that conda says it has installed. How do I force conda to reveal to me where it installed it? – Charlie Parker Sep 24 '20 at 18:35
1

As you can read here:

pip3 and pip would make a difference only when you are not using any environment managers like virualenv (or) conda. Now as you are creating a conda environment which has python==3.x, pip would be equivalent to pip3.

For this reason it could be you did not activate your Conda environment before installing required packages and running your code.

Activate the new environment:

On Windows:

activate myenv

On macOS (this should be your option) and Linux:

source activate myenv

NOTE: Replace myenv with the name of the environment.

Pitto
  • 8,229
  • 3
  • 42
  • 51
  • @Pitto I tried the `conda install` command and then it worked. I then only used the `pip3` command. So I unfortunately cannot tell why it worked (because of `pip3` or `conda install`) – ecjb Nov 05 '18 at 10:21
  • Perfect! Just happy it worked out :) In my experience - once your conda env is correctly activated - you can use both conda install and pip install without issues. Happy hacking! – Pitto Nov 05 '18 at 10:22
  • The environment advice assumes that there is a conda environment created. That's not a requirement, not even on Anaconda. `pip` can still be the command for the Python 3 installation, it is not a given that that is attached to a Python 2 installation. – Martijn Pieters Nov 05 '18 at 10:29
  • I have a similar issue but my pkg is installed with conda 100%, I see it in conda list. But python cannot find it although it CAN find other libaries that conda says it has installed. How do I force conda to reveal to me where it installed it? – Charlie Parker Sep 24 '20 at 18:34
  • Hi @CharlieParker Please open a question here on stackoverflow so that we check and provide answer that can be useful also for other users. – Pitto Sep 24 '20 at 20:07
1

You have installed python2.x package and you're using python3.x. Try:

pip3 install wfdb

If you don't have pip3 run:

[apt-get/yum] install python3-pip

You can see what packages you have currently installed by running:

pip freeze

and for python 3.x packages

pip3 freeze

Please remember each time you install a Python package, it will be placed in the directory for one particular Python version. Hence your error.

Raoslaw Szamszur
  • 1,723
  • 12
  • 21
  • The OP is clearly using Mac, so the packager used here is incorrect. They also showed that `pip` already installed into a Python 3 installation, it's just the wrong one. `pip3` is just one of 3 `pip` variants and `pip` does not automatically mean it is installed into Python 2., that's heavily dependent on the OS, if a packaging system was used, and in many cases, the installation order. – Martijn Pieters Nov 05 '18 at 10:26
  • All of this means that this answer is not helpful for the OP. The generic advice would be helpful for a Ubuntu or Debian system but this doesn't translate to a Mac environment. – Martijn Pieters Nov 05 '18 at 10:27
  • @MartijnPieters I realized that OP uses Mac after I've answered. Unfortunately, my knowledge of this OS is very little, therefore I'm not sure if my answer is helpful in any way for the OP (at least it points where the root cause is). Anyways, good thing you managed to solve this issue. Have a good one! – Raoslaw Szamszur Nov 05 '18 at 12:38
  • I have a similar issue but my pkg is installed with conda 100%, I see it in conda list. But python cannot find it although it CAN find other libaries that conda says it has installed. How do I force conda to reveal to me where it installed it? – Charlie Parker Sep 24 '20 at 18:34