如何更改已经存在的virtualenv的python版本? [英] How to change the python version of already existing virtualenv?

查看:1264
本文介绍了如何更改已经存在的virtualenv的python版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python 3.6创建了一个虚拟环境,然后进行了系统升级,并在系统范围内安装了python 3.7.现在,我无法在该虚拟环境中执行python文件,因为它正在搜索python 3.6.

I have created a virtual environment using python 3.6, then I've made a system upgrade and I've got python 3.7 installed system wide. Now I can't execute python files in that virtual environment because it's searching for python 3.6.

如何升级virtualenv python版本以匹配系统范围的版本,或者如何针对特定的虚拟环境降级python版本?

How can I upgrade the virtualenv python version to match the system wide version or how to downgrade the python version for that particular virtual environment?

我正在使用manjaro.

I'm using manjaro.

推荐答案

编辑1

进行了一些测试,发现了另一种优雅"的方式(至少)更新了可执行文件.假设虚拟环境最初是像这样创建的 virtualenv -p /path/to/my/python2.7 .venv.可执行文件可以更新为特定的python版本,例如:virtualenv --clear -p /path/to/my/python3.6 .venv.请验证.venv/bin/python中的python符号链接是否已使用ls -la .venv/bin/python更新.旧的可执行文件仍将在./venv/bin/中.

Did some testing and found another more "graceful" way to (at least) update the executable. Let's assume the virtual env was initially created like so virtualenv -p /path/to/my/python2.7 .venv. The executable can be updated to a specific python version like so: virtualenv --clear -p /path/to/my/python3.6 .venv. Please validate the python symlink in .venv/bin/python is updated using ls -la .venv/bin/python. The old executable(s) will still be in ./venv/bin/.

注意:您需要安装特定的python目标版本.

Note: You need to have the specific target version of python installed.

请参见此链接,其中对此进行了很好的解释.

See this link which explains it well.

Virtualenvwrapper附带了一些用于管理您的virtualenvs的便捷命令.

Virtualenvwrapper comes with some convenient commands for managing your virtualenvs.

要更改您的Python版本:

To change your Python version:

  1. 停用当前的环境会话.

  1. Deactivate your current environment session.

如果您安装了许多软件包或库,那将是一个很好的选择 制作Requirements.txt文件的想法.记住将版本编辑为

If you have many packages or libraries installed, it would be a good idea to make a requirements.txt file. Remember to edit version as necessary.

使用wrapper命令删除virtualenv:rmvirtualenv

Remove the virtualenv with the wrapper command: rmvirtualenv

  • 这将删除virtualenv,但保留您的项目文件.

使用所需的Python版本制作一个新的virtualenv.

Make a new virtualenv with the Python version you want.

  • 示例:mkvirtualenv -p python3环境名称

  • Example: mkvirtualenv -p python3 env-name

您可以使用-p标志和版本指定Python版本.如果 您有一个requirements.txt文件,可以使用-r来指定 requirements.txt

You can specify the Python version with the -p flag and version. If you have a requirements.txt file, you can specify that with -r requirements.txt

现在将新的virtualenv绑定到项目目录.你可以 指定完整路径,但是拥有新的virtualenv更容易 已激活并位于您的项目目录中.然后,运行命令:

Now bind your new virtualenv to your project directory. You can specify the full paths, but it is easier to have your new virtualenv activated and be in your project directory. Then, run the command:

示例:setvirtualenvproject

Example: setvirtualenvproject

请让我/我们知道此答案是否对您有所帮助!

Please let me/us know if this answer was helpful to you!

这篇关于如何更改已经存在的virtualenv的python版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆