正确安装 Python 包
sudo install vs pip install --user
先放结论:推荐使用pip install --user package_name
sudo pip installBothsudo pip installand its other common variantsudo -H pip installshould not be encouraged because it is a security risk to use root privileges to usepipto install Python packages from PyPI (Python Package Index).当你使用 sudo 运行 pip 时,你以 sudo 权限运行
setup.py。换句话说,你在以 sudo 权限运行一段从网络上下载的 Python 代码,存在极大的安全隐患。pip install --user该命令将 python 包安装到本地用户目录,例如:
~/.local/lib/python
Arch Linux 上安装 Python 包
诸如 Arch Linux、Ubuntu 等发行版自带包管理器,例如: Arch Linux 的 pacman,而 Python 又常用 pip 作为其包管理器,那我们该使用哪一种呢?
一个原则:坚持用一种包管理器
建议:
在可用的情况下,优先使用 pacman 包管理器安装 Python 包
Python3:
sudo pacman -S python-'package'例如:
pacman -S python-bs4Python2:
sudo pacman -S python2-'package'大多数包都可以通过 pacman 进行安装,当一些包既没有在 ArchLinux 仓库,又没有在 AUR 的时候,你需要下载 PKGBUILD 文件,编译然后使用 pacman 安装
1
2makepkg -s
sudo pacman -U 'compiled-package'pacman 安装不来(仓库里没有)的时候,使用 pip 安装
当 AUR 中没有该包或者 PKGBUILD 不管用时,使用 pip 安装 格式:
pip install --user package_namePython3:
sudo pip install 'python-package'Python2:sudo pip2 install 'python-package'注意:确保包只安装在你的家目录下,以避免与系统包混淆。
Never install anything with pip without --user, it will install files to the system and conflict with pacman
对于 Python 项目(因为项目的依赖包不是常用),所以一般使用虚拟环境
例如:
virtualenvsudo pacman -S python-virtualenv