在 venv 中使用 python2.7,其中 python3 是默认 python [英] Use python2.7 in venv where python3 is default python

查看:72
本文介绍了在 venv 中使用 python2.7,其中 python3 是默认 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的机器上有 python2.7、python3.7、python3.6.我仍然不确定如何管理和查看所有三个 python 版本的位置.

I have python2.7, python3.7, python3.6 on my machine. I am still not sure how manage and see location of all three python version.

目前我只是输入带有版本名称的 python 来打开该终端或运行脚本.

Currently I just type python with version name to open that terminal or run script.

我通过 ~/.bashrc 中的别名默认使用了 python3.6.

I did python3.6 default by aliasing in ~/.bashrc.

我已经使用 python3.6 安装了 virtualenv,所以每当我默认创建 venc 时,它都会在其中使用 python3.6.

I have installed virtualenv using python3.6 so whenever I create venc by default it takes python3.6 in it.

用 python2.7 创建 venv 我试过 -

To create venv with python2.7 I tried -

virtualenv -p/usr/bin/python2.7/Users/karim/Documents/venv2.7

但是在这个 venv2.7 中,当我看到 python 版本时,它给出了 3.6.当我在终端中输入 python2.7 时,它会打开 python2.7.

But here also in this venv2.7 when I see python version it give 3.6. When I type python2.7 in terminal it opens python2.7.

我可以在虚拟环境中为 python2.7 做别名,但我怀疑这也可能在 venv 之外创建 python2.7 默认值.

I can do aliasing for python2.7 being in virtual environment but I doubt that may create python2.7 default outside venv as well.

  1. 如何使用默认的 python2.7 创建虚拟环境?
  2. 你能给我推荐一篇解释如何管理多个 python 版本、从一个版本切换到另一个版本的文章,看看那里的位置

我确实检查了所有 SO 线程,但没有文章帮助我在默认为 python3.6 的系统中使用 python2.7 创建 venv.

I really checked all SO threads but no articles helped me to create venv with python2.7 in system having python3.6 default.

推荐答案

我通过在 ~/.bashrc 中使用别名来默认使用 python3.6

I did python3.6 default by aliasing in ~/.bashrc

这就是问题的根源.如果我理解正确,您已经为 python 创建了别名,这会导致您的系统不会在 PATH 中搜索您的 Python 版本,而是使用您的别名.使用 virtualenv 时这种行为不会改变,因为激活它们会改变你的 PATH,但别名仍然会阻止使用其他 python 版本.

That is the source of your problem. If I understand correctly you have created and alias for python, which causes your system to not search your PATH for your python version, but instead use your alias. This behaviour does not change when using virtualenv, because activating them will alter your PATH, but the alias still prevents other python versions from being used.

要使python安装成为默认",您应该通过将所需的python版本添加到前面来更改PATH,而不是制作别名.

To make a python installation "default", you should alter your PATH by adding the desired python version to the front, not make aliases.

您可以使用命令

which python3   #python version 3x
which python27  #python version 2.7
which python    # "default" python

检查你的python安装位置,然后添加一行

to check the locations of your python installations, then add a line

export PATH="<Directory of desired python version>:$PATH"

使您想要的 Python 版本成为默认版本.

to make your desired python version default.

这篇关于在 venv 中使用 python2.7,其中 python3 是默认 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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