Pip3 change python version

Upgrade Python to latest version (3.10) on Ubuntu Linux

Linux systems come with Python install by default, but, they are usually not the latest. Python also cannot be updated by a typical apt upgrade command as well.

To check the version of Python installed on your system run

python keyword is used for Python 2.x versions which has been deprecated

  1. Update Python to the latest version
  2. Fix pip & other Python related issues
  3. While doing the above two, ensure your Ubuntu which is heavily dependent on Python does not break

Updating Python to the latest version

Ubuntu’s default repositories do not contain the latest version of Python, but an open source repository named deadsnakes does.

Python3.10 is not officially available on Ubuntu 20.04, ensure you backup your system before upgrading.

Step 1: Check if Python3.10 is available for install

sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update 

Check if Python 3.10 is available by running

Читайте также:  Css рассчитать высоту блока

This will produce the below result, if you see python3.10 it means you can install it

Step 2: Install Python 3.10

Now you can install Python 3.10 by running

sudo apt install python3.10 

Now though Python 3.10 is installed, if you check the version of your python by running python3 —version you will still see an older version. This is because you have two versions of Python installed and you need to choose Python 3.10 as the default.

Step 3: Set Python 3.10 as default

Steps beyond here are tested on Ubuntu 20.04 in VM & WSL2, but are experimental , proceed at your own risk.

Changing the default alternatives for Python will break your Gnome terminal. To avoid this, you need to edit the gnome-terminal configuration file.

Open the terminal and run:

sudo nano /usr/bin/gnome-terminal 

In first line, change #!/usr/bin/python3 to #!/usr/bin/python3.8 . Press Ctrl +X followed by enter to save and exit.

Then save and close the file.

Next, update the default Python by adding both versions to an alternatives by running the below

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 Now run
sudo update-alternatives --config python3 

Choose the selection corresponding to Python3.10 (if not selected by default).

Now run python3 —version again and you should see the latest Python as the output.

Fix pip and disutils errors

Installing the new version of Python will break pip as the distutils for Python3.10 is not installed yet.

Fix Python3-apt

Running pip in terminal will not work, as the current pip is not compatible with Python3.10 and python3-apt will be broken, that will generate an error like

Traceback (most recent call last): File "/usr/lib/command-not-found", line 28, in <module> from CommandNotFound import CommandNotFound File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module> from CommandNotFound.db.db import SqliteDatabase File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg' 

To fix this first remove the current version of python3-apt by running

sudo apt remove --purge python3-apt 

DO NOT RUN sudo apt autoremove as it will remove several packages that are required. This may break your system if you’re using GUI, if you’re on WSL2 you can proceed.

Finally, reinstall python3-apt by running

sudo apt install python3-apt 

Install pip & distutils

Running pip will still throw an error pip: command not found . We need to install the latest version of pip compatible with Python 3.10.

Also, if try to manually install the latest version of pip, it will throw an error like

ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.10/distutils/__init__.py) 

Or you might also see an error stating No module named ‘distutils.util’ . This is because the distutils module is not installed yet, to install run the below command

sudo apt install python3.10-distutils 

Now you can install pip by running

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python3.10 get-pip.py 

If you get an error like bash: curl: command not found then you need to install curl first by running sudo apt install curl

Now you can run pip and you should see the output of pip —version

Fix pip-env errors when using venv

When you try to create a new virtual environment using python -m venv env , you may into the following error.

Error: Command -Imensurepip--upgrade--default-pipYou can fix this by reinstalling venv by running
sudo apt install python3.10-venv 

All should be done now. It is complicated, but this is how you update Python to latest version.

Extra

If you have oh-my-zsh installed, you can avoid typing out python3 by running

Now you can run your files with py or python .

Источник

Pip3 change python version

I wanted to change my Python version associated with pip from 3.5 to 3.8. This proved nontrivial and totally undocumented anywhere I searched, so here’s an explanation of what I did so that I have a reference of what to do for next time, yay!

This guide describes the process for Ubuntu 18.04.4 LTS.

Changing version of Python3

Changing the version associated to python3 is pretty easy to find instructions about. If you have multiple versions installed, you just need to cd to /usr/bin , check what the path names are, and then use update-alternatives to configure which version is the default.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 

However, note from the comments here that using update-alternatives may be unsafe for some installers that expect python3 to be an older version of Python. Certainly, I would NOT use this to make python point to python3.8 .

Changing version of Pip

But to change version of pip, I couldn’t really find easy documentation. Since pip is just a package manager, and not actual software, there is not as much concern about breaking any dependencies; I want to be able to type pip install instead of python3 -m pip install . This is already possible to do inside of all of my venvs, but outside of a venv, pip install gives the Python 3.5 pip, not Python 3.8.

When searching for how to do this, I found this Stack Overflow thread, but it only helped a little. So here’s the full process of what I did.

First, I uninstalled any current version of pip using apt :

sudo apt remove python-pip sudo apt remove python3-pip 

But pip3 -V still existed and gave the wrong version:

The command pip3 -V giving python 3.5 pip

So then I looked at my $PATH variable ( echo $PATH ):

The output of echo $PATH

And I randomly cd ’d through folders til I got to /usr/local/bin , which had what I wanted:

The contents of /usr/local/bin

Then I restarted my shell, and because I had previously done the update-alternatives thing, I figured I could just run:

sudo apt install python3-pip pip3 -V 

And this did indeed now give me Python3.8 pip, but then I found out I had made a mistake.

A mistake

So previously, I had been playing around in /usr/bin (lol), and I had done a couple things:

The first was to create a symlink from pip3 to the pip package in my Python 3.8 installation:

ln -s pip3 /home/river/.local/lib/python3.8/site-packages/pip 

The second was to create a symlink from pip to pip3 :

Turns out…this was NOT what I wanted to do.

The diagnosis

The command pip -V with a warning

So now pip was working, but when I ran pip -V I got this warning:

Here is the issue it’s mentioning. I didn’t find that too helpful, but I instead searched the actual text of the message, and came across this Stack Overflow thread which suggested that I run the following:

python3 -m pip install --upgrade --force-reinstall pip 

The command to reinstall pip

The resolution — actually a necessary setup step

So I added /home/river/.local/bin/ to my $PATH (before doing this, I echo $PATH ’d just in case I messed up):

export PATH="/home/river/.local/bin/:$PATH" 

And I also put it into ~/.profile and ~/.config/fish/config.fish per this Ask Ubuntu post.

The commands pip -V and pip3 -V with the right outputs!

Installing mwrogue

Clean up the mistake

Now let’s just remove the bad symlinks from /usr/bin and make sure everything still works:

Removing bad symlinks & rechecking pip version

Conclusion

Ignoring the detour we took, you need to:

  • Uninstall python3-pip & python-pip using apt
  • Remove the old pip files from /usr/local/bin
  • Reinstall python3-pip using apt
  • Add $HOME/.local/bin to your $PATH (also restart your shell to make sure you did this right)

And then pip and pip3 should both point to Python 3.8 pip instead of Python 3.5!

Now, let’s just hope I don’t have to update to 3.10 any time soon….

Источник

Change Python Version Mac How to set Python3 as a default python version on MacOS?

By default MacOS ships with Python-2.-. But, I guess most of us have long back started to work with Python-3 and it is very irritating to run python3 every time instead of python in terminal. Here is how to do this. Open the terminal (bash or zsh) whatever shell you are using. Install python-3 using Homebrew (https://brew.sh).

lrwxr-xr-x 1 irfan admin 34 Nov 11 16:32 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3 lrwxr-xr-x 1 irfan admin 41 Nov 11 16:32 /usr/local/bin/python3-config -> ../Cellar/python/3.7.5/bin/python3-config lrwxr-xr-x 1 irfan admin 36 Nov 11 16:32 /usr/local/bin/python3.7 -> ../Cellar/python/3.7.5/bin/python3.7 lrwxr-xr-x 1 irfan admin 43 Nov 11 16:32 /usr/local/bin/python3.7-config -> ../Cellar/python/3.7.5/bin/python3.7-config lrwxr-xr-x 1 irfan admin 37 Nov 11 16:32 /usr/local/bin/python3.7m -> ../Cellar/python/3.7.5/bin/python3.7m lrwxr-xr-x 1 irfan admin 44 Nov 11 16:32 /usr/local/bin/python3.7m-config -> ../Cellar/python/3.7.5/bin/python3.7m-config 

Change the default python symlink to the version you want to use from above.
Note that, we only need to choose the one that end with python3.*. Please avoid using the ones’ that end with config or python3.*m or python3.*m-config. Below command shows how it should be done:

ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python 

Close the current terminal session or keep it that way and instead open a new terminal window (not tab). Run this:

Top comments (85)

I’m not saying anyone would want to install Python 3 without brew, but if they did, pip3 gets installed:
$ pip3 —version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
$ pip3 install —upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54. (1.4MB)
|████████████████████████████████| 1.4MB 1.7MB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2

$ which pip
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

Now, Irfan’s handy command:
sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python

$ python —version
Python 3.8.2

7 likes Like Comment button

Hey! I tried following your above-mentioned method .. but I don’t know how the path is different . Although this method worked but will that path create any problem later on?
On my mac
which pip results in
/usr/local/bin/pip
instead of
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip

apoorveesinha@Apoorvees-Air ~ % python —version
Python 2.7.16
apoorveesinha@Apoorvees-Air ~ % python3 —version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % pip3 —version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
apoorveesinha@Apoorvees-Air ~ % pip3 install —upgrade pip
Collecting pip
Downloading files.pythonhosted.org/packages/54. (1.4MB)Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % which pip
/usr/local/bin/pip
apoorveesinha@Apoorvees-Air ~ % sudo ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
Password:
apoorveesinha@Apoorvees-Air ~ %

apoorveesinha@Apoorvees-Air ~ % python —version
Python 3.8.2
apoorveesinha@Apoorvees-Air ~ % python3 —version
Python 3.8.2

2 likes Like Comment button

Источник

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