简体   繁体   English

在python2和python3之间切换为默认python

[英]Switching between python2 and python3 as the default python

Is there a standard way to switch between python2 to python3 as the default python, similar to how virtualenv can be used to switch between different sandboxed python environments? 是否有标准方法可以在python2和python3之间切换为默认python,类似于virtualenv可用于在不同的沙盒python环境之间进行切换的方法?

I would like to avoid manually fiddling with symlinks and the PATH variable so that the solution is portable. 我想避免手动摆弄符号链接和PATH变量,以便解决方案可移植。

Since it is about switching python version, the solution would preferably not be written in python but rather in bash or something portable. 由于它是关于切换python版本的,因此该解决方案最好不是用python编写,而是用bash或可移植的某种形式编写。

Ideally I would like to find something something similar to nvm for nodejs or rbenv for Ruby. 理想情况下,我想找到类似的东西的东西NVM的或的NodeJS rbenv为Ruby。

向我们展示了如何使用update-alternatives和/或在~./bashrc使用别名: alias python=/usr/local/bin/python2.7

There is a way, and it is called Conda (you can install Miniconda to start with). 有一种方法,它称为Conda (您可以先安装Miniconda )。

It lets you create virtual environments in which you can specify the Python interpreter version you want to use. 它使您可以创建虚拟环境,在其中可以指定要使用的Python解释器版本。 In example: 例如:

conda create -n new_environment python=3.5

Conda will download the interpreter for you, so you don't need to have it available in your system. Conda将为您下载解释器,因此您无需在系统中使用它。

Appart from that, you can install packages without needing to compile them (in case they are not fully written in Python), which is something very convenient specially if you are on Windows. Appart表示,您可以安装软件包而无需编译它们(以防它们不是完全用Python编写的),这在使用Windows时特别方便。 So, for example, conda install numpy matplotlib will not require you to compile any of those packages. 因此,例如, conda install numpy matplotlib将不需要您编译任何这些软件包。

I guess you're talking about using Python under Windows because you mention the PATH variable. 我猜您在谈论在Windows下使用Python,因为您提到了PATH变量。 Recent versions of Python3 ship with the so-called Python launcher. 最新版本的Python3附带了所谓的Python启动器。 You can run py -2 in order to start a Python2 interpreter and py -3 in order to start a Python3 interpreter. 您可以运行py -2以启动Python2解释器,并运行py -3以启动Python3解释器。 I hope this answers your question. 我希望这回答了你的问题。

After some more research it looks like a possible solution could have been pyenv with the usage described in the pyenv tutorial but it only recognizes a single system-wide python runtime (whichever is the default at the moment), and doesn't provide the option to switch between the system-wide python2 and python3. 经过一些调查研究,它看起来像一个可能的解决方案可能已pyenv与所描述的使用pyenv教程 ,但它只能识别一个全系统的Python运行时(无论是目前默认值), 并且不提供选项在系统范围的python2和python3之间切换。

Looks like pyenv can switch only between the system python and any of the versions explicitly installed via pyenv which can all be seen via pyenv install --list and installed with eg pyenv install 3.5.2 . 看起来pyenv只能在系统python和通过pyenv显式安装的任何版本之间切换,所有这些都可以通过pyenv install --list看到,并与pyenv install 3.5.2 In other words, the python3 must be installed via pyenv in order to be able to switch between 2 and 3. 换句话说,必须通过pyenv安装python3才能在2和3之间切换。

Pyenv can integrate with virtualenv which could be handy for dev testing since it includes all versions of anaconda, miniconda, pypy, jython, stackless, etc. It's probably the easiest way of installing multiple versions of python which do not come with your package manager, ie on older Linux distros that don't have a modern python in their repos. Pyenv可以与virtualenv集成,因为它包含anaconda,miniconda,pypy,jython,stackless等所有版本,因此可以方便进行开发测试。这可能是安装软件包管理器未附带的多个python版本的最简单方法,即在较旧的Linux发行版中,其发行版中没有现代python。

But in the long run, all things considered, I found that the solution proposed by metatoaster is simpler and totally meets my requirements since I can use the python2 virtualenv to create both python2 and python3 environments without any overhead: 但是从长远来看,考虑到所有因素,我发现metatoaster提出的解决方案更简单并且完全符合我的要求,因为我可以使用python2 virtualenv来创建python2和python3环境,而没有任何开销:

python -V
Python 2.7.12
mkdir -p ~/.virtualenvs && cd ~/.virtualenvs
virtualenv -p /usr/bin/python3 mypy3env
workon mypy3env
python -V
>>> Python 3.5.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM