Virtual environments and pip
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Real programs lean on packages other people wrote, and pip installs them from PyPI. But one Python has one site-packages folder, so two projects that need different versions clash. A virtual environment gives each project its own python and its own site-packages. Step through six commands: make a venv, activate it, install requests from a requirements.txt, run app.py, then leave the venv and watch the same file fail.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What does python3 -m venv .venv create?
venv makes a folder with a python that points back to the Python you ran it with, a pyvenv.cfg file, and its own site-packages. A fresh one holds only pip.
02You ran source .venv/bin/activate. What changed?
Activating only changes your current shell: .venv/bin goes first in PATH and the prompt shows (.venv). deactivate puts PATH back.
03requirements.txt contains the line requests==2.32.3. What does pip install -r requirements.txt do with it?
== pins an exact version. pip downloads requests 2.32.3 from PyPI and also installs what it depends on (here certifi, charset-normalizer, idna and urllib3).
04requests is installed in .venv. After deactivate, you run python3 app.py, and app.py starts with import requests. What happens?
A venv is isolated: the system Python never looks inside .venv, so (assuming the system Python has no requests of its own) the import fails. Activate the venv again and the import works.
05A teammate clones your project. What should the repository contain so they can run it?
The venv docs call environments disposable and not movable or copyable. Share the list, and rebuild the venv on each computer.
المسرد
- Package (distribution)
- Code someone published for others to install, such as requests. It can contain one or more importable modules.
- pip
- Python's package installer. It downloads packages, usually from PyPI, and installs them into a site-packages folder.
- PyPI
- The Python Package Index, pypi.org: the public collection of packages that pip installs from.
- site-packages
- The folder where a Python installation keeps the third-party packages installed into it. It is on sys.path, so import finds them.
- Virtual environment (venv)
- A folder with its own python and its own site-packages, isolated from the system Python's packages.
- Activate / deactivate
- Shell commands that put a venv's bin folder first in PATH (and add (.venv) to the prompt), and undo that again.
- requirements.txt
- A plain-text list of the packages a project needs, one per line; pip install -r reads it.
- Version pin (==)
- A requirement such as requests==2.32.3 that asks for exactly one version, so every install gets the same code.
الملاحظات والمصادر
- On macOS and Linux the command is often python3; inside an activated venv plain python works too. On Windows use py -m venv .venv to create one.
- pip install ... is the same as python -m pip install ... once the venv is active. The Python docs show python -m pip because it always uses the pip that belongs to the python you name.
- pip prints what it downloads while it works; the figure leaves that out. pip freeze prints every package in the venv with its exact version, a common way to write requirements.txt.
- Many systems (recent Debian and Ubuntu, Homebrew) refuse pip install into the system Python with error: externally-managed-environment. A venv is the intended fix.
- The last step assumes the system Python does not have requests. If it does, python3 app.py works there too. Isolation runs both ways: the system Python never looks inside .venv, and by default (include-system-site-packages = false) a venv does not see the system's packages.
- Since Python 3.13 venv writes a .gitignore inside .venv so Git skips the folder; many projects also list .venv in their own .gitignore.
- Python tutorial: Virtual environments and packages
- Python library: venv — creation of virtual environments
- Python docs: Installing Python modules
- Python library: site — site-specific configuration hook
- Python library: sys.prefix and sys.base_prefix
- Python tutorial: The module search path
- pip user guide: Requirements files
- pip reference: pip freeze
- PEP 668: Marking Python base environments as externally managed
- PyPI: requests 2.32.3
اختبار يتاح بعد الخطوة الأخيرة.