A Python virtual environment helps separate one web project’s libraries from another’s. Consider a learning scenario: an API-checking script works on a laptop but reports a missing module on the server. Installing the package again changes nothing because pip and the script use different interpreters. Instead of reinstalling packages at random, make the environment explicit: choose Python, create a separate dependency directory and check where the program runs from.
The practical workflow below is for Linux or macOS with bash or zsh. Python 3 must already be installed, and the user needs write access to the project directory. Make all changes in a local or staging copy first. This is not a guide to reconfiguring system Python or an active service on a production server.
Define the environment’s boundaries first
Open a terminal at the root of the intended repository. Check the current directory, interpreter version and availability of the venv module. These commands do not install anything:
pwd
python3 --version
python3 -m venv --help
Compare the reported version with the project’s requirements, not your idea of the “latest Python”. If the command cannot be found or the module is unavailable, stop and establish how Python should be installed for your operating system. Do not copy a package-manager command from a guide for another distribution. On managed hosting, installing components may require an administrator’s help.
The .venv directory is for the environment’s interpreter and libraries, not your source code, user uploads or secrets. Make sure the directory does not already exist. If it does, establish how the team uses it first; do not overwrite a working environment at random. The isolation principle is described in the Python venv documentation.
Create .venv and check the interpreter
At the root of the staging project, create a new environment and then address its Python directly:
python3 -m venv .venv
.venv/bin/python -c "import sys; print(sys.executable); print(sys.prefix != sys.base_prefix)"
.venv/bin/python -m pip --version
The first command creates .venv. The second shows the interpreter’s path and whether it is running inside a virtual environment: True is expected for this invocation. The third helps confirm that pip belongs to the same environment. These are expected verification signals, not measurements from your server.
Activation is convenient for interactive work but unnecessary when using an explicit path. That is why these examples do not depend on the state of an open terminal. Windows uses a different structure: the interpreter is normally at .venv\Scripts\python.exe. Adapt Unix paths before using them there. Also, venv isolates Python packages; it does not replace a container, system libraries or file-access restrictions.
Install only the dependencies you need
If the repository already has a reviewed requirements.txt, read it before installation. The file may contain download sources as well as package names. Installing dependencies can execute build procedures, so use only a trusted project and approved sources. From the project’s root, run:
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python -m pip check
The first command changes libraries in the newly created environment. The second checks the compatibility of installed dependencies; its purpose is explained in the pip check reference. A successful dependency check does not establish that the API, authentication or business logic works correctly: application tests are still needed afterwards.
If no dependency file exists, do not create one by copying a random library list from another project. First identify the imports the script actually needs. Record compatible dependencies using the team’s accepted method. Step-by-step installation options are available in the Python Packaging User Guide.
Do not use sudo pip to bypass an access problem. Likewise, do not disable system-environment protection merely because the installer refused to modify it. In this workflow, packages belong to the project, not the entire operating system. A write error is a reason to check directory ownership and the invocation location, not to grant everyone unrestricted permissions.
Make subsequent runs predictable
Add .venv/ to the root .gitignore without removing existing rules. The environment directory itself should not be committed to Git or copied to another server. Before committing, use the Git commit review workflow to make sure secrets and local housekeeping files have not entered the changes.
To inspect the packages actually installed, you can save a separate snapshot. Choose a new filename: redirection with > overwrites an existing file.
.venv/bin/python -m pip freeze > requirements.snapshot.txt
This is a record of the current state, not an automatic guarantee of a reproducible build. It can include unnecessary libraries or local references. Review the snapshot before publishing it, and do not replace the project’s main file with it without checking. The distinction between a snapshot and a dependency definition is explained in the pip freeze documentation.
Finally, perform the repository’s accepted test run specifically through .venv/bin/python. If the shell uses the correct environment but the scheduler or service does not, separately check its Python path, working directory and environment variables. Keep secrets out of command lines and public logs. After dependency changes, retain the previous verified configuration for rollback, and do not update a live service without testing.
It is useful to leave a short startup checklist in the README: the required Python version, dependency file, environment creation method and application verification command. Ask a colleague to follow it on a clean staging copy. If the instructions require verbal explanations or hidden local files, improve them before the next release. Separately identify which settings come from secret storage: explain the variables’ purpose, but do not put their values in documentation.
In conclusion, reliable execution starts not with another pip install but with the question “which Python runs this code?”. An explicit interpreter, reviewed dependencies and a test run make errors easier to understand. If you need to organise a web project’s scripts or integrations, describe the task to AriZone so we can establish a safe diagnostic sequence together.
Leave a comment