Skip to main content
DARKPC.NET

Keep AI Project Dependencies in a Python Virtual Environment

Create a project-local Python environment, check which interpreter is active and rebuild it from a dependency record.

Difficulty Beginner

Prerequisites

Use Python 3 with venv and a Bourne-compatible shell on Linux. Internet access is needed to obtain the example package. A CPU and a writable project directory are sufficient. Work as your ordinary user.

Create and identify the environment

mkdir ai-environment-lab
cd ai-environment-lab
python3 -m venv .venv
. .venv/bin/activate
python -c 'import sys; print(sys.executable); print(sys.prefix != sys.base_prefix)'
python -m pip --version

Expect an executable path under .venv and True. Calling pip through python uses the same interpreter used for the check. You may invoke .venv/bin/python directly when running scheduled jobs or when activation is unavailable.

Install and record one dependency

python -m pip install numpy
python -c 'import numpy; print(numpy.__version__); print(numpy.mean([2, 4, 6]))'
python -m pip freeze > requirements.txt
deactivate

The mean should be 4.0. Save the Python version and operating system with the dependency record. Retain the text file alongside the project. Rebuild the environment on each machine because installed executables and binary wheels can be platform-specific.

Practise a rebuild

python3 -m venv .venv-rebuild
.venv-rebuild/bin/python -m pip install -r requirements.txt
.venv-rebuild/bin/python -m pip check
.venv-rebuild/bin/python -c 'import numpy; print(numpy.mean([2, 4, 6]))'

Compare both runs. If you have a different NumPy version, verify that each command used the intended interpreter. If venv cannot bootstrap pip, check the Python venv package for your distribution. A dependency record alone does not authenticate packages. Install only from the intended source, review dependency changes, and use pinned hashes where your team requires supply-chain verification. Never use an elevated pip command to fix a project environment.

References and related reading