使用pipenv创建虚拟环境时设置Python版本

65

使用Debian 4.14.34-v7+上的Raspberry Pi,我试图用Python 3.6.5作为默认版本来设置pipenv。我首先通过在Pi上编译它(需要几个小时...)安装Python3.6。在创建一个名为“robot”的目录后,我使用sudo pip3 install pipenvpipenv install --three安装pipenv

然后我启动shell并打开Python,得到的是Python 3.5.3…

pi@raspberrypi:~/robot $ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.
. /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/activate
pi@raspberrypi:~/robot $ . /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/activate
(robot-XZ3Md9g0) pi@raspberrypi:~/robot $ python
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

我使用pipenv --python 3.6切换了虚拟环境,但是尽管它正确(表面上)安装了,但我立即收到警告,指出pipenv仍然期望Python 3.5…

(robot-XZ3Md9g0) pi@raspberrypi:~/robot $ pipenv --python 3.6
Virtualenv already exists!
Remove existing virtualenv? [Y/n]: y
Removing existing virtualenv…
Creating a virtualenv for this project…
Using /usr/local/bin/python3.6m (3.6.5) to create virtualenv…
⠋Running virtualenv with interpreter /usr/local/bin/python3.6m
Using base prefix '/usr/local'
New python executable in /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/python3.6m
Also creating executable in /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0
Warning: Your Pipfile requires python_version 3.5, but you are using 3.6.5 (/home/pi/.local/share/v/r/bin/python).
  $ pipenv check will surely fail.
(robot-XZ3Md9g0) pi@raspberrypi:~/robot $ python
Python 3.6.5 (default, May  3 2018, 11:25:17) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

当我第一次创建虚拟环境时,如何设置pipenv以寻找Python 3.6?我可以手动编辑Pipfile,但这似乎有违让pipenv为我处理事情的目的。

6个回答

112

如果要更改现有环境的Python版本,则“编辑Pipfile”是正确的方法。

如果您想使用Python 3.6创建一个的环境,可以运行:

pipenv install --python 3.6

而不是

pipenv install --three

那就应该可以解决问题了。

只要确定如果你创建了新的环境,要删除旧的Pipfile文件,否则命令将会失败。


1
然后只需忽略警告 Your Pipfile requires python_version 3.5, but you are using X (...) pipenv check will surely fail. - Tomasz Gandor
如果我们使用"--python 3.6",那就必须已经在本地安装了Python 3.6...难道我们不能在本地安装Python 3.11,并在pipenv中安装一个Python 3.6吗? - undefined

20

在设置pipenv时,您可以使用以下命令指定Python版本:pipenv --python 3.6,以使用Python3.6。

通过编辑Pipfile,也可以更改以下内容来指定特定的Python版本:

[requires] python_version = "3.6.6"

如果您执行了上述所有操作,但在pipenv shell中仍无法访问所需的正确Python版本,则可能需要检查您的.bashrc文件,并确保您没有针对特定Python版本的alias


这需要编辑Pipfile,可能会有版本控制等。python_version不接受范围或多个版本(可以按优先顺序指定)。考虑使用poetry代替pipenv以获得更好的Python版本支持。 - Tomasz Gandor

13

如果您想为项目创建虚拟环境,上述答案可能不适用于您。例如,如果您安装了多个版本的Python并且必须像下面的示例中一样重命名可执行文件,或者对于同一项目,您可能希望创建使用不同Python版本的虚拟环境。

在为项目创建虚拟环境时,您可以更加具体并指定Python可执行文件的路径。

请记得相对于项目文件夹运行以下命令。

pipenv install --python 'C:\Python373\python373.exe'

2
同样适用于Ubuntu:pipenv install --python '/usr/bin/python3.7',谢谢。 - C.K.

3
我建议你两种方法:
  1. pip install --python 3.7 <你想要替换的版本号>

(这需要安装asdf或pyenv。如果你没有安装它们,请继续阅读!)

  1. 手动方法。(适用于Linux)(对于Windows,请参考@Ryner的答案!)
  • Download the python gzip from python.org and extract it to a folder, cd to it.

  • Hit the following

     ./configure
    
     make 
    
     sudo make install
    
  • Now you have python version locally installed, all you gotta do is step one.

pipenv install --python <version-u-installed>

注意:如果需要卸载,进入执行make的目录,然后简单地输入make uninstall

希望这有所帮助!愉快编程!


2

针对现有环境,我无法弄清如何更改Python的版本。我尝试更改Pipfile并指定替代版本的Python路径。

相反,我首先使用pipenv --rm删除了虚拟环境,然后使用pipenv --python 3.10重新开始。


0

以上解决方案只有在我卸载了mysql-connector和mysql-connector-python并重新安装mysql-connector-python后才对我起作用。

- pip uninstall mysql-connector
- pip uninstall mysql-connector-python
- pip install mysql-connector-python

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接