متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 95Build · Python

How to create a Python virtual environment

Installing Python packages with sudo pip install mixes them with the system's own packages — and breaks dnf, apt or other tools. New Ubuntu and Debian even refuse it (externally-managed-environment). The clean way is a virtual environment (venv) per app or script: its own Python packages, its own versions, easy to delete. This how-to creates one, pins the packages, uses it from systemd and cron, and shows the faster uv tool.

  • 5 الدروس
  • 7 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

95.1

The quick answer

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

On Ubuntu/Debian install the venv module first: apt-get install -y python3-venv.

الأوامر في هذا الدرس

95.2

Step by step

  1. Choose the Python version — the system one, or a newer one from the distro:
python3 --version
dnf -y install python3.12
python3.12 -m venv /opt/shop/.venv
  1. Create and use the venv (quick answer). After source .venv/bin/activate the commands python and pip are the venv's (the prompt shows (.venv)). Activation only changes PATH for this shell — you can also call the venv's Python directly, without activating:
/opt/shop/.venv/bin/python -m pip install --upgrade pip
/opt/shop/.venv/bin/pip install -r /opt/shop/requirements.txt
  1. Pin your packages so every server gets the same versions:
pip install flask gunicorn psycopg[binary]
pip freeze > requirements.txt
head -3 requirements.txt
blinker==1.9.0
click==8.1.8
flask==3.1.0

Commit requirements.txt, not the .venv folder (add .venv/ to .gitignore).

  1. Use it in a systemd service or cron job — always the full path to the venv's binaries:
/etc/systemd/system/shop-api.service (part)
[Service]
User=shop
WorkingDirectory=/opt/shop
ExecStart=/opt/shop/.venv/bin/gunicorn --bind 127.0.0.1:8000 app:app
/etc/cron.d/shop-cleanup
15 3 * * * shop /opt/shop/.venv/bin/python /opt/shop/jobs/cleanup.py
  1. Command-line tools you want everywhere (ansible-lint, yamllint, httpie) → pipx gives each its own venv:
pipx install yamllint
  1. uv — a much faster replacement for pip and venv (same files, same idea):
uv venv .venv
uv pip install -r requirements.txt
  1. Rebuild instead of repair: a broken venv is deleted and created again from requirements.txt — that is the point of it.

95.3

Check that it worked

which python
/opt/shop/.venv/bin/python
pip list | head
python -c 'import flask; print(flask.__version__)'

The packages are inside .venv, and the system Python is unchanged.

95.4

Common problems

ProblemCauseFix
error: externally-managed-environmentNew Debian/Ubuntu protect the system Python (PEP 668).Use a venv (or pipx). Do not use --break-system-packages.
The virtual environment was not created successfully because ensurepip is not availablepython3-venv missing.apt-get install python3-venv.
ModuleNotFoundError in the serviceThe service uses the system Python.ExecStart with /opt/app/.venv/bin/….
The venv breaks after a Python upgradeThe venv points to the old Python binary.Delete and recreate it from requirements.txt.
Build errors for packages like psycopg2Needs compilers and headers.Use binary wheels (psycopg[binary]) or install gcc and *-devel packages.
The .venv folder ended up in GitNot ignored.Add .venv/ to .gitignore; remove it from the repo.

95.5

Go further

1Why not install packages with sudo pip install?

They mix with the system's packages and can break system tools; new distros even refuse it.

2How does a systemd service use a venv?

ExecStart points to the venv's binary, for example /opt/shop/.venv/bin/gunicorn.

كل الأوامر في هذا الفصل