Running Multiple Python Versions
Asked Answered
K

4

8

I want to run multiple Python versions in my box. Is there any version manager for Python where I can switch between multiple Python versions without having to call the full path of the Python binary? I have tried virtualenv and it seems to only cover problems running multiple Python library versions.

Thanks for your help.

Kowalewski answered 6/10, 2010 at 21:43 Comment(2)
Usually this is handled by your operating system shell. What are operating system and/or shell are you using?Paramnesia
Hi Daniel, I am using Ubuntu and bash.Kowalewski
C
7

I use virtualenv to keep track of different environments I need for my projects. I may setup django 1.0 in one environment or django 1.2 for another. You can use it to set which version of python you'd like to use in a particular environment as well. Here's the link to the site which has great samples and tutorials for how to get running: http://pypi.python.org/pypi/virtualenv

Chaps answered 6/10, 2010 at 23:4 Comment(0)
D
10

When calling python from bash you could try an alias.

user@machine:~$ alias python1234='/usr/bin/python2.5'
user@machine:~$ python1234
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Let's say you have a script called script.py with following content:

import sys
print sys.version

So, launching a script with a different version of python looks like:

user@machine:~$ python script.py 
2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3]
user@machine:~$ python1234 script.py 
2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
[GCC 4.3.3]
Donnie answered 6/10, 2010 at 22:13 Comment(2)
This might make sense for self-compiled pythons in weird locations such as /opt/my-python2.7/bin/python, but I don't see the point for Ubuntu-provided python2.5, which you can already call by a short name: 'python2.5'.Misgiving
The above case of self-compiled pythons in /opt/python2.7 was exactly what I needed this for, so it is very helpful for that.Shakespeare
C
7

I use virtualenv to keep track of different environments I need for my projects. I may setup django 1.0 in one environment or django 1.2 for another. You can use it to set which version of python you'd like to use in a particular environment as well. Here's the link to the site which has great samples and tutorials for how to get running: http://pypi.python.org/pypi/virtualenv

Chaps answered 6/10, 2010 at 23:4 Comment(0)
M
6

You don't have to use the full path.

user@machine:$ python2.5
Python 2.5.5 (r255:77872, Sep 14 2010, 17:16:34) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

user@machine:$ python2.6
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Does that answer your question?

Misgiving answered 6/10, 2010 at 22:35 Comment(1)
Also, if you wanted just to type in python to run your version of choice at the moment, all that python is, is a symbolic link to python2.5, python2.6, etc., so you could make a link to those binaries in /usr/bin.Rhee
F
-1

Installing Multiple Interpreter Versions

node-build is a common tool for installing multiple Node versions.

node-build installation:

brew install node-build

node-build usage:

node-build 14.20.1 ~/.nodes/node-14.20.1
node-build 16.17.1 ~/.nodes/node-16.17.1

python-build is a common tool for installing multiple Python versions.

python-build installation:

git clone https://github.com/pyenv/pyenv.git
cd pyenv/plugins/python-build
./install.sh

python-build usage:

python-build 3.8.10 ~/.pythons/python-3.8.10
python-build 3.9.13 ~/.pythons/python-3.9.13

Managing Multiple Interpreter Versions

rvm, rbenv, and chruby are common tools for managing multiple Ruby versions. They inspired nvm, nodenv, and chnode which are equivalent tools for managing multiple Node versions. They also inspired pyenv and chpython which are equivalent tools for managing multiple Python versions (there is no rvm equivalent as far as I know).

To automatically switch the targeted executables located in the bin directories of the installed Ruby versions when invoking a Ruby command (ruby, irb, rake, gem, …) based on the Ruby version defined in the .ruby-version file of the current or nearest parent directory, the tools use different strategies (from the heaviest to the lightest):

  • rvm updates the PATH environment variable before each invocation of the cd command by redefining the cd command;
  • rbenv invokes the Ruby command in a dedicated environment with an updated PATH environment variable by executing a proxy executable (shim);
  • chruby updates the PATH environment variable before each invocation of a command by executing a function hooked into the shell with preexec_functions for Zsh and PROMPT_COMMAND for Bash.

The drawback of the rvm strategy is that it is invasive and slow. The drawback of the rbenv strategy is that it is complex and requires shims to be regenerated (rehashed) at each installation of a Ruby version or Ruby package (gem) that provides an executable. The chruby strategy does not have those issues so chruby is the recommended tool.

chnode installation:

brew tap tkareine/chnode
brew install tkareine/chnode/chnode
echo "source /usr/local/opt/chnode/share/chnode/chnode.sh" >>~/.zshrc
echo "source /usr/local/opt/chnode/share/chnode/auto.sh" >>~/.zshrc
echo "precmd_functions+=(chnode_auto)" >>~/.zshrc

chnode usage:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#    node-14.20.1
#    node-16.17.1
chnode 16.17.1
echo $PATH
# /Users/me/.nodes/node-16.17.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#    node-14.20.1
#  * node-16.17.1
echo 14.20.1 >.node-version
echo $PATH
# /Users/me/.nodes/node-14.20.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chnode
#  * node-14.20.1
#    node-16.17.1

chpython installation:

git clone https://github.com/kisoku/chpython.git
cd chpython
make install
echo "source /usr/local/share/chpython/chpython.sh" >>~/.zshrc
echo "source /usr/local/share/chpython/auto.sh" >>~/.zshrc

chpython usage:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#    python-3.8.10
#    python-3.9.13
chpython 3.9.13
echo $PATH
# /Users/me/.pythons/python-3.9.13/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#    python-3.8.10
#  * python-3.9.13
echo 3.8.10 >.python-version
echo $PATH
# /Users/me/.pythons/python-3.8.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
chpython
#  * python-3.8.10
#    python-3.9.13
Floats answered 9/10, 2022 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.