Interpreter#
This section covers Python as software: installation, usage, source files, etc.
Note: Some examples may change the environment in which they run, so it’s better to run them in Docker. Use the following cell to start the container, the rest of the code will be executed in the container.
Build python#
Sometimes you need Python with a specific configuration, for such cases you’ll need to build Python from source. You can download the distribution from the corresponding releases of the official github account. Then just compile it with the C compiler (gcc
for linux) and prepare it to work in the system with the make
build system.
For more details check:
Consider the process of building and installing any Python distribution.
We require some software to be able to download and build source code.
apt update &> /dev/null && apt install -y wget gcc make &> /dev/null
The following cell downloads and unpacks the source code to be built.
[ -d /tmp/installation ] && rm -r /tmp/installation
mkdir /tmp/installation
cd /tmp/installation
wget -q https://github.com/python/cpython/archive/refs/tags/3.8.tar.gz
tar -xzf 3.8.tar.gz
ls
3.8.tar.gz cpython-3.8
As the result there is cpython-3.8
folder that contains all necessary to build.
The following cell compiles the project, builds it, and adds it to the system as a regular package.
cd cpython-3.8
./configure &> /dev/null
make &> /dev/null
make install &> /dev/null
true
All these steps added to the system new, self-built python. The following cell shows that it works fine.
python3.8 --version
Python 3.8.20
After installation, the typical location where Python appears is /usr/local/bin/
so you can invoke it directly.
Virtual environment#
It’s useful to have a different python than the one you’re using - you can install different packages and even different pythons. There is a venv
package in the standard library that automates the process of building special Python interpreters.
To activate envrironmet typicaly use
*/bin/activate
script.To deactivate environment use
deactivate
command that is awailable in the environment.
Check more at the following sources:
Relevant page on the official documentation.
Python Virtual Environments: A Primer extended tutorial.
The following cells show an example of creating, enabling, and disabling a Python virtual environment. But first, let’s check the current Python location to validate that the interpreter has successfully changed after activating the environment.
which python3
/usr/local/bin/python3
The following cell creates a new environment and displays the contents of the current folder.
[ -d /tmp/venv ] && rm -r /tmp/venv
mkdir /tmp/venv
cd /tmp/venv
python3 -m venv venv
ls
venv
There is a venv
folder - this is actually a folder that contains the python distribution.
The following code activates the environment and shows the path to the interpreter.
source venv/bin/activate
which python3
/tmp/venv/venv/bin/python3
Path is the path of the previously created virtual environment.
The deactivation process and the result are shown next.
deactivate
which python3
/usr/local/bin/python3