Linux Server setup

Python, pip, PostgreSQL

In this chapter, we will set up a web application based on Python with the Django framework.

The programming language Python is already a good 30 years old and has become one of the most popular languages in recent years due to a rapidly growing community. The package manager for Python is called pip, which can be used to obtain modules from the Python Package Index (PyPI).

We use the Django framework with the open-source PostgreSQL database.

I will create the web application in the folder /var/www/ We had already prepared that in Subdomain Server Block (dev). Be sure to adjust that accordingly for yourself at the appropriate point.

Python in virtual environments

Python 3 is already integrated in our Linux Ubuntu 20 distribution. We can easily check this by querying the version of Python:

__$ python3 –V

When I copy and paste the above command, I get the message can't open file '-V':

python3: can't open file '–V': [Errno 2] No such file or directory

As the error suggests, the python3 command treats the following parameter -V or --version as a filename.

If, on the other hand, the command is typed in manually, the version appears:

Python 3.8.10

Python applications that depend on pip packages should ideally run in their own virtual environments. The background is that pip packages are usually installed globally. This can lead to problems when packages are updated and applications become incompatible with them.

venv module

With the venv module, Python applications can be encapsulated. We simply install the software extension with apt:

__$ sudo apt install -y python3-venv

We will come back to the module later when we create the application.

Install pip

We install the package management tool with apt:

__$ sudo apt install -y python3-pip

Create database in PostgreSQL

We have already set up the PostgreSQL database in the Installing PostgreSQL chapter.

We can create a new database named django_db from the console:

__$ createdb django_db