Python Programming Language Ecosystem

This post is an introduction to the programming language Python.

Understanding Python

What is Python?

Python is a high-level, object-oriented, dynamically typed and interpreted programming language.

It is “interpreted” in the sense that there is no additional step required to compile before executing source code; however, most of the programming language implementations converts source code into bytecode in a transparent way for the user.

Its first version was released on 1991, but it has received major updates over the time. The latest version update was Python3 on 2008.

Python is owned by the Python Software Foundation (PSF).

It has a BSD-based license, meaning that it is free and open source (FOSS), what attracts some developers and explain its popularity among some communities like Linux users.

You need to have a virtual machine installed in your computer in order to run a Python application.

As an interpreted language, what is the interpreter for Python?

What are the advantages of using Python?

One main characteristic that makes Python different to others is that it is dynamically typed. Because you do not need to worry about the variable types, it is easier and it is common among beginners and casual programs.

Python is sometimes referred as a glue language, because it is often used to connect large software components.

There are multiple libraries available for Python, and you can find some examples on this post.

Python is supported by a numerous and active community.

What are the disadvantages of using Python?

One critique about Python is that it is not as mature as other established programming languages.

Another critique is that is performance is not as good as other languages like C, C++ or Rust.

Do I need to compile Python source code?

Source code files include the logic of the application in a format that is readable by a human. These files have .py extension file.

The interpreter CPython is not able read source code directly, and it needs it to be converted to bytecode, in the same way as other interpreted languages as Java. In this case, once a .py file is compiled, a new file .pyc with complied bytecode is generated.

The interpreter Jython generated .class files.

Are there different versions of Python?

There has been different versions of Python during its history.

As of 2023, Python 3 is the latest version and it is not backwards compatible. It was released on 2008.

Python 2 support ended on January 2020.

When you read about Python, most probably they are talking about Python 3.

Does the user need to have Python installed on their computer to run an app?

Generally speaking, Python scripts need a Python interpreter (CPython) installed in a computer to run the compiled bytecode.

Nevertheless, there are ways to create standalone files from the Python script (binaries) that can be run on specific OS without the need of an interpreter. So the short answer to the question would be ‘no’, as we can avoid the requirement of having Python installed on a computer to run one of its programs.

One of the advantages of interpreted language is that the same file can be run on any computer without modifications. A drawback is that you need to have installed a Python interpreter (CPython) on each computer where it is going to run.

Alternatively, you can create an standalone executable file that does not require to have CPython previously installed, but on the other hand it will work only on specific platforms and the file will lose its portability.

Python Programming Language

You can read more about Python programming language syntax and common solutions on this post.

Python Tools

Types of Python tools:

  • Interpreter
  • Package manager
  • Interactive environments
  • Formatter

Interpreter

An Python interpreter is the engine that executes Python.

The are different Python interpreter implementations, being the most popular:

  • CPython
  • PyPy
  • Cython
  • Numba
  • IronJython
  • Jython

CPython performs interpreted execution that compiles to bytecode. It is the reference or standard implementation, written in C. It requires the CPython VM to read the bytecode. CPython is called by default when using the commands python or python3 from a terminal.

PyPy is a just-in-time (JIT) compiler, adding a real-time compilation step to native machine code as soon as it is executed. Take note that, unlike CPython, it compiles to native machine code instead of bytecode.

Cython is an optimizing static compiler for Python, allows you to write code in C, compile it ahead of time, and then use the results in your Python program.

Numba is another JIT compiler used for a subset of Python and NumPy module code. It leverages LLVM via the llvmlite package.

IronPython compiles to CLR/.NET code. It is developed by Microsoft.

Jython complies to JVM code.

Python package repository

A Python package repository or Python package source is a service that provides Python packages.

Public Python package repositories:

  • PyPI
  • Anaconda

PyPI

Python Package Inventory (PyPI) is the official package inventory.

Anaconda

The Anaconda project has its own Python package repository.

Package manager

A package manager is used to install dependencies, either in the system or in a virtual environment.

Package manager featured on this post:

  • pip
  • conda
  • mamba
  • poetry

Either of these package manager can be used. It is not recommend to use a package manager on a package that was installed with a different one.

pip

pip is the default Python package manager.

python3-pip installs Python modules.

When pip is invoked, it checks whether there is a file within the folder called “setup.py” or “pyproject.py”.

It retrieves package from PyPI.

It install Python package only. Non-Python libraries may need to be installed manually.

It may compile some source code before installation. This can lead to compiler dependency issues.

conda

conda is a package manager popular among data scientist.

It retrieves package from the Anaconda package repository.

conda installs non-Python libraries (e.g. C libraries, BLAS, compilers, etc.) in addition to Python packages.

It install binaries directly.

conda is recommended when there are non-Python libraries involved. This is common in some packages like those related to data science (NumPy, pandas, etc.).

It is FOSS, under a BSD license.

mamba

mamba is a fast reimplementation of conda.

It is maintained by the conda-forge community.

Interactive environments

Interactive environments featured on this post:

  • Standard Python shell
  • IPython
  • Jupyter

Standard Python Shell

The standard Python shell is an interactive environment for Python that provides a command-line interface (CLI).

IPython

IPython, meaning interactive Python, adds functionality to the standard Python shell.

You can read more on this post about IPython.

Jupyter

Jupyter is an interactive environment for Python that provides a GUI. It may use iPython as backend.

Code formatter

Python code formatter featured on this post:

  • Black

Black

Black is a code formatter for Python.

Python distributions

A Python distribution is a software package that includes bundled software related to Python.

A Python developer can either install each package individually or install a pre-packaged distribution.

There are different Python distributions:

  • Anaconda
  • Mambaforge
  • EPD

Anaconda

Anaconda is a Python distribution.

Anaconda is recommended for beginners and data scientists that look for a pre-build environment.

It is a proprietary package, as it contains both FOSS and proprietary content.

Anaconda is managed by the company Anaconda Ltd.

Miniconda is a lightweight version of Anaconda that just includes conda.

Miniconda is recommended for advanced users that want to control what is install or when pip and conda packages are going to exist.

You can find the Miniconda installation instructions on this external link.

Mambaforge

Mambaforge is a FOSS replacement for Anaconda, that includes both the conda standard implementation and the mamba reimplementation.

It is FOSS.

It’s not available in apt package manager because it is another package manager that is not tightly integrated with a single application, but many, and it can compete with apt.

Miniforge excludes the mamba manager from the package.

Miniforge code repository

Do you wish to update your shell profile to automatically initialize conda?
This will activate conda on startup and change the command prompt when activated.
If you'd prefer that conda's base environment not be activated on startup,
   run the following command when conda is activated:

conda config --set auto_activate_base false

You can undo this by running `conda init --reverse $SHELL`?

EPD

Enthought Python Distribution (EPD) is a Python distribution.

How to install Python

Python installation instructions may differ depending on the OS.

Installing Python on Linux

Python is probably installed by default on Linux OS, but not on its latest version. You can add a repository to Linux in order to ensure you have the latest version.

To check Python version on Linux OS:

$ python3 --version

To install Python3:

$ sudo apt install python3

If you want to intall a specific version of Python, you can add a dot plus the version number after “python3”.

For example, if you want to install Python 3.11:

$ sudo apt install python3.11

To add the repository containing the latest versions to Linux OS, open a terminal and type:

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

To test that Python works,  go to a terminal and type:

$ python3

Installing Python on Windows

Go to this external link and download Python for Windows.

Follow the installation wizard instructions.

Installing Python on macOS

Go to this external link and download Python for macOS.

Follow the installation wizard instructions.

Python Project Setup

Steps on Python project setup:

  1. Metadata setup
  2. Virtual environment setup
  3. Module setup
  4. Program execution

Project metadata setup

The project metadata can be stored in a configuration file called “setup.py” or “pyproject.toml” file located in the project root folder.

This file is looked up by pip, testing tools and other utilities.

Virtual environment setup

You can create a virtual environment using the python3-ven application.

To create a virtual environment in the folder “.venv”, go to the project root folder:

python3-venv -m .venv

Once the virtual environment is activated, everything you install using pip is installed within the virtual environment, not the Python system.

To activate the virtual environment in Linux:

source .venv/bin/activate

You can revert the activation closing the terminal or typing:

deactivate

Module setup

Then install the necessary modules within the virtual environment in development mode:

pip install -e .[dev]

If you don’t need a development mode because you’re not installing it for development purposes, just type:

pip install .[dev]

Program execution

Just run program.

You might be also interested in…

External references

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *