0

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?

I would like to avoid manually fiddling with symlinks and the PATH variable so that the solution is portable.

Since it is about switching python version, the solution would preferably not be written in python but rather in bash or something portable.

Ideally I would like to find something something similar to nvm for nodejs or rbenv for Ruby.

ccpizza
  • 28,968
  • 18
  • 162
  • 169
  • 2
    You would have to create the virtual env with `virtualenv -p /usr/bin/python2 env2` and `virtualenv -p /usr/bin/python3 env3`, and naturally they would be separate environments that you have to install dependencies for. – metatoaster Sep 30 '16 at 09:35
  • @metatoaster: this is an interesting possibility — so does it mean that e.g. `virtualenv` installed for Python 2 can be used to create a Python 3 environment (and viceversa)? Is it just the executable that is significant in this case? – ccpizza Sep 30 '16 at 13:14

4 Answers4

1

This shows us how to use update-alternatives and/or use an alias in ~./bashrc: alias python=/usr/local/bin/python2.7

flcoder
  • 713
  • 4
  • 14
  • 1
    This would definitely be the answer for Debian-based Linuces, but leaves out OSX and non-Debian Linux systems.. – ccpizza Sep 30 '16 at 13:18
  • A link is not an answer, add the relevant parts of the link to your answer. – Padraic Cunningham Oct 01 '16 at 09:37
  • The link was for extra information if you so choose to investigate more, i.e., we all can use some hand-holding sometimes. My answer was: use `update-alternatives` and/or use an alias in ~./bashrc, .e.g, `alias python=/usr/local/bin/python2.7` If that answer is not satisfactory for you, feel free to down vote it. If you have any questions about my answer, feel free to ask them. – flcoder Oct 01 '16 at 11:35
1

There is a way, and it is called Conda (you can install Miniconda to start with).

It lets you create virtual environments in which you can specify the Python interpreter version you want to use. 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.

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. So, for example, conda install numpy matplotlib will not require you to compile any of those packages.

Peque
  • 13,638
  • 11
  • 69
  • 105
  • An interesting approach but doesn't seem to be very lightweight. – ccpizza Sep 30 '16 at 13:21
  • @ccpizza You mean Conda or Miniconda? Miniconda is not heavy-weighted, and it allows you to have full control over the Python version you want to use in any virtual environment, even without the need to have that version installed in your system. – Peque Sep 30 '16 at 14:35
  • I looked at miniconda: it installs a separate python runtime. I am looking for a simple tool to switch between existing runtimes. – ccpizza Sep 30 '16 at 17:10
0

I guess you're talking about using Python under Windows because you mention the PATH variable. Recent versions of Python3 ship with the so-called Python launcher. You can run py -2 in order to start a Python2 interpreter and py -3 in order to start a Python3 interpreter. I hope this answers your question.

ThoWe
  • 346
  • 3
  • 13
  • 1
    Linux uses a $PATH environment variable too.. Windows, on the other hand does not have bash by default or symlinks, AFAIK, so my guess would be that the OP is on Linux. – flcoder Sep 30 '16 at 09:46
  • @flcoder: yes, I'm looking for a osx/linux solution, bash or c-based. – ccpizza Sep 30 '16 at 13:06
0

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.

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 e.g. 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 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.

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:

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
Community
  • 1
  • 1
ccpizza
  • 28,968
  • 18
  • 162
  • 169