如果 OS Python 版本是 3.5,如何设置 pipenv Python 3.6 项目?

How to setup a pipenv Python 3.6 project if OS Python version is 3.5?

我的 Ubuntu 16.04.03 安装了 Python 3.5.2。当我的系统没有 python 3.6 时,如何设置 pipenv 以使用 Python 3.6?

$ pipenv --python 3.6
Warning: Python 3.6 was not found on your system…
You can specify specific versions of Python with:
  $ pipenv --python path/to/python

我不认为你可以做一个你没有的 Python 版本的 virtualenv。您可以选择以下选项之一:


  • 如果您想在 Python 的多个版本中测试您的代码,正确的方法是 Tox
  • 如果您希望在您的系统中安装多个 python,我建议您 asdf for Python

安装python3.6reference

Ubuntu 14.04 和 16.04 如果您使用 Ubuntu 14.04 或 16.04,您可以在 https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa:

使用 Felix Krull 的 deadsnakes PPA
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

或者,您可以在 https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6:

使用 J Fernyhough 的 PPA
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
Ubuntu 16.10 and 17.04

如果您使用的是 Ubuntu 16.10 或 17.04,那么 Python 3.6 在 Universe 存储库中,因此您可以 运行:

sudo apt-get update
sudo apt-get install python3.6

然后创建特定版本python env

virtualenv -p python3.6 python36venv

要么在你的 Pipfile 中手动写入你需要的版本:

[requires]
python_version = "3.6"

或者在您的系统上安装它。但我猜你 如果你打算实际 运行 pipenv install.

需要安装该版本

我建议使用 pyenv: https://github.com/pyenv/pyenv.

按照安装说明进行安装,然后安装Python 3.6 只是

的事情
pyenv install 3.6.3

然后你可以用

设置优先顺序
pyenv global system 3.6.3

此外,如果 pyenv 可用,pipenv 将自动使用它来安装所需的版本。来自 pipenv 自述文件:

Automatically install required Pythons, if pyenv is available.

使用 brew install pyenv(如果没有)安装 'pyenv' 包。

使用 pyenv install 3.6

安装 python 3.6

将新安装的 python 版本导出到 PATH

export PATH=${PYENV_PYTHON_VERSIONS_HOME}/3.6/bin

现在在 'Piplock' 中指定相同的版本。

[requires] python_version = "3.6"

最后,运行 pipenv install --dev.

在 MacOS 上,我也使用 pyenv 来管理 python 版本,类似于@pawamoy 的建议。

安装后我执行pipenv shell--python选项指向特定pyenv版本的目录。这将自动生成一个 Pipfilepython_version = "3.6".

⇒  pipenv --python /Users/<Your User>/.pyenv/versions/3.6.3/bin/python3.6 shell

在 MacOS(最新版本 BigSurMonterey)上,从 pyenv 下载 python 版本会在构建时引发一些错误。所以我建议直接从官方网站下载该版本 (3.6 in your case) 的 python 安装程序。然后使用命令

pipenv install --python 3.6

PS:不使用命令行可以为您节省一些时间和挫败感的罕见时刻之一。