23

I have installed python3.8 to python 19.10:

 sudo apt install python3.8

I now want to install pip for python 3.8

 python3.8 -m pip install pip
 Requirement already satisfied: pip in /usr/lib/python3/dist-packages (18.1)

But the pip 3 is 3.7

 pip3 -V
 pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

So that, for example :

pip3 install pyinotify
Requirement already satisfied: pyinotify in ./.local/lib/python3.7/site-packages (0.9.6)

Attempting to import pyinotify to a python3.8 script throws a ModuleNotFound error

6 Answers 6

15

In order to make sure you are using the correct pip, please use it as follows:

python3.8 -m pip install pyinotify

Brett Cannon, one of the Python core developers, just recently published a blog article about this topic: https://snarky.ca/why-you-should-use-python-m-pip/

This all said, you usually want to install Python packages into a virtual environment, not in the system Python.

https://realpython.com/python-virtual-environments-a-primer/

1
9

sudo apt install python3-pip or python3.8-pip .

Let the system manage your Python version.


Edit:

The original question was trying to install pip via python -m pip, and after the original poster used apt, the system package manager, instead, they were able to get pip working correctly.

As noted in the comments and in jugmac00's answer, it is now recommended to use pip by calling as a Python module:

python -m pip install pyinotify

If multiple versions of Python are installed on the system and this isn't the default version, you may need to specify the version:

python3.8 -m pip install pyinotify
4
  • 15
    E: Unable to locate package python3.8-pip. There's a python3-pip package in ubuntu standard repos, but not python3.8-pip, and installing python3-pip in an 3.8 environment installs Python 3.6 on the system. Apr 4, 2020 at 21:30
  • the answer by jugmac00 works, this does not work
    – con
    Apr 15, 2020 at 15:50
  • 1
    This answer worked at the time of posting and worked for the original poster. Things have shifted and accepted answers cannot be deleted. The point remains the same, though... it is best to let the system's package manager manage the python version if possible.
    – earthmeLon
    Apr 16, 2020 at 17:13
  • @earthmeLon I have modified the question to indicate the Ubuntu release that motivated this question. I hope this will clear things up for people encountering your answer. May 18, 2020 at 12:47
0

Have you tried using Miniconda or Anaconda?

In a nutshell, it's a good way to test different python versions and you won't have to worry about messing up your system python (projects/installers that make use of the keyword python).

  1. After installation, open terminal and initialize conda (if you didn't add it to PATH) source ~/miniconda3/bin/activate
  2. Add the channels intel and conda-forge to find packages there: conda config --add channels intel && conda config --add channels conda-forge
  3. Create a new environment called py38 with python 3.8 and pip conda create -n py38 python=3.8 pip
  4. Conda will solve the environment and make sure the packages are compatible. Addressing your initial problem, you can double check the pip version (when I did it, it was pip-19.3.1-py38_0
  5. Activate your environment conda activate py38
  6. Now when you type python while inside this environment, it will use the interpreter you set up. Also, pip install and conda install will be directed to your py38 environment until you conda deactivate or switch.

Here's the output when I ran conda create -n py38 python=3.8 pip.

Collecting package metadata (current_repodata.json): done Solving environment: done

Package Plan

environment location: /home/mikkeyboi/miniconda3/envs/py38

added / updated specs: - pip - python=3.8

The following packages will be downloaded:

package                    |            build
---------------------------|-----------------
certifi-2019.9.11          |           py38_0         147 KB  conda-forge
pip-19.3.1                 |           py38_0         1.9 MB  conda-forge
python-3.8.0               |       h357f687_3        39.0 MB  conda-forge
setuptools-41.6.0          |           py38_1         628 KB  conda-forge
wheel-0.33.6               |           py38_0          35 KB  conda-forge
------------------------------------------------------------
                                       Total:        41.7 MB

The following NEW packages will be INSTALLED:

_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
ca-certificates
conda-forge/linux-64::ca-certificates-2019.9.11-hecc5488_0 certifi
conda-forge/linux-64::certifi-2019.9.11-py38_0 libffi
conda-forge/linux-64::libffi-3.2.1-he1b5a44_1006 libgcc-ng
pkgs/main/linux-64::libgcc-ng-9.1.0-hdf63c60_0 libstdcxx-ng
pkgs/main/linux-64::libstdcxx-ng-9.1.0-hdf63c60_0 ncurses
conda-forge/linux-64::ncurses-6.1-hf484d3e_1002 openssl
conda-forge/linux-64::openssl-1.1.1d-h516909a_0 pip
conda-forge/linux-64::pip-19.3.1-py38_0 python
conda-forge/linux-64::python-3.8.0-h357f687_3 readline
conda-forge/linux-64::readline-8.0-hf8c457e_0 setuptools
conda-forge/linux-64::setuptools-41.6.0-py38_1 sqlite
conda-forge/linux-64::sqlite-3.30.1-hcee41ef_0 tk
conda-forge/linux-64::tk-8.6.9-hed695b0_1003 wheel
conda-forge/linux-64::wheel-0.33.6-py38_0 xz
conda-forge/linux-64::xz-5.2.4-h14c3975_1001 zlib
conda-forge/linux-64::zlib-1.2.11-h516909a_1006

Proceed ([y]/n)? y

Downloading and Extracting Packages python-3.8.0 | 39.0 MB |

############################### | 100% setuptools-41.6.0 | 628 KB | ##################################### | 100% pip-19.3.1

| 1.9 MB | ##################################### | 100% wheel-0.33.6 | 35 KB |

############################### | 100% certifi-2019.9.11 | 147 KB | ##################################### | 100% Preparing

transaction: done Verifying transaction: done Executing transaction: done

I also did pip install pyinotify to address your import inquiry, python -c "import pyinotify; print(pyinotify.__version__)" gave me 0.9.6.

0

I just solved this problem by myself because I couldn't find a simple way to make this right.

Because of Python3.7 installed first, and Python3.8 is not going to replace old pip when you install it.

Try to look up the location of pip3

$ which pip3

In my case(macOS), it's located at /usr/local/bin/pip3

$ cd /usr/local/bin

If you installed python3.8 pip properly, you're supposed to see pip3.8

If you do, try to remove it.

$ sudo rm pip

And copy pip3.8 with a new name, pip3

$ sudo cp pip3.8 pip3

That's it!!!

I'm not sure is it a perfect solution, but it works for me.

0

As suggested in official documentation you can try with get-pip.py.

wget https://bootstrap.pypa.io/get-pip.py
python3.8 get-pip.py

This will install pip as pip3.8

-1

I had the very same issue. I think the best practice is to call pip from within the python installation, which forces you to be specific on the version of python in which to install the library, ie.

python3.8 -m pip install pyinotify

Alternatively, you can change the default python installation so that pip3 will refer to python3.8. For managing python versions you can use update-alternatives:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

Now you will have to select the default version. Run the following and press 2

sudo update-alternatives --config python3

pip3 should now refer to your python3.8 package

3
  • 1
    DON'T EVER CHANGE DEFAULT PYTHON!!! It may cause your system to break and some applications won't even run.
    – Kulfy
    Aug 11, 2020 at 19:39
  • found it the hard way. I was not aware that my system needs both python2.7 and python3.6 to function. should I delete the comment? Aug 11, 2020 at 20:30
  • I'm not sure if this answer is deletable since at least 20.04 uses Python 3.8 as default Python. You can add warning for users using other releases.
    – Kulfy
    Aug 13, 2020 at 6:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .