Generally this post is about to setup a Python3 environment on Ubuntu 16.04. However since there seems a problem in pip so let me name it in the title directly.
I started with a fresh new Ubuntu 16.04 droplet at DigitalOcean and have run these commands.
(As non-root with sudo
)
$ sudo apt-get update
$ sudo apt-get install python3-pip -y
$ sudo pip3 install -U pip
So pip 10 is installed and things seem fine until here. However when I attempt to run pip3
, it crashes with an unclear Python stacktrace.
$ pip3 --version
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main'
It seems that when apt-installed pip upgrades a pypi pip, the pypi pip will be broken and won't work. And the idea is not to use the system-wide pip but to use a venv
pip instead.
So the solution is like this.
$ sudo apt-get install python3-venv
$ python3 -m venv $HOME/.venv # or any directory you like
$ export PATH=$HOME/.venv/bin:$PATH
Then the pip in the virtualenv will work.
$ which python
/home_directory/.venv/bin/python
$ python --version
python 3.5.2
$ which pip
/home_directory/.venv/bin/pip
Now upgrade pip with itself.
$ pip install -U pip
...
$ pip --version
pip 10.0.1 from /home_direcotry/.venv/lib/python3.5/site-packages/pip (python 3.5)
Also, you can add export PATH=$HOME/.venv:$PATH
to your .bashrc
or .zshrc
or anything like that.