Creating a Virtual Environment in Python (Venv)

Опубликовано: 22 Март 2025
на канале: Tommy Tech
86
4

A Python virtual environment is a way to create isolated Python environments, separate from your system's global Python installation. This allows you to install packages and dependencies for a specific project without affecting other projects or the global Python installation.

To create a virtual environment, you can use the "venv" package. Once venv is installed, you can create a new virtual environment by running the command "venv [environment_name]" in your terminal. This will create a new directory with the specified name, containing a copy of the Python executable and the standard library.

Activating a virtual environment is done by running the command "source [environment_name]/bin/activate" on Linux/macOS or ".[environment_name]\Scripts\activate" on Windows. Once the environment is activated, any packages you install using pip will be installed in the virtual environment, rather than in the global Python installation.

To deactivate a virtual environment, you can run the command "deactivate". And you can also use "pip freeze" command to check the packages installed in your virtual environment.