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]
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
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?
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
Related
Yesterday, I did put my laptop on upgrade 19.10 to 20.04 but due to power failure, that became a partial-upgrade, the system broked. I resolved everything but my Django app wasn't running due to PYTHONPPATH so I tried uninstalling python3 and everything got broken. I re-installed that again.
Now when I do python --version I got
bash: python: command not found
whereas python3 --version gives correct answer.
Python 3.8.2
I have python2.7 and python3 both installed. So for now, my Python is not working and also I think I've messed up my PYTHONPATH and I really don't know what I'm going to do now.
My ./~bashrc file looks like below :
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH
# Install Ruby Gems to ~/gems
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"
I'm using Ubuntu 20.04.
Please specify how are you running your project and what exactly is the issue you are facing. May be you can paste the error message you get.
For python command,
In Linux, generally the base commands (like python) without version in it, would actually be pointing the specific (python) version executable through symbolic links (or simply links).
[foo#linuxbox ~]$ ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 16 Feb 9 16:26 /usr/bin/python -> /usr/bin/python3
These links can be created or even edited to our need to point to the version we need. Use the below command to link python to python3. This is equivalent to setting alias for python3 as python but bit more than that as all users/process can run python but in case of alias the tool/user must be running from bash or corresponding shell where alias was created.
sudo ln -f -s /usr/bin/python3 /usr/bin/python
I feel in Ubuntu 20 you have to run command python2 to go into 2.7.* interpreter. python and python3 command both refers to Python3. But anyway your python command should work.
#ideapad:~$ python
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ideapad:~$ python2
Python 2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ideapad:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
To solve your issue, use an alias. Place command alias python=python3 into ~/.bashrc file, after adding this run source ~/.bashrc.
Other solutions:
run command which python it will reveal the location of installed Python and then try adding the location given by which python command to PYTHONPATH
Reinstall your python - sudo apt install python
I just created my first AWS EC2 instance. I used sudo yum install python3 -y to install Python3 but when I check the version via python --version it says Python 2.7.16. How do I switch versions?
You can either invoke python 3 with python3 directly from the terminal, or create an alias by adding the following line to your ~/.bashrc or ~/.bash_aliases file:
alias python=python3
More details and troubleshooting tips available in this related question.
There are a couple of ways to do this:
You could explicitly request python3, invoking it as-is, instead of just python, i.e.:
$ python3
Python 3.7.6 (default, Feb 26 2020, 20:54:15)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
As per #Nick Walsh's answer, you can create a shell alias(1) that just expands python to python3 by putting the following either into .profile, .bashrc, or even .bash_aliases:
alias python=python3
Granted python3 is in your PATH, this will work without a hitch, with the added benefit that this is a per user setting, meaning you won't be changing the system-wide python interpreter (since python remains pointing to /usr/bin/python2). If you'd like, you can opt for a system-wide alias as well by modifying /etc/profile or /etc/bashrc, adding the alias there.
You could replace the python symlink, linking it to python3 instead.
You can achieve this using ln(1) (pay close attention to the # vs. $ prompt, meaning you require root privileges to issue this command. Using sudo will suffice):
# ln -sf /usr/bin/python{3,}
I'm leveraging bash's string expansion features to avoid repetition. The command effectively expands to:
# ln -sf /usr/bin/python3 /usr/bin/python
This is probably recommended for the sake of portability (when it comes to scripting).
The latter alternative might work up until python gets updated, replacing the default python interpreter again with python2. #kichik pointed out the use of alternatives(8) to adequately (and truly persistently) configure your python interpreter.
As per this answer, you can issue the following commands to install and configure your default python interpreter:
# alternatives --install /usr/bin/python python /usr/bin/python2 50
# alternatives --install /usr/bin/python python /usr/bin/python3 60
# alternatives --config python
There are 2 programs which provide 'python'.
Selection Command
-----------------------------------------------
1 /usr/bin/python2
*+ 2 /usr/bin/python3
Enter to keep the current selection[+], or type selection number: 2
$ alternatives --display python
python - status is manual.
link currently points to /usr/bin/python3
/usr/bin/python2 - priority 50
/usr/bin/python3 - priority 60
Current `best' version is /usr/bin/python3.
$ python
Python 3.7.6 (default, Feb 26 2020, 20:54:15)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
I'm doing a learn python the hardway tutorial, and they are using python2.7
I got it downloaded but unable to switch back from 3.3 to 2.7
I manipulated PATH variable, adding C:\Python27 but this was no use
any other suggestion?
Rename the python interpreter executables to their respective versions. The OS is just executing the first 'python' executable it finds in the path, which is probably the 3.x version. So in command line, you can type python2 or python3 to select the version of interpreter you want.
Another option is.
you can create virtual environment for python 2.7 version.
And Activate the environment.
And use your virtual env for your python 2.7 learning.
username#mypc:~/dev/learn-code$ virtualenv myenv -p /usr/bin/python
Already using interpreter /usr/bin/python
New python executable in /home/username/dev/learn-code/myenv/bin/python
Installing setuptools, pip, wheel...done.
username#mypc:~/dev/learn-code$
username#mypc:~/dev/learn-code$
username#mypc:~/dev/learn-code$ source myenv/bin/activate
(myenv) username#mypc:~/dev/learn-code$
(myenv) username#mypc:~/dev/learn-code$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>>
Setting up in windows
environment also similar. see this link
I run into this issue ImportError numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS2_AsASCIIString installing Python in a pyenv-virtualenv environment.
In my case, it happens with the matplotlib package instead of numpy (as in the above question), but it's basically the same issue.
The answer given in that question is a simple:
Rebuild NumPy against a Python built as UCS-4.
I don't know how to do this. In this other question it is said that one has to use:
./configure --enable-unicode=ucs4
but I don't know how to use that command along with pyenv.
This issue is also mentioned in pyenv's repo issue list, and a solution given in a comment. Sadly (for me) I can not understand how to apply the fix explained in said comment.
So my question basically is: how do I build Python as UCS-4 via pyenv?
Installing python with pyenv with ucs2:
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2
$ pyenv install -v 2.7.11
...
$ pyenv local 2.7.11
$ pyenv versions
system
* 2.7.11 (set by /home/nwani/.python-version)
$ /home/nwani/.pyenv/shims/python
Python 2.7.11 (default, Aug 13 2016, 13:42:13)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_vars()['CONFIG_ARGS']
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs2' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"
Installing python with pyenv with ucs4:
$ pyenv uninstall 2.7.11
pyenv: remove /home/nwani/.pyenv/versions/2.7.11? y
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs4
$ pyenv install -v 2.7.11
...
$ pyenv local 2.7.11
$ pyenv versions
system
* 2.7.11 (set by /home/nwani/.python-version)
$ /home/nwani/.pyenv/shims/python
Python 2.7.11 (default, Aug 13 2016, 13:49:09)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_vars()['CONFIG_ARGS']
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs4' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"
I have a Linux drive with no root access. It has Python 2.4.2 installed in /usr/bin/python directory.
which python
/usr/bin/python
I installed Python 2.7.8 in my local folder:
cd /usr2/steve/bin
ls
python2.7
If I still run Python it starts older version:
python
Python 2.4.2 (#1, May 6 2011, 13:26:21)
[GCC 4.1.2 20070115 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I have added /usr2/steve/bin to the PATH:
echo $PATH
/usr2/steve/usr2/steve/bin:/usr2/steve/local/mnt/workspace/steve/Python-2.7.8:/usr2/steve/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
How do I force the system to use the newer Python version as opposed to the older one?
You only have a python2.7 binary in your /usr2/steve/bin folder. You can make a symlink, then just calling python should work:
cd /usr2/steve/bin
ln -s python2.7 python
make your life simple by creating an alias. Are you using BASH (hopefully you are)? If so this is the syntax
alias pythonsteve='/usr2/steve/bin/python2.7'
(drop the = sign if you are running CSH). Next, run to verify
$pythonsteve -c 'import sys; print sys.prefix'
/usr2/steve/bin
Put the alias command in your shell user start-up file. On typical BASH installs this is the file ~/.bashrc. Note, when you manually edit the ~/.bashrc file you must either log out/log in, or run source ~/.bashrc for the commands to execute.
If you want to use python 2.7 you can specify it by appending it after 'python'
which python2.7