在我的 MacBook Pro 上,我默认使用 Python 3.6,但我从另一个项目接管的项目需要 2.7,我通过 Anaconda 安装了它。我使用pipenv install将版本设置为 3.6的 Pipenv 进行设置。然后我尝试通过以下方式更改版本:
pipenv --python 2.7
Run Code Online (Sandbox Code Playgroud)
但它返回了这个警告:
Warning: Your Pipfile requires python_version 3.6, but you are using 2.7.15 (/Users/j/.local/share/v/Z/bin/python).
Run Code Online (Sandbox Code Playgroud)
然后当然pipenv check失败了,并返回:
Specifier python_version does not match 3.6 (2.7).
Run Code Online (Sandbox Code Playgroud)
然后我尝试了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
Run Code Online (Sandbox Code Playgroud)
这是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
Run Code Online (Sandbox Code Playgroud)
请建议我如何使用 Pipenv 将这个特定项目的 python 从 3.6 切换到 2.7?
祝一切顺利,
嘉骏
由于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>
Run Code Online (Sandbox Code Playgroud)
我希望这能解决您的问题