Python

Python venv

Python virtual environment commands for isolated development.

24 commands
Windows MacOS Linux
#virtual-environment #python

Create & Activate

Create a virtual environment
python -m venv myenv
Activate on Linux or macOS
source myenv/bin/activate
Activate on Windows
myenv\Scripts\activate
Deactivate the current environment
deactivate
Recreate environment from scratch
python -m venv --clear myenv
Create without pip installed
python -m venv --without-pip myenv

Package Management

Install a package in the venv
pip install <package>
Save venv packages to file
pip freeze > requirements.txt
Install packages from file
pip install -r requirements.txt
List packages installed in the venv
pip list
Upgrade pip inside the venv
pip install --upgrade pip

Environment Info

Show path to active Python binary
which python
Show Python version in the venv
python --version
Show pip version and install location
pip --version
Print the venv path
python -c "import sys; print(sys.prefix)"
Display active venv directory
echo $VIRTUAL_ENV

Best Practices

Use .venv as convention for project env
python -m venv .venv
Exclude venv from version control
echo ".venv/" >> .gitignore
Install pip-tools for dependency pinning
pip install pip-tools
Generate pinned requirements file
pip-compile requirements.in
Sync venv with requirements exactly
pip-sync requirements.txt

Quick Commands

Create a new virtual environment
python -m venv myenv
Activate the virtual environment on Linux or macOS
source myenv/bin/activate
Deactivate the current virtual environment
deactivate