Candid photograph of a Linux server terminal with a soft olive-green glow against a dark slate background, conveying a calm technical atmosphere.

Step-by-step guides for system administrators — covering command-line basics, web server setup, and preparation material for technical interviews.

Browse Tutorials

Building a Python Virtual Environment on RHEL

Python virtual environments keep project dependencies separate from the operating system and from other applications on the same server. On Red Hat Enterprise Linux (RHEL), this is especially useful when one service requires an older framework while another depends on newer packages.

The process is straightforward with dnf, the standard package manager on current RHEL releases. The same approach works for many CentOS Stream and Fedora systems, although package names and Python versions can vary slightly. A clean environment also makes deployments easier to reproduce across Australian cloud regions, offices, and development laptops.

Check The RHEL Host

Start by confirming the operating system and available Python interpreters. RHEL 8 and RHEL 9 can provide several Python streams, so checking the installed version helps prevent confusion when a command such as python3 points to a different interpreter than expected.

cat /etc/redhat-release
python3 --version
sudo dnf module list python

If Python is missing, install the standard interpreter and development tools. On a registered RHEL host, ensure the relevant repositories are enabled through Red Hat Subscription Management before running dnf. Servers in Sydney, Melbourne, or Brisbane data centres often use the same package workflow, even when the application is deployed across different availability zones.

sudo dnf install -y python3 python3-pip python3-devel gcc

Create A Dedicated Project Directory

Avoid placing application code directly in /usr/lib or another directory managed by the package system. A dedicated path such as /opt/inventory-api gives the project clear ownership and reduces the chance that a system update will overwrite application files.

sudo mkdir -p /opt/inventory-api
sudo chown "$USER":"$USER" /opt/inventory-api
cd /opt/inventory-api

Create the environment with the interpreter you intend to use. The venv module creates an isolated directory containing its own Python executable and package installer. Naming the directory .venv is common because it keeps environment files separate from source code and can be excluded from Git.

python3 -m venv .venv

Activate And Verify The Environment

Activation changes the current shell’s PATH, placing the virtual environment’s binaries before the system-wide ones. After activation, confirm both Python and pip resolve inside /opt/inventory-api/.venv.

source .venv/bin/activate
which python
python --version
python -m pip --version

A prompt usually displays (.venv) while the environment is active. Use python -m pip rather than calling pip directly, because it guarantees that packages are installed for the selected interpreter. Upgrade packaging tools before installing application dependencies.

python -m pip install --upgrade pip setuptools wheel

Install And Record Dependencies

Install a framework or library only after activation. For example, a small API might use Flask, while a data-processing service could need Requests and a database driver. The packages remain inside .venv and do not modify the host’s system Python.

python -m pip install flask requests
python -m pip freeze > requirements.txt

The requirements file provides a reproducible package list for testing and deployment. A fresh server can recreate the same dependency set with:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

For an Android-backed service, the API may supply data to a mobile interface; a navigation drawer guide can be useful when the client application needs structured navigation. Keep mobile code and Python server code in separate repositories or directories.

Use Environment Variables Safely

Configuration values such as database addresses, secret keys, and API tokens should not be hard-coded into Python files or committed to a public repository. Set them in the shell for testing, or provide them through a protected systemd service definition in production.

export APP_ENV=production
export DATABASE_URL='postgresql://localhost/appdb'
python app.py

Australian organisations handling customer information should consider obligations under the Privacy Act 1988 and the Australian Privacy Principles. A virtual environment protects software dependencies, but it does not protect credentials or personal data. Store secrets with suitable file permissions and use a managed secret store where practical.

Run The Application On A RHEL Server

Deactivate the environment whenever you finish working in the current shell:

deactivate

For a web service, a process manager or systemd unit should call the environment’s absolute interpreter path, such as /opt/inventory-api/.venv/bin/gunicorn. Nginx can then provide TLS termination, reverse proxying, and static file delivery; the Linuxtpoint web server guides cover related configuration patterns.

Firewall rules should expose only the required ports, commonly SSH and HTTPS. After configuring SSH protection, review the Fail2ban SSH guide for a complementary defence against repeated login attempts. For Australian users, selecting a nearby region can reduce latency, while scheduling maintenance in AEST or AEDT helps avoid disrupting local business hours.

A Python virtual environment is a small, repeatable boundary around an application’s interpreter and dependencies. Install Python with RHEL’s package manager, create .venv, activate it before using pip, record requirements, and run production services with explicit paths. The key practice is simple: isolate each project, protect its configuration, and never confuse application packages with the operating system’s Python installation.