Ubuntu 下安装 pip
问题
$ sudo -H python get-pip.py --prefix /usr
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
Downloading https://files.pythonhosted.org/packages/30/db/9e38760b32e3e7f40cce46dd5fb107b8c73840df38f0046d8e6514e675a1/pip-19.2.3-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 1.1MB/s
Installing collected packages: pip
Successfully installed pip-19.2.3
$ which pip
/usr/bin/pip
$ pip --version
Traceback (most recent call last):
File "/usr/bin/pip", line 6, in <module>
from pip._internal import main
ImportError: No module named pip._internal
这个问题只会在 Debian/Ubuntu 系统上出现,因为 Debian 系默认将 Python 的 site-packages 路径改成了 dist-packages:
$ lsb_release -i
Distributor ID: Ubuntu
$ python -m site
sys.path = [
'/home/runsisi/working',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/home/runsisi/.local/lib/python2.7/site-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
]
USER_BASE: '/home/runsisi/.local' (exists)
USER_SITE: '/home/runsisi/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True
但是 get-pip.py 会把 pip 安装到 site-packages:
$ ls /usr/lib/python2.7/site-packages/
pip pip-19.2.3.dist-info
$ ls /usr/lib/python2.7/dist-packages/ | grep pip
解决办法
两种方式:
添加 site-package 目录
$ echo '/usr/lib/python2.7/site-packages/' | sudo tee /usr/lib/python2.7/dist-packages/xxx.pth
/usr/lib/python2.7/site-packages/
$ which pip
/usr/bin/pip
$ pip --version
pip 19.2.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
将 pip 安装至用户目录
严格来说,这不是解决办法,而是一种规避手段:
$ python get-pip.py --user
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
Using cached https://files.pythonhosted.org/packages/30/db/9e38760b32e3e7f40cce46dd5fb107b8c73840df38f0046d8e6514e675a1/pip-19.2.3-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-19.2.3
$ which pip
/home/runsisi/.local/bin/pip
$ pip --version
pip 19.2.3 from /home/runsisi/.local/lib/python2.7/site-packages/pip (python 2.7)
$ pip install ipython --user
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting ipython
Using cached https://files.pythonhosted.org/packages/b0/88/d996ab8be22cea1eaa18baee3678a11265e18cf09974728d683c51102148/ipython-5.8.0-py2-none-any.whl
...
参考资料
What’s the difference between dist-packages and site-packages?
How do I find the location of my Python site-packages directory?
Configuring Python to use additional locations for site-packages
最后修改于 2019-09-17