51

I'd like to start by pointing out that this question may seem like a duplicate, but it isn't. All the questions I saw here on Ask Ubuntu were regarding pip for Python 3 and I'm talking about Python 3.6. The steps used back then don't work for Python 3.6.

  1. I got a clear Ubuntu 16.10 image from the official docker store.
  2. Run apt-get update
  3. Run apt-get install python3.6
  4. Run apt-get install python3-pip
  5. Run pip3 install requests bs4
  6. Run python3.6 script.py

Got ModuleNotFoundError below:

 Traceback (most recent call last):
    File "script.py", line 6, in <module>
     import requests
 ModuleNotFoundError: No module named 'requests'

Python's and pip's I have in the machine:

python3
python3.5
python3.5m
python3.6
python3m
python3-config
python3.5-config
python3.5m-config
python3.6m
python3m-config  

pip
pip3
pip3.5
wjandrea
  • 14,236
  • 4
  • 48
  • 98
JChris
  • 993
  • 1
  • 8
  • 17

2 Answers2

58

This answer assumes that you have python3.6 installed. For python3.7, replace 3.6 with 3.7. For python3.8, replace 3.6 with 3.8, but it may also first require the python3.8-distutils package.

Installation with sudo

With regard to installing pip, using curl (instead of wget) avoids writing the file to disk.

curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6

The -H flag is evidently necessary with sudo in order to prevent errors such as the following when installing pip for an updated python interpreter:

The directory '/home/someuser/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The directory '/home/someuser/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Installation without sudo

curl https://bootstrap.pypa.io/get-pip.py | python3.6 - --user

This may sometimes give a warning such as:

WARNING: The script wheel is installed in '/home/ubuntu/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Verification

After this, pip, pip3, and pip3.6 can all be expected to point to the same target:

$ (pip -V && pip3 -V && pip3.6 -V) | uniq
pip 18.0 from /usr/local/lib/python3.6/dist-packages (python 3.6)

Of course you can alternatively use python3.6 -m pip as well.

$ python3.6 -m pip -V
pip 18.0 from /usr/local/lib/python3.6/dist-packages (python 3.6)
Asclepius
  • 939
  • 1
  • 7
  • 15
  • 6
    The script seems to assume that python3.6 has working setup-tools / easy install. For that reason it didn't work for me. I ended up using curl https://bootstrap.pypa.io/ez_setup.py -o - | python3.6 && python3.6 -m easy_install pip – FirefoxMetzger Dec 03 '17 at 17:49
  • I installed python3.6 and python3.6-dev from ppa:jonathonf/python-3.6 on a Ubuntu 14.04 . I'm not sure if I simply forgot setuptools or if it broke, but it wasn't working at the time. – FirefoxMetzger Dec 24 '17 at 21:49
  • It doesn't falsify your answer in any way =) It's just an addition in case anybody runs into the same (hopefully uncommon) problem. – FirefoxMetzger Dec 25 '17 at 10:02
  • @A-B-B If I want to pip -V be python 2.7 and pip3 -V && pip3.6 -V be python 3.6 how can I do? – Benyamin Jafari Aug 20 '18 at 07:41
  • @BenyaminJafari First run curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6 and then run curl https://bootstrap.pypa.io/get-pip.py | sudo python2.7. I haven't tried it but it may work. – Asclepius Sep 23 '18 at 17:58
  • 1
    You can use wget -O - to stream the result to stdout as well: wget -O - https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6 – ingomueller.net Mar 09 '19 at 14:35
  • curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6 was the way to go. Now my napalm gets installed for python3.6 instead of python3.5. – trogne Sep 11 '20 at 14:44
  • It seems like for Python 3.6 the command should be a little different as the suggested has failed to me with error: "ERROR: This script does not work on Python 3.6 The minimum supported Python version is 3.7. Please use https://bootstrap.pypa.io/pip/3.6/get-pip.py instead." – php-coder Jul 09 '22 at 11:56
19

I got an answer on stackoverflow.

Source: https://stackoverflow.com/a/44254088/1812319

Let's suppose that you have a system running Ubuntu 16.04, 16.10, or 17.04, and you want Python 3.6 to be the default Python.

If you're using Ubuntu 16.04 LTS, you'll need to use a PPA:

sudo add-apt-repository ppa:jonathonf/python-3.6  # (only for 16.04 LTS)

Then, run the following (this works out-of-the-box on 16.10 and 17.04):

sudo apt update
sudo apt install python3.6
sudo apt install python3.6-dev
sudo apt install python3.6-venv
wget https://bootstrap.pypa.io/get-pip.py
sudo python3.6 get-pip.py
sudo ln -s /usr/bin/python3.6 /usr/local/bin/python3
sudo ln -s /usr/local/bin/pip /usr/local/bin/pip3

# Do this only if you want python3 to be the default Python
# instead of python2 (may be dangerous, esp. before 2020):
# sudo ln -s /usr/bin/python3.6 /usr/local/bin/python

When you have completed all of the above, each of the following shell commands should indicate Python 3.6.1 (or a more recent version of Python 3.6):

python --version   # (this will reflect your choice, see above)
python3 --version
$(head -1 `which pip` | tail -c +3) --version
$(head -1 `which pip3` | tail -c +3) --version
terdon
  • 100,812
JChris
  • 993
  • 1
  • 8
  • 17