Python

Setup python development environment

RELATED: DEVELOP > Languages > Python | SETUP > Dev Environments > Visual Studio Code > Setup Python Dev Environment | SETUP > Dev Environments > Cloud |

Linux (Ubuntu) typically comes with python pre-installed.

Highlights

virtual environment: create, activate, deactivate & delete
$ cd <project_folder>
$ virtualenv ./.venv
$ virtualenv --python="python311" ".venv"    # for specific python version 
$ source .venv/bin/activate     # linux debian
C:\project_folder>.venv\Scripts\activate    # windows
(.venv) $ deactivate
$ rm -r ./.venv 

Setup (debian)

Source: AWS - Setting up your Python development environment

$ python3 --version
Python 3.8.10
  • Install pip; Use pip to install packages from the Python Package Index and other indexes. Additional setup is needed to pull from other indexes (i.e., package repository) as discussed below.

$ pip --version
Command 'pip' not found, but can be installed with:
sudo apt install python3-pip
$ sudo apt install python3-pip
[...]
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
$ pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
$
Useful pip commands
pip install [package-name]
pip uninstall [package-name]
pip search [package-name]
pip freeze > requirements.txt
pip install -r requirements.txt --trusted-host
  • Setup Python Virtual Environment. Note: python virtual environment is a nice method to provide a clean environment for the application. There are two options:

  • Install the virtualenv package

$ sudo apt install python3-virtualenv
  • Create the virtual environment

$ cd ~/projects/<project-folder>
$ virtualenv ./.venv

# for python3.11; make sure python 3.11 is install; see below Upgrade to Python 3.11
$ virtualenv ./.venv
  • Once the virtual environment is created, activate/enter the virtual environmen by running the activate script located in the environment's bin directory:

$ cd ~/projects/<project-folder>
$ source ./.venv/bin/activate
(venv) $
  • Install & uninstall python packages in the virtual environment

(venv) $ cd ~/projects/<project-folder>
(venv) $ pip install <packageName>        # individual package
(venv) $ pip install -r requirements.txt  # from the requirements file
(venv) $
(venv) $ pip uninstall <packageName>
  • Capture the virtual environment packages (name, version, etc.); such as for deployment in other environments.

(venv) $ cd ~/projects/<project-folder>
(venv) $ pip freeze > requirements.txt
(venv) $
  • When done, leave the virtual environment

(venv) $ cd ~/projects/<project-folder>
(venv) $ deactivate
$
  • Remove the virtual environment folder

$ rm -rf .venv

Upgrade Python to 3.9

Source: https://www.tecmint.com/install-python-in-ubuntu/

From 3.8 to 3.9

Host: Windows WSL Ubuntu 20.04.5 LTS

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.9
# see OUTPUT below
$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root      25 Feb 21 22:53 /usr/bin/python3 -> /etc/alternatives/python3
-rwxr-xr-x 1 root root    5902 Jan 13  2020 /usr/bin/python3-wsdump
-rwxr-xr-x 1 root root 5494584 Nov 14 04:59 /usr/bin/python3.8
-rwxr-xr-x 1 root root 5762192 Dec  6 17:11 /usr/bin/python3.9
$ python3.9 --version
Python 3.9.16
$
# Setting 3.9 as default python; 3.8 still exist
$ python3 --version
Python 3.8.10
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
$ sudo update-alternatives --config python3
# select auto mode: 0
$ python3 --version
Python 3.9.16
$
# virtualenv should work
OUTPUT
$ sudo add-apt-repository ppa:deadsnakes/ppa
 This PPA contains more recent Python versions packaged for Ubuntu.

Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.

Update Note
===========
Please use this repository instead of ppa:fkrull/deadsnakes.

Reporting Issues
================

Issues can be reported in the master issue tracker at:
https://github.com/deadsnakes/issues/issues

Supported Ubuntu and Python Versions
====================================

- Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.11
- Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9 - Python3.11
- Ubuntu 22.04 (jammy) Python3.7 - Python3.9, Python3.11
- Note: Python2.7 (all), Python 3.6 (bionic), Python 3.8 (focal), Python 3.10 (jammy) are not provided by deadsnakes as upstream ubuntu provides those packages.

Why some packages aren't built:
- Note: for focal, older python versions require libssl<1.1 so they are not currently built
- Note: for jammy, older python versions requre libssl<3 so they are not currently built
- If you need these, reach out to asottile to set up a private ppa

The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported.

Packages
========

The packages provided here are loosely based on the debian upstream packages with some modifications to make them more usable as non-default pythons and on ubuntu.  As such, the packages follow debian's patterns and often do not include a full python distribution with just `apt install python#.#`.  Here is a list of packages that may be useful along with the default install:

- `python#.#-dev`: includes development headers for building C extensions
- `python#.#-venv`: provides the standard library `venv` module
- `python#.#-distutils`: provides the standard library `distutils` module
- `python#.#-lib2to3`: provides the `2to3-#.#` utility as well as the standard library `lib2to3` module
- `python#.#-gdbm`: provides the standard library `dbm.gnu` module
- `python#.#-tk`: provides the standard library `tkinter` module

Third-Party Python Modules
==========================
Python modules in the official Ubuntu repositories are packaged to work with the Python interpreters from the official repositories. Accordingly, they generally won't work with the Python interpreters from this PPA. As an exception, pure-Python modules for Python 3 will work, but any compiled extension modules won't.

To install 3rd-party Python modules, you should use the common Python packaging tools.  For an introduction into the Python packaging ecosystem and its tools, refer to the Python Packaging User Guide:
https://packaging.python.org/installing/

Sources
=======
The package sources are available at:
https://github.com/deadsnakes/

Nightly Builds
==============

For nightly builds, see ppa:deadsnakes/nightly https://launchpad.net/~deadsnakes/+archive/ubuntu/nightly
 More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]                                                                       
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease                                                                                          
Get:3 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]                                                                        
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]                                                                         
Get:5 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease [10.5 kB]                                                              
Get:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease [18.1 kB]              
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [24.7 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [2007 kB]
Get:9 https://packages.microsoft.com/ubuntu/20.04/prod focal/main amd64 Packages [238 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]            
Get:11 https://packages.microsoft.com/ubuntu/20.04/prod focal/main arm64 Packages [55.7 kB]      
Get:12 https://packages.microsoft.com/ubuntu/20.04/prod focal/main armhf Packages [32.9 kB]                                   
Get:13 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [327 kB]                                         
Get:14 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [12.2 kB]                
Get:15 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 Packages [25.0 kB]
Get:16 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1508 kB]                             
Get:17 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [213 kB]                        
Get:18 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2384 kB]                                     
Get:19 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [624 B]                         
Get:20 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [797 kB]                                
Get:21 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [156 kB]                                      
Get:22 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [17.0 kB]                                    
Get:23 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [22.9 kB]                                          
Get:24 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [5488 B]                                           
Get:25 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [528 B]                                                 
Get:26 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main Translation-en [5396 B]                                     
Get:27 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [409 kB]                       
Get:28 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.3 kB]
Get:29 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1610 kB]
Get:30 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [227 kB]
Get:31 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [620 B]
Get:32 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1026 kB]
Get:33 http://archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [239 kB]
Get:34 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [23.6 kB]
Get:35 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [24.9 kB]
Get:36 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [880 B]
Fetched 11.8 MB in 4s (2726 kB/s)                                    
Reading package lists... Done

$ sudo apt install python3.9
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  dconf-gsettings-backend dconf-service glib-networking glib-networking-common glib-networking-services gsettings-desktop-schemas libdconf1
  libfwupdplugin1 libproxy1v5 libsoup2.4-1 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libpython3.9-minimal libpython3.9-stdlib python3.9-minimal
Suggested packages:
  python3.9-venv binfmt-support
The following NEW packages will be installed:
  libpython3.9-minimal libpython3.9-stdlib python3.9 python3.9-minimal
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 5023 kB of archives.
After this operation, 19.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.9-minimal amd64 3.9.16-1+focal1 [804 kB]
Get:2 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.9-minimal amd64 3.9.16-1+focal1 [2062 kB]
Get:3 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.9-stdlib amd64 3.9.16-1+focal1 [1662 kB]
Get:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.9 amd64 3.9.16-1+focal1 [495 kB]
Fetched 5023 kB in 5s (1017 kB/s)  
Selecting previously unselected package libpython3.9-minimal:amd64.
(Reading database ... 41005 files and directories currently installed.)
Preparing to unpack .../libpython3.9-minimal_3.9.16-1+focal1_amd64.deb ...
Unpacking libpython3.9-minimal:amd64 (3.9.16-1+focal1) ...
Selecting previously unselected package python3.9-minimal.
Preparing to unpack .../python3.9-minimal_3.9.16-1+focal1_amd64.deb ...
Unpacking python3.9-minimal (3.9.16-1+focal1) ...
Selecting previously unselected package libpython3.9-stdlib:amd64.
Preparing to unpack .../libpython3.9-stdlib_3.9.16-1+focal1_amd64.deb ...
Unpacking libpython3.9-stdlib:amd64 (3.9.16-1+focal1) ...
Selecting previously unselected package python3.9.
Preparing to unpack .../python3.9_3.9.16-1+focal1_amd64.deb ...
Unpacking python3.9 (3.9.16-1+focal1) ...
Setting up libpython3.9-minimal:amd64 (3.9.16-1+focal1) ...
Setting up python3.9-minimal (3.9.16-1+focal1) ...
Setting up libpython3.9-stdlib:amd64 (3.9.16-1+focal1) ...
Setting up python3.9 (3.9.16-1+focal1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for mime-support (3.64ubuntu1) ...

When encountering issue, take a look at From 3.8 to 3.11 section below, about how/what to remove and reinstall.

From 3.8 to 3.11 (Had issue)

Host: Windows WSL Ubuntu 20.04.5 LTS

Details: Python 3.11 installed correctly but there were several issues including: the need to remove-reinstall python3-apt but at the end, the show stopper was the problem with the virtualenv; the issue can't be resolved even with remove-reinstall.

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.11
$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root       9 Mar 13  2020 /usr/bin/python3 -> python3.8
-rwxr-xr-x 1 root root    5902 Jan 13  2020 /usr/bin/python3-wsdump
-rwxr-xr-x 1 root root 7163544 Feb  8 06:49 /usr/bin/python3.11
-rwxr-xr-x 1 root root 5494584 Nov 14 04:59 /usr/bin/python3.8
$ python3.11 --version
Python 3.11.2
$
# to set 3.11 as default
$ python3 --version
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
$ sudo update-alternatives --config python3
# select auto mode: 0
$ python3 --version
Python 3.11.2
$
# Need to remove and reinstall python3-apt because of error during apt update:
# ModuleNotFoundError: No module named 'apt_pkg'
# happen on 3.11 but not on 3.9; to be reconfirmed for 3.9 in the future
$ sudo apt remove python3-apt
$ sudo apt autoremove
$ sudo apt autoclean
$ sudo apt install python3-apt

# reinstall python3-virtualenv
$ sudo apt remove python3-virtualenv
$ sudo apt autoremove
$ sudo apt autoclean
# pip was also removed hence need reinstall
$ sudo apt install python3-pip
$ python3 -m pip install virtualenv
# alternative method to install virtualenv apt install python3-virtualenv
# See OUTPUT for all the issues
# Still had issue; to be investigated when 3.11 is needed!!
# Roll back 3.9
Output:
$ sudo add-apt-repository ppa:deadsnakes/ppa
 This PPA contains more recent Python versions packaged for Ubuntu.

Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.

Update Note
===========
Please use this repository instead of ppa:fkrull/deadsnakes.

Reporting Issues
================

Issues can be reported in the master issue tracker at:
https://github.com/deadsnakes/issues/issues

Supported Ubuntu and Python Versions
====================================

- Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.11
- Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9 - Python3.11
- Ubuntu 22.04 (jammy) Python3.7 - Python3.9, Python3.11
- Note: Python2.7 (all), Python 3.6 (bionic), Python 3.8 (focal), Python 3.10 (jammy) are not provided by deadsnakes as upstream ubuntu provides those packages.

Why some packages aren't built:
- Note: for focal, older python versions require libssl<1.1 so they are not currently built
- Note: for jammy, older python versions requre libssl<3 so they are not currently built
- If you need these, reach out to asottile to set up a private ppa

The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported.

Packages
========

The packages provided here are loosely based on the debian upstream packages with some modifications to make them more usable as non-default pythons and on ubuntu.  As such, the packages follow debian's patterns and often do not include a full python distribution with just `apt install python#.#`.  Here is a list of packages that may be useful along with the default install:

- `python#.#-dev`: includes development headers for building C extensions
- `python#.#-venv`: provides the standard library `venv` module
- `python#.#-distutils`: provides the standard library `distutils` module
- `python#.#-lib2to3`: provides the `2to3-#.#` utility as well as the standard library `lib2to3` module
- `python#.#-gdbm`: provides the standard library `dbm.gnu` module
- `python#.#-tk`: provides the standard library `tkinter` module

Third-Party Python Modules
==========================

Python modules in the official Ubuntu repositories are packaged to work with the Python interpreters from the official repositories. Accordingly, they generally won't work with the Python interpreters from this PPA. As an exception, pure-Python modules for Python 3 will work, but any compiled extension modules won't.

To install 3rd-party Python modules, you should use the common Python packaging tools.  For an introduction into the Python packaging ecosystem and its tools, refer to the Python Packaging User Guide:
https://packaging.python.org/installing/

Sources
=======
The package sources are available at:
https://github.com/deadsnakes/

Nightly Builds
==============

For nightly builds, see ppa:deadsnakes/nightly https://launchpad.net/~deadsnakes/+archive/ubuntu/nightly
 More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]                                                                       
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease                                                                                          
Get:3 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]                                                                        
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]                                                                         
Get:5 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease [10.5 kB]                                                              
Get:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease [18.1 kB]              
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [24.7 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [2007 kB]
Get:9 https://packages.microsoft.com/ubuntu/20.04/prod focal/main amd64 Packages [238 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]            
Get:11 https://packages.microsoft.com/ubuntu/20.04/prod focal/main arm64 Packages [55.7 kB]      
Get:12 https://packages.microsoft.com/ubuntu/20.04/prod focal/main armhf Packages [32.9 kB]                                   
Get:13 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [327 kB]                                         
Get:14 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [12.2 kB]                
Get:15 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 Packages [25.0 kB]
Get:16 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1508 kB]                             
Get:17 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [213 kB]                        
Get:18 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2384 kB]                                     
Get:19 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [624 B]                         
Get:20 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [797 kB]                                
Get:21 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [156 kB]                                      
Get:22 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [17.0 kB]                                    
Get:23 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [22.9 kB]                                          
Get:24 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [5488 B]                                           
Get:25 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [528 B]                                                 
Get:26 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main Translation-en [5396 B]                                     
Get:27 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [409 kB]                       
Get:28 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.3 kB]
Get:29 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1610 kB]
Get:30 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [227 kB]
Get:31 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [620 B]
Get:32 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1026 kB]
Get:33 http://archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [239 kB]
Get:34 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [23.6 kB]
Get:35 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [24.9 kB]
Get:36 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [880 B]
Fetched 11.8 MB in 4s (2726 kB/s)                                    
Reading package lists... Done

$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease                                                                          
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease                                                                                          
Hit:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease                                                 
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease                
Hit:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease           
Hit:7 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease
Reading package lists... Done
Building dependency tree       
Reading state information... Done
67 packages can be upgraded. Run 'apt list --upgradable' to see them.

$ sudo apt install python3.11
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython3.11-minimal libpython3.11-stdlib python3.11-minimal
Suggested packages:
  python3.11-venv binfmt-support
The following NEW packages will be installed:
  libpython3.11-minimal libpython3.11-stdlib python3.11 python3.11-minimal
0 upgraded, 4 newly installed, 0 to remove and 67 not upgraded.
Need to get 5565 kB of archives.
After this operation, 21.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.11-minimal amd64 3.11.2-1+focal1 [849 kB]
Get:2 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.11-minimal amd64 3.11.2-1+focal1 [2311 kB]
Get:3 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.11-stdlib amd64 3.11.2-1+focal1 [1795 kB]
Get:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.11 amd64 3.11.2-1+focal1 [611 kB]
Fetched 5565 kB in 5s (1109 kB/s)   
Selecting previously unselected package libpython3.11-minimal:amd64.
(Reading database ... 43323 files and directories currently installed.)
Preparing to unpack .../libpython3.11-minimal_3.11.2-1+focal1_amd64.deb ...
Unpacking libpython3.11-minimal:amd64 (3.11.2-1+focal1) ...
Selecting previously unselected package python3.11-minimal.
Preparing to unpack .../python3.11-minimal_3.11.2-1+focal1_amd64.deb ...
Unpacking python3.11-minimal (3.11.2-1+focal1) ...
Selecting previously unselected package libpython3.11-stdlib:amd64.
Preparing to unpack .../libpython3.11-stdlib_3.11.2-1+focal1_amd64.deb ...
Unpacking libpython3.11-stdlib:amd64 (3.11.2-1+focal1) ...
Selecting previously unselected package python3.11.
Preparing to unpack .../python3.11_3.11.2-1+focal1_amd64.deb ...
Unpacking python3.11 (3.11.2-1+focal1) ...
Setting up libpython3.11-minimal:amd64 (3.11.2-1+focal1) ...
Setting up python3.11-minimal (3.11.2-1+focal1) ...
Setting up libpython3.11-stdlib:amd64 (3.11.2-1+focal1) ...
Setting up python3.11 (3.11.2-1+focal1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for mime-support (3.64ubuntu1) ...
$
# ISSUES
$ virtualenv ./.venv
Traceback (most recent call last):
  File "/usr/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3253, in <module>
    @_call_aside
     ^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3237, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3266, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 584, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 901, in require
    needed = self.resolve(parse_requirements(requirements))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 787, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'importlib-resources<2,>=1.0' distribution was not found and is required by virtualenv
$
$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease                                                                                          
Hit:2 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease                                                                          
Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease                                                                                  
Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease                                                                                
Hit:5 https://download.docker.com/linux/ubuntu focal InRelease                                                                                  
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease                                                                
Hit:7 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease       
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 12, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
$
# after remove and reinstall python3-apt
$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease                                        
Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease                                
Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease                              
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease                                        
Hit:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease                                
Hit:7 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease        
Reading package lists... Done
Building dependency tree       
Reading state information... Done
59 packages can be upgraded. Run 'apt list --upgradable' to see them.
$
# BUT virtualenv is still an issue;
# after remove & reinstall python3-virtualenv still same issue
$ virtualenv ./.venv
Traceback (most recent call last):
  File "/usr/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3253, in <module>
    @_call_aside
     ^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3237, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3266, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 584, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 901, in require
    needed = self.resolve(parse_requirements(requirements))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 787, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'importlib-resources<2,>=1.0' distribution was not found and is required by virtualenv
$

Customize pip

Setup additional package repository

The setup file is pip.conf, or pip.ini for Windows; that can be found in one or more locations due to: platform variations, or scope (i.e., site-wide, per-user, etc.)

For Ubuntu, the file does not exist by default; can be added to /etc/pip.conf:

[global]
timeout = 60
index-url = https://download.zope.org/ppix

For Raspbian, the default setup, /etc/pip.conf, points to an extra repo:

[global]
extra-index-url=https://www.piwheels.org/simple

For more details, refer to [REF-2] Pip User Guide

Setup trusted host

Example:

$ pip install --trusted-host pypi.python.org

Setup (Windows)

Source: TBD

  • Download version 3.11.0 - https://www.python.org/downloads/

  • Run the downloaded installer:

    • Choose "Customize installation"

    • Uncheck "Install launcher for all users (recommended)

    • Check "Add Python 3.* to PATH"

    • Click next

    • Edit the installed location; default is c:\users\<username>\AppData\local\Programs\Python\Python3**

    • Uncheck the python launcher

    • Click install

  • Check installation - open command prompt

C:\>python --version
Python 3.11.0

C:\>pip --version
pip 22.3 from C:\zApps\Python311\Lib\site-packages\pip (python 3.11)

C:\>
  • Setup Python Virtual Environment. Note: python virtual environment is a nice method to provide a clean environment for the application. There are two options:

  • Install the virtualenv package

C:\>pip install virtualenv
Collecting virtualenv
  Downloading virtualenv-20.16.6-py3-none-any.whl (8.8 MB)
     ---------------------------------------- 8.8/8.8 MB 10.6 MB/s eta 0:00:00
Collecting distlib<1,>=0.3.6
  Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     ---------------------------------------- 468.5/468.5 kB 4.9 MB/s eta 0:00:00
Collecting filelock<4,>=3.4.1
  Downloading filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting platformdirs<3,>=2.4
  Downloading platformdirs-2.5.2-py3-none-any.whl (14 kB)
Installing collected packages: distlib, platformdirs, filelock, virtualenv
Successfully installed distlib-0.3.6 filelock-3.8.0 platformdirs-2.5.2 virtualenv-20.16.6
C:\>
C:\>virtualenv --version
virtualenv 20.16.6 from C:\zApps\Python311\Lib\site-packages\virtualenv\__init__.py

C:\>
  • Create the virtual environment

C:\>cd c:\projects\<project-folder>
C:\projects\project-folder>virtualenv .venv
created virtual environment CPython3.11.0.final.0-64 in 6485ms
  creator CPython3Windows(dest=C:\zCodes\test-py\.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\gabe\AppData\Local\pypa\virtualenv)
    added seed packages: pip==22.3, setuptools==65.5.0, wheel==0.37.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

C:\projects\project-folder>dir
[...]
10/30/2022  04:00 PM    <DIR>          .venv
               0 File(s)              0 bytes
               3 Dir(s)  414,490,734,592 bytes free

C:\projects\project-folder>
  • Once the virtual environment is created, activate/enter the virtual environmen by running the activate script located in the environment's bin directory:

C:\projects\project-folder>.venv\Scripts\activate
(.venv) c:\projects\project-folder>python --version
Python 3.11.0

(.venv) c:\projects\project-folder>  

Multiple versions (side-by-side)

  • For windows 11, the default installed folder is ..\<username>\AppData\Local\Programs\Python\; for Python version 3.11, it will be \Python311\

  • After installation, add the following to the System Properties > Environment Variables

..\<username>\AppData\Local\Programs\Python\Python311\
..\<username>\AppData\Local\Programs\Python\Python311\Scripts\
  • Got the the installed folder and copy the python.exe to python311.exe

  • Let's assume we have an existing python38, and we just installed python311. Now, open a command window and we should see the following

C:\Users\user1>python --version
Python 3.8.9

C:\Users\user1>python311 --version
Python 3.11.6
  • To install the python virtual environment using the chosen python version

C:\project1>virtualenv --python="python311" ".venv"
created virtual environment CPython3.11.6.final.0-64 in 6305ms
  creator CPython3Windows(dest=C:\project1\.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\user1\AppData\Local\pypa\virtualenv)
    added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
C:\project1>.venv\Scripts\activate
(.venv) C:\project1>python --version
Python 3.11.6
(.venv) C:\project1>
 

Jupyter Notebook

Install JupyterLab

JupyterLab is a feature-rich notebook authoring application and editing environment.

cd ~/<project_folder>
# create virtual environment
virtualenv ./.venv
pip install jupyterlab
# api docs on hover-over
pip install jupyter-lsp
pip install jupyterlab-lsp
pip install python-lsp-server
# starting
jupyter lab

Cloud Environment

Extension

References

Last updated