Python 101 - Setting Python Environment

Installing or updating Python on your computer is the first step to becoming a Python programmer. There are many ways of python installation:

  1. You can download official Python distributions from Python.org
  2. Install from a package manager, and even install specialized distributions such as Anaconda for scientific computing
  3. Distribution from Intel for Internet of Things, and embedded systems.

In this post we will focus on:

  • Python Installation on Linux Platform and Getting Started.
  • Python Package Management using pip
  • Creating and Managing Python Virtual Environments

Install Python on Linux (Ubuntu)

Python is already comes installed by default on any Linux platform. You can check which version of python is installed on your system using below commands.

# Check the system Python version
$ python --version
# Check the Python 2 version
$ python2 --version
# Check the Python 3 version
$ python3 --version

If you don't find any python version installation on your system ( which usually doesn't happens ) you can still install it using below commands.

First, update the system package information from all configured sources. Run command to install Python (Recommended Python 3) and then check the version.

$ sudo apt-get update
$ sudo apt-get install python3 python3-venv python3-pip
$ python3 --version

It may happen that upstream ubuntu does not provides up-to-date python version. No worries we can add custom ppa/repository to linux and install package from them.

Add custom PPA to Ubuntu Repository and install latest Python Version.

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.10 python3-venv python3-pip
$ python3 --version

As we have installed Python and ready to use it. Prior to that I would like to introduce you what is virtual environment in Python.

What is Virtual Environment?

Python Virtual Environment is a folder/directory in which everything is installed to run a python program and is isolated from another environment such as "libraries installed in the system" or other "virtual environment"

When you use virtual environment you don't need to modify or mess up with systems libraries or break any system configuration.

Creating Virtual Environment.

Now you know what is required to get started and already know what is Python Virtual Environment. It is always recommended to create virtual environment to work with python projects so system's python library and your project's python libraries version's do not conflict with each other.

Let's create virtual environment named worklab using below command.

$ python -m venv worklab

This will create the worklab directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.

worklab directory structure will look something like below tree.

worklab
│
├── bin/
│   ├── Activate.ps1
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── pip
│   ├── pip3
│   ├── pip3.10
│   ├── python -> /usr/bin/python
│   ├── python3 -> python
│   └── python3.10 -> python
│
├── include/
│
├── lib/
│   │
│   └── python3.10/
│       │
│       └── site-packages/
│           │
│           ├── _distutils_hack/
│           │
│           ├── pip/
│           │
│           ├── pip-22.0.4.dist-info/
│           │
│           ├── pkg_resources/
│           │
│           ├── setuptools/
│           │
│           ├── setuptools-58.1.0.dist-info/
│           │
│           └── distutils-precedence.pth
│
├── lib64/
│   │
│   └── python3.10/
│       │
│       └── site-packages/
│           │
│           ├── _distutils_hack/
│           │
│           ├── pip/
│           │
│           ├── pip-22.0.4.dist-info/
│           │
│           ├── pkg_resources/
│           │
│           ├── setuptools/
│           │
│           ├── setuptools-58.1.0.dist-info/
│           │
│           └── distutils-precedence.pth
│
└── pyvenv.cfg

As we have already created virtual environment, let's activate and start using it.

$ cd worklab/
$ source bin/activate
(worklab)$     <- you can see environment name in front of command prompt.

Well Done...!

You have successfully created your own isolated virtual environment. Now you can install any package with any version you need.

Managing Packages with pip

pip is the de facto and recommended package-management system written in Python and is used to install and manage software packages. It connects to an online repository of public packages, called the Python Package Index.

We can install, find, list, and remove the packages from library using pip.

pip has various subcommands such as install, uninstall, show, list, freeze

Let's install python package called pandas using pip but make sure you have activated you virtual environment using source bin/activate command from your environment directory.

(worklab)$ pip install pandas

Collecting pandas
  Downloading pandas-1.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.7/11.7 MB 910.2 kB/s eta 0:00:00
Collecting python-dateutil>=2.8.1
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 KB 844.0 kB/s eta 0:00:00
Collecting numpy>=1.21.0
  Downloading numpy-1.22.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.8/16.8 MB 822.0 kB/s eta 0:00:00
Collecting pytz>=2020.1
  Downloading pytz-2022.1-py2.py3-none-any.whl (503 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 503.5/503.5 KB 1.1 MB/s eta 0:00:00
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: pytz, six, numpy, python-dateutil, pandas
Successfully installed numpy-1.22.3 pandas-1.4.2 python-dateutil-2.8.2 pytz-2022.1 six-1.16.0

Show information about specific package

(worklab)$ pip show pandas
Name: pandas
Version: 1.4.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author: The Pandas Development Team
Author-email: pandas-dev@python.org
License: BSD-3-Clause
Location: /home/logan/worklab/lib/python3.10/site-packages
Requires: numpy, python-dateutil, pytz
Required-by:

Upgrade package using pip install --upgrade.

(worklab)$ pip install --upgrade django
Requirement already satisfied: django in ./lib/python3.10/site-packages (2.0)
Collecting django
  Downloading Django-4.0.4-py3-none-any.whl (8.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.0/8.0 MB 1.6 MB/s eta 0:00:00
Collecting asgiref<4,>=3.4.1
  Downloading asgiref-3.5.2-py3-none-any.whl (22 kB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Installing collected packages: sqlparse, asgiref, django
  Attempting uninstall: django
    Found existing installation: Django 2.0
    Uninstalling Django-2.0:
      Successfully uninstalled Django-2.0
Successfully installed asgiref-3.5.2 django-4.0.4 sqlparse-0.4.2

Uninstall or remove python packages from python environment. Below command will prompt you for confirmation of package uninstall.

(worklab)$ pip uninstall django
Found existing installation: Django 4.0.4
Uninstalling Django-4.0.4:
  Would remove:
    /home/logan/worklab/bin/django-admin
    /home/logan/worklab/lib/python3.10/site-packages/Django-4.0.4.dist-info/*
    /home/logan/worklab/lib/python3.10/site-packages/django/*
Proceed (Y/n)? Y
  Successfully uninstalled Django-4.0.4

You can list packages/libraries installed in your environment.

(worklab)$ pip list
Package         Version
--------------- -------
numpy           1.22.3
pandas          1.4.2
pip             22.0.2
python-dateutil 2.8.2
pytz            2022.1
setuptools      59.6.0
six             1.16.0

pip freeze will also produce similar list like above command but the output will be similar to what pip install command uses to install packages

Well this command is also useful when you want replace the existing environment with new one and install all the required packages all at once.

(worklab)$ pip freeze > requirements.txt
(worklab)$ cat requirements.txt 
asgiref==3.5.2
numpy==1.22.3
pandas==1.4.2
python-dateutil==2.8.2
pytz==2022.1
six==1.16.0
sqlparse==0.4.2

Install all above mentioned packages using same requirements.txt file created above.

(worklab)$ pip install -r requirements.txt

After you have done using you virtual environment you deactivate this or remove the environments.

To deactivate you simply need to use deactivate command.

(worklab)$ deactivate
$               <- you can notice worklab name is gone from prompt.

Conclusion

Congratulations on making it through this tutorial on Python installation and creating virtual environments.

In this tutorial, you learned how to:

  • Install Python in Different ways.
  • Create and activate a Python virtual environment
  • Visualize how virtual environment directory looks like
  • Activate, Deactivate and Remove virtual environments
  • Using python package management tool pip