I want to change the default python version I use, without affecting any other defaults. Appending the python path to the $PATH environment variable only accomplishes that as long as there is only the python executable at that given location, which is not the case if I want to use the /usr/bin version (export PATH=\usr\bin:$PATH).
I know I could theoretically create a symbolic link in some other folder of my choosing, but this is not very elegant.
Is there any other way of changing the default python (like a nice environment variable that python uses which takes precedence over the $PATH environment variable)?
3 Answers 3
If what you want is to get a specific version of the Python interpreter when you type python in your shell, there is no environment variable that can help you in that sense. It is the shell that decides the binary of the interpreter to use, and by the time it is spawned it cannot be swapped to a different one. Some ways that you can work around this:
- As you said, making a symlink somewhere. It does not need to be in a system directory, many users have something like
$HOME/binin their$PATH(some Linux distributions do this by default). - Make an alias (e.g. in your
.bashrcor similar). - Make a function (same as previous). You could even program it to receive some argument that decides which version of the interpreter you want to use, or to use the value of some environment variable, as you suggested.
- In distributions supporting it, set up an alternative.
- Make a virtual environment using the interpreter that you want and work within that environment.
In any case, there are a few things you should take into account:
- As mentioned in another answer, changing the version of the Python interpreter system-wide may break things. Particularly, switching the command
pythonfrom Python 2 to Python 3 (or the other way around) is almost guaranteed to cause quite a lot of trouble. Some of the solutions above may be unaffected by this (e.g. setting and alias or function in.bashrcshould not affect other scripts, unlesssourced), but you should be careful about it. - A common pitfall here is that changing the
pythoncommand does not change every Python-based command. For example, if you use IPython and just typeipythonyou need to make sure that the correct interpreter and script are launched. Depending on your context there may be a few tools that you may need to consider in that sense.
A virtual environment is probably the easiest and cleanest solution, since it was designed for that particular problem (even though issues still arise sometimes, e.g. if you run ipython but you forgot to install it in your environment first you will get the system-wide one, obviously), although if you want to use it for every shell session then it may not be as convenient.
Comments
Depending on what OS you're using, you can use alternatives to change the default.
https://wiki.debian.org/DebianAlternatives https://linuxconfig.org/how-to-switch-between-python-versions-on-fedora-linux
So, to change the default Python, you could do something like
alternatives --install /usr/bin/python python /usr/bin/python3.6 1
Be aware though, this can break a lot of stuff as the OS is likely dependent on the version of Python.
1 Comment
In general, Python itself does not provide any way to do this. Once Python starts up, there is already one particular version running, and it can't change to a different version on the fly.
Your operating system may provide some way of choosing which version of Python is the default (e.g. eselect python on Gentoo Linux), but it's impossible to say whether that's the case for you without knowing what OS you're using. If your OS doesn't provide something like this, it is possible to make your own scripts to set and change a default Python version.
which pythonand consequently which python version is used when I typepythonin the terminal, which is already accomplished by appending the path to the desired python version to the PATH environment variable.