Python
Setup python development environment
RELATED: DEVELOP > Languages > Python | SETUP > Dev Environments > Visual Studio Code > Setup Python Dev Environment | SETUP > Dev Environments > Cloud |
Highlights
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)
$
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
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
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>
Install python package using
pip
. See Setup (Debian)
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
topython311.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.
Highly recommended - install in the virtual 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
Google Colaboratory - https://colab.research.google.com/
Extension
Link - https://link.makinarocks.ai/
References
Last updated