Python on Linux
As Django is written in Python, understanding how to access and use Python is a must. As such, in this first chapter, we will cover the most important concepts to ensure your career as a Djangonaut is a smashing success.
Installing Python
First things first: getting Python installed on your Linux system. We'll walk through the process of installing Python using package managers like pacman
or apt.
The REPL
Ever wanted to try out a quick snippet of code without leaving the terminal? The Read-Eval-Print Loop (REPL) is your friend! In this section, we'll briefly introduce you to the basic concepts of the REPL.
Virtual Environments with venv
Once you have Python installed, it's time to talk about virtual environments. Virtual environments allow you to isolate your project dependencies from the system-wide Python installation, making it easier to manage different projects with conflicting requirements. We'll cover how to create and manage virtual environments using the built-in venv
module from the Python standard library.
We will also briefly look at installing libraries and packages with pip
.
Poetry
We will then look at Poetry, a popular dependency management and packaging tool, as well as introduce and alternative.
Installing Python applications with pipx
Finally, we'll talk about pipx, a tool that allows you to install packages in isolated environments. With pipx, you can easily manage dependencies for different projects without polluting the system-wide Python installation.
Installing Python and other packages
Note: as we will be using the terminal to install the required packages, you may wish to skim through the Software and Packages and The Linux Terminal chapters if anything is unclear. This will introduce you to accessing the terminal, and running commands.
Arch Linux
Python, Poetry and pipx can be installed from the official Arch Linux package repository via pacman
. To do so, open a terminal and run the following command:
Ubuntu
Python, Poetry and pipx can be installed from the official Ubuntu package repository via apt
. You may also wish to install the python-is-python3
package. This will allow you access Python via the python
command, as opposed to python3
.
To install the packages, open a terminal and run the following command:
The REPL
Once Python is installed, you should be able to run Python code in your terminal. To do so, open a terminal (or use the one in which you installed Python) and run the following command:
You should see a line with the Python version, followed by some helpful text, and a prompt like >>>
.
As the name Read-Eval-Print Loop suggest, the prompt is waiting to read some input, so you can call the Python built-in print()
function to print something to the terminal.
Your input will be evaluated, and may print something before looping back to the read prompt. Type print("Hello, world!")
and hit enter, and you should see output like the following:
If you do not capture the return value of a call, it will be assigned to the _
variable, as in the following example:
Just as in a Python script, anything following the #
is a comment, and will be ignored by the REPL. Any function should work just as in a script, and you can also import modules from the standard library, or any other library and available in your path.
Going into too many details here would be redundant. The Python tutorial linked at the end of this chapter should have all the information you need.
You can type quit()
to exit the REPL now, or keep exploring it and get back to this book afterwards.
Virtual environments and venv
Once you have Python installed and running, it's time to talk about virtual environments. Virtual environments are a way to isolate your project dependencies from the system-wide Python installation. This is especially useful when working on multiple projects that have conflicting requirements. For example, you can have one project running the latest version of Django, which is currently 5.1, and another project that depends on the last long-term support version of Django, which is currently 4.2.
Creating a Virtual Environment
To create a new virtual environment using venv
, navigate to the directory where you want to create the environment and run the following commands:
$ mkdir -p ~/projects/polls/ # name this whatever you like
$ cd ~/projects/polls/ # make sure this matches the name above
$ python -m venv .venv
This will create the directory ~/projects/polls/
, and create a new virtual environment called .venv
within that directory.
Activating a virtual environment
To activate the virtual environment on a bash shell, use the following command:
$ source .venv/bin/activate
(.venv) $ # Notice how it adds the name of the virtual environment to the prompt
If you use a different, such as fish, you may need to adjust the command, e.g.:
Once activated, your shell prompt will change to indicate that you are now operating within the virtual environment.
Deactivating a virtual environment
To deactivate the virtual environment and return to the system-wide Python installation, simply type deactivate
in the terminal:
You can reactivate the virtual environment by running the source
command again.
Installing packages with pip
Within the virtual environment, you can install packages using pip. To install a package, use the following command:
This will download and install Django within the isolated environment.
To install a specific version of a package, use the ==
operator followed by the version number:
You can also use other version specifiers such as ~=
to install a compatible release:
This will install the latest patch version compatible with 4.2. As of this writing, that is version 4.2.16.
Listing installed packages
To list all packages installed within the virtual environment, use the following command:
This will output a list of all packages installed in the environment, along with their versions.
Using a requirements file
You can use the >
operator to redirect the output of pip freeze
to a file named requirements.txt
. This is useful for several reasons:
- It creates a record of all packages installed in your virtual environment, which can be useful for reproducibility or sharing with others.
- It allows you to easily install the same set of packages on another machine by reading from the
requirements.txt
file.
To redirect the output of pip freeze
to a file named requirements.txt
, use the following command:
This will create a new file called requirements.txt
in the current directory, containing a list of all packages installed in the virtual environment.
To install packages from this file, you can use the -r
option with pip:
This will read the requirements.txt
file and install each package listed in it. This is especially useful when sharing a project with others or setting up a new environment for a project.
Other pip features
You can run pip -h
to get help for the pip command, which will list all available options. We will see other features as relevant further in this book.
Installing Python applications with pipx
While we can install packages and libraries using pip, we can also use pipx to install applications. pipx automatically manages virtual environments and dependencies, which can help prevent conflicts.
For example, since the djlint
package is not available in the Arch Linux repository, we can install it using pipx as follows:
The djlint
command will then be available in the terminal, as well as to other applications such as neovim.
Poetry
Throughout this book, we will use Poetry to manage our Django projects. Poetry is a tool for packaging Python projects. Some reasons I recommend Poetry include:
- It uses a
pyproject.toml
file to declare your project's dependencies. This relatively new file is used by many tools for configuration, making things more centralized. - It supports dependency groups, meaning you can define different dependencies for development or production in the same file, rather than having a
requirements.txt
andrequirements-dev.txt
file. - It uses a lock-file to ensure that you have the same version of each dependency installed on each machine. It also makes updating dependencies very simple.
- It automatically manages virtual environments for you on a per-project basis.
If you develop libraries or third-party applications, it also allows you to easily publish packages to PyPI, but this is beyond the scope of this book.
uv
uv is a new alternative to Poetry written in Rust. It will not be covered at this time, as I did not feel it to be production-ready when I last looked into it, but may be worthy of consideration.
Recommendations for package installation
When it comes to packaging or installing Python packages, you have many choices. This can be both a blessing and a curse - on one hand, having multiple options means that you can choose the tool that best fits your needs. On the other hand, it can be overwhelming to decide which tool to use.
In this chapter, we've introduced various popular installation methods, including pip, pipx, and Poetry. So when should you use which?
Official Repository
When a package is available in your distro's official repository, as is the case for many packages on rolling release distros such as Arch Linux, my recommendation is to install that. It is usually vetted by the distro's maintainers, and if other packages depend on it, the version should be compatible.
Poetry
When writing a project, Poetry is an excellent choice for managing dependencies. It allows you to declare your project's dependencies in a pyproject.toml
file, which makes it easy to keep track of what packages are required and which versions are installed. Poetry also provides features like dependency resolution and lock files, which make it easier to reproduce your project's setup.
pipx
When installing applications that aren't available in the official repository, pipx is a great choice. It allows you to install packages in isolated environments, which helps ensure things run smoothly.
Ultimately, the key is to choose a tool that fits your needs and workflow, and to be aware of the trade-offs involved in using each tool. With practice and experience, you'll develop a sense of which tools are best suited for different tasks, and you'll become more efficient at managing your Python packages.