Changing default python to another version - python

Currently when I use "python" command, it points to python2.6. I have installed python3.1 and I want the "python" command point to python3.1. How it is possible?
mahmood#mpc:~$ which python
/usr/bin/python
mahmood#mpc:~$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 2010-11-24 16:14 /usr/bin/python -> python2.6
mahmood#mpc:~$ uname -a
Linux orca 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux

Since you have Linux, and if you want to simply type "python" instead of "python3" in order to run Python programs, a solution is simply to define an alias in you shell configuration file (.bashrc, etc.). For Bourne shells, it should be something like
alias python=python3
(or whatever your Python 3 name is).
This way, you do not have to change anything on your system, so this solution should quite innocuous and it should not break your system.

You really don't want to change what python points to, because some programs might expect Python 2, and break.
The solution is to use virtualenv: create an isolated Python 3 environment (with the -p python3 option), activate it, and you're good to go.

unlink /usr/bin/python
ln -s /usr/bin/python3.1 /usr/bin/python

It is not advisable.
You could write at the top in your own script (a shebang):
#!/usr/bin/env python3
If you're on Windows then install pylauncher. It understands #!.
On Linux to make your script executable, run once:
$ chmod +x your-script
After that, to run your script:
$ ./your-script
For interactive use you could create virtualenv as #Petr Viktorin points out. To install/upgrade (versions from Ubuntu's repositries are too old):
$ pip install -U virtualenv{,wrapper}
Follow instructions in /path/to/virtualenvwrapper.sh, to create virtualenv that uses python3:
$ mkvirtualenv --python python3 py3
To activate virtualenv:
$ workon py3
In an active virtualenv python refers to /path/virtualenv/bin/python. So you could run:
$ python your_module.py

You could follow this procedure:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.1 /usr/bin/python
But as already stated by Petr Viktorin, any programs that would expect python v2 would stop to work. So use with caution. You can undo the change by running:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python2.6 /usr/bin/python

On Linux/Mac OS you can use python3 instead of python.

Related

create the python soft link fails

I have multiple Python versions, and thus also include multiple Python binary executables.
ls /usr/bin/python*
shows the python environments in my ubuntu 18.04
/usr/bin/python /usr/bin/python2.7 /usr/bin/python2-pbr /usr/bin/python3 /usr/bin/python3.5m /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/bin/python3m
/usr/bin/python2 /usr/bin/python2-jsonschema /usr/bin/python2-wsdump /usr/bin/python3.5 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3-config /usr/bin/python3m-config
in order to satisfy the PyFlink requirement regarding the Python environment version, i have to choose to soft link python to point to your python3 interpreter:
ln -s /usr/bin/python3 python
however when I use the command, it tells me
ln: failed to create symbolic link 'python': File exists
so i wonder do I need to delete the /usr/bin/python first and then use the command to create the soft link?
Command ln -s /usr/bin/python3 python creates link ./python pointing to /usr/bin/python3. You probably executed it multiple times so ./python already exists. You could overwrite it by providing -f flag to ln.
You definitely should not delete /usr/bin/python.
The whole idea with manually creating links to Python interpreter to install a package seems very weird. I suggest one of the following options:
just use the full path to the interpreter: /usr/bin/python3.6 -m pip install <package>; if the package adds any scripts globally usable from command line, like pyspark, they will be installed with hashbang pointing to the interpreter you installed them with
use a virtual environment: /usr/bin/python3.6 -m venv ~/.env-py36; source ~/.env-py36/bin/activate; python -m pip install <package>
use something like pyenv

How to revert back to python 2.7?

Whenever I ran python --version the result was 2.7 and 3.8 for python3 --version. I wanted to get 3.8 by running only python --version instead of python3 --version and after looking for the same on stackoverflow I did this
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
and while it did what I wanted but now a lot of things are going haywire. Therefore, I want my system to go back to the way it was before.
P.S. I wanted python to call python3 in the first place because whenever I would run mkvirtualenv name it would give me a warning that I am using python 2.7 and should switch to 3.8 but python 3.8 was already there on my computer and I thought maybe by doing the above mentioned I would get rid of the warning.
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python2 /usr/bin/python

How to set Python 3.8 as my default python version on kali linux

I installed python 3.8 in my kali linux OS, but when I type python in kali linux terminal it showing like this
> Python 2.7.18 (default, Apr 20 2020, 20:30:41)
How can I set Python 3.8 as my default?
Hope this answers your question.
The easiest way would be to run alias python=python3 .
So, now if you run the command python it will load python3 instead of python while being under the same alias/command python.
What I usually do is install non-default versions of programs under /usr/local. Then I configure the file $HOME/.bash_profile so that the $PATH variable has /usr/local/bin first, before /usr/bin and /bin (which might actually be the same physical directory on your system).
$HOME/.bash_profile:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# PATH starts with...
PATH="/usr/local/bin"
# add $HOME/bin if it exists...
if [ -d "${HOME}/bin" ]; then
PATH="${PATH}:${HOME}/bin"
fi
# These should exist, add them as fall back....
PATH="${PATH}:/bin:/usr/bin:/sbin:/usr/sbin"
# Some custom locations last
if [ -d /usr/local/arm-elf/bin ]; then
PATH="${PATH}:/usr/local/arm-elf/bin"
fi
if [ -d /opt/local/bin ]; then
PATH="${PATH}:/opt/local/bin"
fi
This has the effect of making anything located in /usr/local/bin the default when I am using an interactive shell, and letting the system defaults remain in effect otherwise.
first, check if you have python3.8 installed
use
ls /usr/bin/python*
then check for an alternative python version using
update-alternatives --list python
if the output of python3.8 is set to 2
To set the default python command to run python3.8
use this to create a persistent symlink
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
the 2 depends on your python alternatives
then
python --version
First remove python 2.7.18
Type >> sudo apt purge -y python2-minimal
install python latest version
Type >> sudo apt install -y python3-pip
latest version of python is installed!

HOWTO: Install python2.x within virtualenv? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python - It is possible to install another version of Python to Virtualenv?
Within my (virtualenv activated) env folder, it appears as though the python2.7 folder has symlinks to absolute paths for my system python installation.
This is not ideal.
Even when I install from a local path, and use virtualenv to set and environment in a folder env, the paths to Python are absolute, and a dependency on the OS environment is established. I want to rid this dependency and make the python interpreter, as well as all software relying upon it, completely independent.
Let's imagine that I want python2.6 to be included IN the env tree as a STAND-ALONE installation without symlinks to my system folders.
How does one accomplish this feat of extraordinary non-linkage?
$ > pwd
/Users/foo/development/v1/bar/env
(env)
$ > ls -l lib/python2.7/
total 920
lrwxr-xr-x 1 foo staff 82 Oct 15 16:48 UserDict.py -> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py
...
lrwxr-xr-x 1 foo staff 85 Oct 15 16:48 _weakrefset.py -> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py
Thank you for your help.
EDIT: Moreover, it will be most ideal to have the virtualenv (including the local python install) relocatable.
UPDATE: Please ALSO refer to Is it possible to install another version of Python to Virtualenv?
Big thanks to #millimoose, et al.
Here's what I ended up doing, very specifically. I will update if I encounter problems in the future.
Set up environment folders.
$ mkdir env
$ mkdir pyenv
$ mkdir dep
Get Python-2.7.3, and virtualenv without any form of root OS installation.
$ cd dep
$ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
Extract and install Python-2.7.3 into the pyenv dir. make clean is optional if you are doing this a 2nd, 3rd, Nth time...
$ tar -xzvf Python-2.7.3.tgz
$ cd Python-2.7.3
$ make clean
$ ./configure --prefix=/path/to/pyenv
$ make && make install
$ cd ../../
$ ls
dep env pyenv
Create your virtualenv
$ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
Fix the symlink to python2.7 within env/include/
$ ls -l env/include/
$ cd !$
$ rm python2.7
$ ln -s ../../pyenv/include/python2.7 python2.7
$ cd ../../
Fix the remaining python symlinks in env. You'll have to delete the symbolically linked directories and recreate them, as above. Also, here's the syntax to force in-place symbolic link creation.
$ ls -l env/lib/python2.7/
$ cd !$
$ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
[...repeat until all symbolic links are relative...]
$ cd ../../../
Test
$ python --version
Python 2.7.1
$ source env/bin/activate
(env)
$ python --version
Python 2.7.3
Aloha.
Use pythonbrew to get an isolated Python installation.
Use that Python installation to initiate your virtual environment.
Make the virtual environment relocatable.

How to choose python version run when typing only "python" at prompt?

I have just installed Python2.6 on a Red Hat linux server which was supplied with python2.4 preinstaleld.
When I type python, python2.4 is launched, typing python2.6 launches python 2.6 correctly.
What is the correct way to make 2.6 the default? Also how can this result be possible:
$ python -V && which python && pwd && ./python -V
Python 2.4.3
/usr/local/bin/python
/usr/local/bin
Python 2.6.6
It's a symlink actually.
$ ls -al `which python`
lrwxrwxrwx 1 root root 9 2010-04-21 12:08 /usr/bin/python -> python2.6
So changing the default version from 2.4 to 2.6 would be just:
cd /usr/bin
sudo ln -sf `which python2.6` python
The easiest and quickest way, is to prepend the path to the 2.6.6 version to your PATH variable for your shell. This would go into your .bashrc or .profile, etc.
Assuming this path is /opt/foo/bin it would be:
export PATH=/opt/foo/bin:$PATH
If you don't include the possible side effects of things in the new path, this is the quick win.
This really depends on if you need this to be system-wide.

Categories

Resources