Linux set default python version

Two versions of python on linux. how to make 2.7 the default

@User: Yeah, I broke an ancient Mandrake similarly (changing /usr/bin/env python to mean 2.6 instead of 2.3 meant half of the commands in rpm and all of urpmi stopped working).

I have followed the following step to install Django on Centos 5:1st, install Python 3.6 from source code. 2nd: in shell type the following command «alias python=/usr/local/bin/python3.6» 3rd: run following command to install Django «pip3 install Django» 4th: «python -m django —version» to verify the Django installed with version «1.10.5»

7 Answers 7

You probably don’t actually want to change your default Python.

Your distro installed a standard system Python in /usr/bin , and may have scripts that depend on this being present, and selected by #! /usr/bin/env python . You can usually get away with running Python 2.6 scripts in 2.7, but do you want to risk it?

On top of that, monkeying with /usr/bin can break your package manager’s ability to manage packages. And changing the order of directories in your PATH will affect a lot of other things besides Python. (In fact, it’s more common to have /usr/local/bin ahead of /usr/bin , and it may be what you actually want—but if you have it the other way around, presumably there’s a good reason for that.)

But you don’t need to change your default Python to get the system to run 2.7 when you type python .

First, you can set up a shell alias:

alias python=/usr/local/bin/python2.7 

Type that at a prompt, or put it in your ~/.bashrc if you want the change to be persistent, and now when you type python it runs your chosen 2.7, but when some program on your system tries to run a script with /usr/bin/env python it runs the standard 2.6.

Alternatively, just create a virtual environment out of your 2.7 (or separate venvs for different projects), and do your work inside the venv.

Читайте также:  Synology vmm guest tool linux

Источник

How can I change the default python on my Ubuntu 20.04 to Python3.8?

Complains it cannot find /usr/bin/python3.8, buuuuut: gt@gt-ThinkPad-X230:~$ ls /usr/bin/python* /usr/bin/python /usr/bin/python3.8 /usr/bin/python3-pasteurize /usr/bin/python2 /usr/bin/python3.8-config /usr/bin/python3-unidiff /usr/bin/python2.7 /usr/bin/python3-config /usr/bin/python3 /usr/bin/python3-futurize How do I get bash to find see /usr/bin/python3.8?

4 Answers 4

The correct way is sudo apt install python-is-python3 — it effectively does a symlink, but it also keeps pace with future updates; so if your ubuntu distribution moves to say Python 3.9, the manual symlink will no longer work, but the package makes sure it is still valid.

I did sudo apt install python-is-python3 , but nothing changed. What are the next steps? No man entry either

@Abdull inspect your environment. As I wrote, it does not do much more then install a symlink, so perhaps your PATH is messed up, or the binary that the symlink points to has been changed, there can be bunch of other problems. If you are at your wits end, I suggest you open a new question.

Firstly to answer your question, your approach should work, I think the path you’ve given in your alias needs the / preceding the path so the command should be alias python=’/usr/bin/python3.8′ , this would indeed need to go into your ~/.bashrc file assuming you are using bash.

Secondly, Ubuntu has a really nice method of setting default binaries globally rather than messing with dot config files as depicted here: update-alternatives

a better solution may be to simply run:

sudo update-alternatives --set python /usr/bin/python3.8 

This will ensure you have the version of python in use that you intend, everywhere.

I just tried this (in my case setting ‘python’ to ‘/usr/bin/python3’ on ubuntu20.04, but got the following error: ‘update-alternatives: error: no alternatives for python’. Any idea what I’m doing wrong?

@dagmarPrime update-alternatives is exactly what you need, as your aliases won’t get called in scripts. python can and should point to python 3 for most purposes these days and update-alternatives is the right way to do that.

@MaxPower You’re correct; this does not work on Ubuntu 20.04 LTS, for the reason you describe. But sudo apt install python-is-python3 does, per another answer, askubuntu.com/a/1272899/379076 .

Читайте также:  Astra linux слетел пароль

After updating from 16 Xenial to 20.04.3 (via 18) alternatives seems to be a little wonky. alternatives says the symlink is pointing to python3.9 but it isn’t. update-alternatives —display python3 link currently points to /usr/bin/python3.9 link python3 is /usr/bin/python3 But. ls -l /usr/bin/python3 gives lrwxrwxrwx 1 root root 9 Mar 13 2020 /usr/bin/python3 -> python3.8 . ie pointing at 3.8. update-alternatives —set python3 /usr/bin/python3.9 shows update-alternatives: warning: forcing reinstallation . because link group python3 is broken .

Источник

How to set default python3 to python 3.9 instead of python 3.8 in Ubuntu 20.04 LTS

I have installed Python 3.9 in the Ubuntu 20.04 LTS. Now the system has both Python 3.8 and Python 3.9.

# which python # which python3 /usr/bin/python3 # which python3.8 /usr/bin/python3.8 # which python3.9 /usr/bin/python3.9 # ls -alith /usr/bin/python3 12583916 lrwxrwxrwx 1 root root 9 Jul 19 2021 /usr/bin/python3 -> python3.8 
# pip3 install --upgrade --find-links file:///path/to/directory

I want to change that default pip3 behavior by updating the symbolic link /usr/bin/python3 to /usr/bin/python3.9. How to do that?

# update-alternatives --set python3 /usr/bin/python3.9 This command will not work as expected. 
# which pip3 /usr/bin/pip3 # ls -alith /usr/bin/pip3 12589712 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3 # pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8) # 
# alias python3=python3.9 # ls -alith /usr/bin/python3 12583916 lrwxrwxrwx 1 root root 9 Jul 19 2021 /usr/bin/python3 -> python3.8 

Thank you. I have added the pip3 info in the post. But I still don’t know how to do that, can you help?

The «python3.9 -m pip install . » will have problem when I added the pip3 option «—find-links file:///path/to/directory», it won’t recognize the /path/to/directory at all. But if I use the above «pip3 install .. -find-links file:///path/to/directory», then the pyhton 3.8 can recognize it correctly. So I need to change the default python 3.8 version to python 3.9. and then use the pip3 as usually.’

Gotcha. The problem is that you also need to change the pip executable symlink as I understand you’ve done for the python executable.

2 Answers 2

You should be able to use python3.9 -m pip install to run pip with a specific python version, in this case 3.9.

If you want python3 to point to python3.9 you could use the quick and dirty.

Читайте также:  Opening jar files in linux

Tried to recreate your problem,

# which python3 /usr/bin/python3 # python3 --version Python 3.8.10 # which python3.8 /usr/bin/python3.8 # which python3.9 /usr/bin/python3.9 

Then update the alternatives, and set new priority:

# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 # sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2 # sudo update-alternatives --config python3 There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.9 2 auto mode 1 /usr/bin/python3.8 2 manual mode * 2 /usr/bin/python3.9 2 manual mode Press to keep the current choice[*], or type selection number: 0 
# ls -alith /usr/bin/python3 3338 lrwxrwxrwx 1 root root 25 Feb 8 14:33 /usr/bin/python3 -> /etc/alternatives/python3 # python3 -V Python 3.9.5 # ls -alith /usr/bin/pip3 48482 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3 # pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9) 

Hope this helps (tried it in wsl2 Ubuntu 20.04 LTS)

Источник

How do I change my default python 3 having more than one version installed (Ubuntu 18.04)? [duplicate]

When I type python —version in my terminal, it shows Python 3.8.5, but when I type python3 —version , it shows Python 3.6.9. I want to create a virtual environment using python3 -m venv .venv with the version 3.8.5, but because of the fact that my default python3 version is the 3.6.9, it’s creating a virtual environment using 3.6.9 as it’s version. How do I change my default python3 version?

2 Answers 2

The python and python3 commands are usually soft links to the actual executables and you can change the targets. For example:

Firstly, find out where python 3.6 and python 3.8 are located:

# which python /usr/bin/python # ls -l /usr/bin/python /usr/bin/python -> python3.8 # which python3 /usr/bin/python3 # ls -l /usr/bin/python3 /usr/bin/python3 -> python3.6 

Then, change the soft links:

# rm /usr/bin/python3 # ln -s /usr/bin/python3.8 /usr/bin/python3 

Run this Command in your terminal

sudo update-alternatives --config python 

You’ll get choice prompt, enter selection number of your desired python version.

But if this shows an error like this: update-alternatives: error: no alternatives for python3

Then you have to update your update-alternatives , then you will be able to set your default python version.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6.9 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8.5 

Now run below command to set default python

sudo update-alternatives --set python /usr/bin/python3.8 

Источник

Оцените статью
Adblock
detector