129

I've installed Python 3.7 on my Ubuntu 18.04 machine. Following this instructions in case it's relevant:

Download : Python 3.7 from Python Website [1] ,on Desktop and manually unzip it, on Desktop Installation : Open Terminal (ctrl +shift+T)

Go to the Extracted folder
$ cd ~/Desktop/Python-3.7.0
$ ./configure
$ make
$ sudo make install

Making Python 3.7 default Python :

$ sudo vim ~/.bashrc
press i
on the last and new line - Type
alias python= python3.7
press Esc
type - to save and exit vim
:wq
now type
$ source ~/.bashrc

From here: https://www.quora.com/How-can-I-upgrade-Python-3-6-to-3-7-in-Ubuntu-18-04

I've downloaded several modules through pip install module but when I try to import them, I get a ModuleNotFoundError: No module names 'xx'

So I did some research and apparently when used pip to install, it installed in the modules in previous version of Python. Somewhere (probably a question in SO) I found a suggestion to install the module using python3.7 -m pip install module but then I get /usr/local/bin/python3.7: no module named pip.

Now I'm stuck, pip is installed, but apparently not for Python 3.7. I'm assuming that if I can install pip for Python 3.7, I can run the pip install command and get the modules I need. If that is the case, how can I install pip for python 3.7, since it's already installed?


This is the best I have come up with:

I have installed python 3.7 successfully and I can install modules using pip (or pip3) but those modules are installed in Python 3.6 (Comes with ubuntu). Therefore I can't import those modules in python 3.7 (get a module not found)

Python 3.7 doesn't recognize pip/pip3, so I can't install through pip/pip3 I need python 3.7

Neuron
  • 5,141
  • 5
  • 38
  • 59
Justcurious
  • 1,952
  • 4
  • 11
  • 17

14 Answers14

166

In general, don't do this:

pip install package

because, as you have correctly noticed, it's not clear what Python version you're installing package for.

Instead, if you want to install package for Python 3.7, do this:

python3.7 -m pip install package

Replace package with the name of whatever you're trying to install.

Took me a surprisingly long time to figure it out, too. The docs about it are here.

Your other option is to set up a virtual environment. Once your virtual environment is active, executable names like python and pip will point to the correct ones.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
mpenkov
  • 21,621
  • 10
  • 84
  • 126
  • 70
    Already tried, output: `/usr/local/bin/python3.7: no module named pip` [Same for pip3] – Justcurious Feb 19 '19 at 16:58
  • 14
    Sounds like your Python 3.7 install is missing pip. You could try installing it using: `python3.7 -m ensurepip`. Also, I notice that you installed from source. If the above options don't work for you, then perhaps try installing via apt instead: `apt install python3.7`? The apt package comes with the pip module installed. – mpenkov Feb 20 '19 at 03:10
  • 6
    I tried `python3.7 -m ensurepip`, it installed but when I used `python3.7 -m pip install package` got a bunch of errors. Since I was on a VM, I went back before installing python 3.7 and tried `apt install python3.7` and then `python3.7 -m pip install package`. Now everything is running smooth. Thanks! – Justcurious Feb 22 '19 at 17:51
  • 1
    please correct your answer. it failed. but this `python3.7 -m pip install pip` works fine – Nwawel A Iroume Apr 22 '19 at 19:06
  • 2
    You're supposed to replace "package" with the name of the package you're trying to install. – mpenkov Apr 23 '19 at 12:28
  • `Sounds like your Python 3.7 install is missing pip`, do you perhaps mean his python 3.6 is missing pip? – dangel May 15 '19 at 01:24
  • 2
    I just installed `python3.7` on fresh Ubuntu, and met `/usr/local/bin/python3.7: no module named pip`. Preceding command with `sudo apt install python3-pip` fixed the issue. – pt12lol Aug 19 '19 at 06:59
  • 2
    Neither this answer, nor ensurepip worked for me. :( – bob Feb 05 '20 at 15:09
  • Edited to add a key missing detail captured in dangel's answer. – bob Feb 05 '20 at 15:18
  • Well I tried to edit it. Not a complete answer without that info, so to all: if you run into the issue I did, apply dangel's answer first. See my answer below for a complete solution. – bob Feb 05 '20 at 16:23
  • this approach is also helping when you need to install e.g. pip3.8 – simkusr Apr 13 '20 at 06:34
  • This also will need `export PATH=$HOME/.local/bin:$PATH` cause `python3.7 -m pip install pip` puts pip into `$HOME/.local/` – manasouza May 04 '20 at 22:20
  • 14
    `No module named ensurepip` -_- – Rainb May 20 '20 at 21:03
134

A quick add-on to mpenkov's answer above (didn't want this to get lost in the comments)

For me, I had to install pip for 3.6 first

sudo apt install python3-pip

now you can install python 3.7

sudo apt install python3.7

and then I could install pip for 3.7

python3.7 -m pip install pip

and as a bonus, to install other modules just preface with

python3.7 -m pip install <module>

EDIT 1 (12/2019):

I know this is obvious for most. but if you want python 3.8, just substitute python3.8 in place of python3.7

EDIT 2 (5/2020):

For those that are able to upgrade, Python 3.8 is available out-of-the-box for Ubuntu 20.04 which was released a few weeks ago.

dangel
  • 7,238
  • 7
  • 48
  • 74
35

This works for me.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Then this command with sudo:

python3.7 get-pip.py

Based on this instruction.

Tomek Tajne
  • 658
  • 7
  • 7
22

I used apt-get to install python3.7 in ubuntu18.04. The installations are as follows.

  1. install python3.7
sudo apt-get install python3.7 
  1. install pip3. It should be noted that this may install pip3 for python3.6.
sudo apt-get install python3-pip 
  1. change the default of python3 for python3.7. This is where the magic is, which will make the pip3 refer to python3.7.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

Hope it works for you.

Kealen
  • 229
  • 2
  • 4
10

To install all currently supported python versions (python 3.6 is already pre-installed) including pip for Ubuntu 18.04 do the following:

To install python3.5 and python3.7, use the deadsnakes ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5
sudo apt-get install python3.7

Install python2.7 via distribution packages:

sudo apt install python-minimal  # on Ubuntu 18.04 python-minimal maps to python2.7

To install pip use:

sudo apt install python-pip  # on Ubuntu 18.04 this refers to pip for python2.7
sudo apt install python3-pip  # on Ubuntu 18.04 this refers to pip for python3.6
python3.5 -m pip install pip # this will install pip only for the current user
python3.7 -m pip install pip

I used it for setting up a CI-chain for a python project with tox and Jenkins.

S. Brunner
  • 101
  • 3
9

Combining the answers from @mpenkon and @dangel, this is what worked for me:

  1. sudo apt install python3-pip

  2. python3.7 -m pip install pip

Step #1 is required (assuming you don't already have pip for python3) for step #2 to work. It uses pip for Python3.6 to install pip for Python 3.7 apparently.

bob
  • 419
  • 4
  • 11
  • 6
    One problem here is that `sudo apt install python3-pip` does not guarantee the installation of python3.7, but other python3 versions as well – manasouza May 04 '20 at 21:54
  • That's true, but oddly why it seems to work (I think; not at all a Linux guru). It seems like some type of bootstrap approach is needed since you can't use software you don't have to install software you don't have. In any case it did work for me. Did you run into issues following this approach? – bob May 05 '20 at 13:06
  • In the way that it did not installed python 3.7, yes. Definitely the best approach is to use `sudo apt install python3.7` considering Ubuntu 18.04. – manasouza May 05 '20 at 14:01
3

When i use apt install python3-pip, i get a lot of packages need install, but i donot need them. So, i DO like this:

apt update
apt-get install python3-setuptools
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
rm -f get-pip.py
JJP
  • 825
  • 1
  • 7
  • 12
3

The following steps can be used:


sudo apt-get -y update
---------
sudo apt-get install python3.7
--------------
 python3.7
-------------
 curl -O https://bootstrap.pypa.io/get-pip.py
-----------------
sudo apt install python3-pip
-----------------
sudo apt install python3.7-venv
-----------------
 python3.7 -m venv /home/ubuntu/app
-------------
 cd app   
----------------
 source bin/activate
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
  • sudo apt-get update sudo apt-get -y update sudo apt-get install python3.7 python3.7 curl -O https://bootstrap.pypa.io/get-pip.py sudo apt install python3-pip sudo apt install python3.7-venv python3.7 -m venv /home/ubuntu/app cd app ---- source bin/activate – Anil Konduru Sep 13 '19 at 09:38
3

Install python pre-requisites

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Install python 3.7 (from ppa repository)

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7

Install pip3.7

sudo apt install python3-pip
python3.7 -m pip install pip

Create python and pip alternatives

sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3.7 10
sudo update-alternatives --install /usr/local/bin/pip pip /home/your_username/.local/bin/pip3.7 10

Make changes

source ~/.bashrc
python --version
pip --version
Marvin Correia
  • 851
  • 7
  • 10
1

For those who intend to use venv:

If you don't already have pip for Python 3:

sudo apt install python3-pip

Install venv package:

sudo apt install python3.7-venv

Create virtual environment (which will be bootstrapped with pip by default):

python3.7 -m venv /path/to/new/virtual/environment

To activate the virtual environment, source the appropriate script for the current shell, from the bin directory of the virtual environment. The appropriate scripts for the different shells are:

bash/zsh – activate

fish – activate.fish

csh/tcsh – activate.csh

For example, if using bash:

source /path/to/new/virtual/environment/bin/activate

Optionally, to update pip for the virtual environment (while it is activated):

pip install --upgrade pip

When you want to deactivate the virtual environment:

deactivate 
James T
  • 795
  • 9
  • 11
1

I installed pip3 using

python3.7 -m pip install pip

But upon using pip3 to install other dependencies, it was using python3.6.
You can check the by typing pip3 --version

Hence, I used pip3 like this (stated in one of the above answers):

python3.7 -m pip install <module>

or use it like this:

python3.7 -m pip install -r requirements.txt

I made a bash alias for later use in ~/.bashrc file as alias pip3='python3.7 -m pip'. If you use alias, don't forget to source ~/.bashrc after making the changes and saving it.

Sunil Singh
  • 56
  • 1
  • 5
1

How about simply

add-apt-repository ppa:deadsnakes/ppa
apt-get update
apt-get install python3.7-dev
alias pip3.7="python3.7 -m pip"

Now you have the command

pip3.7

separately from pip3.

El Sampsa
  • 1,673
  • 3
  • 17
  • 33
1
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.7

if all else fails.

DragonLord
  • 6,395
  • 4
  • 36
  • 38
-2

pip3 not pip. You can create an alias like you did with python3 if you like.

Patrick Conwell
  • 630
  • 1
  • 5
  • 10