Running Multiple Python Versions
Question:
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.
Answers:
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]
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?
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
Installing Multiple Interpreter Versions
node-build
is a common tool for installing multiple Node versions. python-build
is a common tool for installing multiple Python versions.
node-build
installation:
brew install node-build
python-build
installation:
git clone https://github.com/pyenv/pyenv.git
cd pyenv/plugins/python-build
./install.sh
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
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 Ruby version bound to the ruby
command depending on the version defined in the .ruby-version file of the current working directory or the nearest parent directory, the tools use different strategies (from the heaviest to the lightest):
rvm
updates the PATH
environment variable before each execution of the cd
command with a function redefining the cd
command;
rbenv
uses a proxy ruby
command (shim);
chruby
updates the PATH
environment variable before each execution of a shell command with 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 the man
command cannot find the manual page of the ruby
command anymore because of the shim indirection. 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
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
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
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
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.
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]
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?
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
Installing Multiple Interpreter Versions
node-build
is a common tool for installing multiple Node versions. python-build
is a common tool for installing multiple Python versions.
node-build
installation:
brew install node-build
python-build
installation:
git clone https://github.com/pyenv/pyenv.git
cd pyenv/plugins/python-build
./install.sh
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
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 Ruby version bound to the ruby
command depending on the version defined in the .ruby-version file of the current working directory or the nearest parent directory, the tools use different strategies (from the heaviest to the lightest):
rvm
updates thePATH
environment variable before each execution of thecd
command with a function redefining thecd
command;rbenv
uses a proxyruby
command (shim);chruby
updates thePATH
environment variable before each execution of a shell command with a function hooked into the shell withpreexec_functions
for Zsh andPROMPT_COMMAND
for Bash.
The drawback of the rvm
strategy is that it is invasive and slow. The drawback of the rbenv
strategy is that the man
command cannot find the manual page of the ruby
command anymore because of the shim indirection. 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
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
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
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