默认情况下,我有带有python2.6的ubuntu 10.04。我已经安装了python2.7。
当我想安装 python 包时
apt-get python-<package>
它被安装到python2.6。如何使它安装包到 python2.7?有什么选择吗?
我已经看过这个,但我在我的操作系统中找不到这样的目录。我考虑过使用 easy_install-2.7
,但并非所有软件包都受支持。例如python-torctl
.
我对将python2.7与apt-get install
绑定更感兴趣。
Python有自己的包管理工具,与Linux发行版(包括Ubuntu)设置的包管理工具并行。存储库是 Pypi - Python 包索引,包与 pip
或 easy_install 脚本一起安装,这是 Python 的 setuptools 包的一部分。
根据经验,您不应该同时使用通过pip/setuptools安装的软件包和发行版可用的软件包(通过apt-get,yum,urpmi等),因为它们可能会发生冲突。
因此,处理它的一种不太容易出错的方法是在系统中安装单独的 Python - 将系统附带的 python 留给系统脚本等 - 在此 python 上,仅使用包管理器安装的包。并安装其他版本的Python(甚至相同),以使用"virtualenv"运行 - 在这些其他安装中,您仅使用pip/setuptools安装东西。
(即使一个人选择大胆地生活而不使用virtualenvs,在与系统的Python相同的前缀(/usr
,甚至/usr/local
)上安装另一个python版本也是混淆错误和冲突的来源)。
请注意,Debian 和 Ubuntu 系统设计了一种在/usr 中并行运行官方 Python 的方法,并让 apt-get 同时将 Python 包安装到两个 Python 版本。这基本上有效,但它们弄乱了 Python 的默认目录层次结构,并且某些应用程序无法以这种方式使用 Python。(在 Debian 或 Ubuntu 中找到模块文件本身也是一团糟)。因此,即使您的系统在 apt-get 上确实有多个版本的 Python 可用,上述方法也适用。
简而言之,一旦你编译了你想要的Python版本,就这样做:
- 使用系统的包管理器安装"python-setuptools"和"python-virtualenv"(不确定这些是否是实际的包名称)。
- 使用
virtualenv
创建一个环境,您将在其中使用不同的 Python 版本 - 激活您的虚拟环境,并使用
pip
安装Python软件包。
Virtualenv 确实有一个"--help"开关来帮助你,但你基本上是这样做的:
$ virtualenv -p <path-to-python-interpreter> <environment-dir>
$ source <environment-dir>/bin/activate
这就是 - 由于设置了环境变量,所有使用 Python 的东西都会在 virtualenv 中"看到"解释器。
ubuntu 10.04 没有 python2.7 包。 你必须自己构建 2.7。 我确实读过一篇关于 ubuntu 在 12.04 发布时发布 python2.7 包的文章,但我不确定存储库位置是什么。
http://eli.thegreenplace.net/2011/10/10/installing-python-2-7-on-ubuntu/
或:
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.7
https://askubuntu.com/questions/101591/install-python-2-7-2-on-ubuntu-10-04-64-bit
这个问题在网上有很多答案。
pyenv
https://github.com/pyenv/pyenv
Pyenv 允许您为单个用户管理多个 Python 版本而无需 sudo,就像 Node.js NVM 和 Ruby RVM 一样。
安装Pyenv:
curl https://pyenv.run | bash
然后添加到您的.bashrc
:
export PATH="${HOME}/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
查找要安装的 Python 版本:
pyenv install --list
安装您想要的 python 版本:
# Increase the chances that the build will have all dependencies.
# https://github.com/pyenv/pyenv/wiki/Common-build-problems
sudo apt build-dep python3
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
# Build and install a Python version from source.
pyenv install 3.8.0
列出可用的 Python 版本:
pyenv versions
我们现在有:
* system (set by /home/cirsan01/.pyenv/version)
3.8.0
选择不同的 python 版本:
pyenv global 3.8.0
python --version
python3 --version
两个输出:
Python 3.8.0
我们现在可以继续正常安装和使用软件包:
pip install cowsay
python -c 'import cowsay; cowsay.tux("Python is fun")'
cowsay 'hello'
我们可以通过以下方式确认所有内容都本地安装在我们干净的环境中:
python -c 'import cowsay; print(cowsay.__file__)'
给:
/home/ciro/.pyenv/versions/3.8.0/lib/python3.8/site-packages/cowsay/__init__.py
和:
which cowsay
给:
/home/ciro/.pyenv/shims/cowsay
和:
which python
给:
/home/ciro/.pyenv/shims/python
按项目使用情况
在上一节中,我们了解了如何在全局设置中使用 pyenv。
但是,您通常想要的是在每个项目的基础上设置特定的 python 和包版本。这是怎么做到的。
首先像以前一样安装所需的 Python 版本。
然后,从项目目录中,使用以下命令设置所需的 python 版本:
pyenv local 3.8.0
这将创建一个包含版本字符串的文件.python-version
。
现在让我们在本地为我们的项目安装一个包: TODO:似乎没有好方法:Pyenv 选择虚拟环境目录
现在,当有人想要使用您的项目时,他们将执行以下操作:
pyenv local
这会将 Python 版本设置为正确的版本。
相关主题:
- https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get
- https://unix.stackexchange.com/questions/9711/what-is-the-proper-way-to-manage-multiple-python-versions
- apt-get 安装不同的 python 版本
在 Ubuntu 18.04, pyenv 1.2.15 上测试。