Here’s an guide that includes instructions for installing the venv module and creating virtual environments on Mac, Linux, and Windows:
Prerequisites:
- Python 3.x installed on your system. To check, run:
or
python3 --versionpython --version
Step 1: Install venv (if not already installed)
Mac & Linux:
-
If you are using a Linux distribution or macOS, you may need to install
venvusing your package manager:For Debian/Ubuntu:
sudo apt-get install python3-venvFor Fedora:
sudo dnf install python3-venvFor macOS with Homebrew: Python 3 typically comes with
venv, but if not, you can reinstall it:brew install python3
Windows:
- If Python 3 is installed,
venvshould be available by default. No additional steps are required.
Step 2: Create a Virtual Environment
Mac & Linux:
-
Navigate to the desired directory where you want to create your virtual environment:
cd /path/to/your/project -
Create the virtual environment using
venv:python3 -m venv venv_nameReplace
venv_namewith the name you want for your virtual environment. -
Activate the virtual environment:
source venv_name/bin/activate -
Deactivate the virtual environment when you're done:
deactivate
Windows (Command Prompt):
-
Navigate to the desired directory where you want to create your virtual environment:
cd path\to\your\project -
Create the virtual environment using
venv:python -m venv venv_name -
Activate the virtual environment:
venv_name\Scripts\activate -
Deactivate the virtual environment when you're done:
deactivate
Windows (PowerShell):
-
Navigate to the desired directory where you want to create your virtual environment:
cd path\to\your\project -
Create the virtual environment using
venv:python -m venv venv_name -
Activate the virtual environment:
.\venv_name\Scripts\ActivateIf activation fails, you might need to change the execution policy:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -
Deactivate the virtual environment when you're done:
deactivate
Notes:
- Replace
venv_namewith the desired name for your virtual environment. - After activating the virtual environment, any Python packages you install (e.g., using
pip) will be isolated to that environment. - To install packages, use:
pip install package_name