Python

Setup python development environment

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

circle-info

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

Highlights

chevron-rightvirtual environment: create, activate, deactivate & delete hashtag
$ 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 environmentarrow-up-right

$ python3 --version
Python 3.8.10
  • Install pip; Use pip to install packages from the Python Package Indexarrow-up-right 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)
$
chevron-rightUseful pip commandshashtag
  • 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

  • Create the virtual environment

  • Once the virtual environment is created, activate/enter the virtual environmen by running the activate script located in the environment's bin directory:

  • Install & uninstall python packages in the virtual environment

  • Capture the virtual environment packages (name, version, etc.); such as for deployment in other environments.

  • When done, leave the virtual environment

  • Remove the virtual environment folder

Upgrade Python to 3.9

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

From 3.8 to 3.9

Host: Windows WSL Ubuntu 20.04.5 LTS

chevron-rightOUTPUThashtag

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.

chevron-rightOutput:hashtag

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:

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

For more details, refer to [REF-2] Pip User Guidearrow-up-right

Setup trusted host

Example:

Setup (Windows)

Source: TBD

  • 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

  • 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

  • Create the virtual environment

  • Once the virtual environment is created, activate/enter the virtual environmen by running the activate script located in the environment's bin directory:

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

  • 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

  • To install the python virtual environment using the chosen python version

Jupyter Notebook

Install JupyterLab

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

Cloud Environment

Extension

References

Last updated