首页 > 解决方案 > 无法在 Pipenv 上切换 python 版本

问题描述

在我的 MacBook Pro 上,我默认使用 Python 3.6,但我从另一个项目接管的项目需要 2.7,我通过 Anaconda 安装了它。pipenv install我使用将版本设置为 3.6 来设置Pipenv 。然后我尝试通过以下方式更改版本:

pipenv --python 2.7

但它返回了这个警告:

Warning: Your Pipfile requires python_version 3.6, but you are using 2.7.15 (/Users/j/.local/share/v/Z/bin/python).

然后当然pipenv check失败了,然后返回:

Specifier python_version does not match 3.6 (2.7).

然后我尝试pipenv install python 2.7.15了,也失败了。Pipfile 保持 3.6 不变。

Error:  An error occurred while installing 2.7.15!
  Could not find a version that satisfies the requirement 2.7.15 (from versions: )
No matching distribution found for 2.7.15

这是python版本ls -ls /usr/bin/python*

32 -rwxr-xr-x  1 root  wheel  66880 24 Oct 12:47 /usr/bin/python
 0 -rwxr-xr-x  4 root  wheel    925 18 Aug 02:45 /usr/bin/python-config
 0 lrwxr-xr-x  1 root  wheel     75  8 Oct 21:45 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
 0 lrwxr-xr-x  1 root  wheel     82  8 Oct 21:45 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
32 -rwxr-xr-x  1 root  wheel  66880 24 Oct 12:47 /usr/bin/pythonw
 0 lrwxr-xr-x  1 root  wheel     76  8 Oct 21:45 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

请建议我如何使用 Pipenv 将这个特定项目的 python 从 3.6 切换到 2.7?

一切顺利,

家军

标签: pythonpipenv

解决方案


pipenv install python 2.7.15由于多种原因,您尝试的命令是错误的。

首先,命令的格式pipenv如下

pipenv install <package> <package>...

因此,当您运行时,pipenv install python 2.7.15您只是尝试安装分别称为pythonand的两个软件包2.7.15,这显然不是您想要做的。

即使您使用了错误的正确语法pipenv install python==2.7.15,因为您将在另一个安装了 python 3.6 的 python 环境中安装 python 2.7.15(您的笔记本电脑上的系统版本)。

如果您想在同一环境中安装多个版本的 Python(作为您的笔记本电脑读取)并且不弄乱系统版本,您应该使用“pyenv”(https://github.com/pyenv/pyenv)之类的东西. Pyenv 与pipenv.

您将能够使用此命令安装 Python 2.7.15

pyenv install 2.7.15

如您所见,这与您已经尝试过的命令不同pipenv install python 2.7.15

此外,由于您的 Pipfile 存在问题,我建议将该文件与 Pipfile.lock 一起移动到另一个目录中(出于备份目的),并从一个空目录开始。

同样如这里建议的那样https://pipenv.readthedocs.io/en/latest/最好在根文件夹中创建一个空文件夹“.venv”,将安装来自该虚拟环境的所有 python 依赖项。

所以要运行的正确命令列表是

pyenv install 2.7.15
mkdir .venv
pipenv --python 2.7.15
pipenv install <package>

我希望这能解决你的问题


推荐阅读