Here’s an guide that includes instructions for installing the venv module and creating virtual environments on Mac, Linux, and Windows:
- Python 3.x installed on your system. To check, run:
or
python3 --version
python --version
-
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-venv
For Fedora:
sudo dnf install python3-venv
For macOS with Homebrew: Python 3 typically comes with
venv, but if not, you can reinstall it:brew install python3
- If Python 3 is installed,
venvshould be available by default. No additional steps are required.
-
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_name
Replace
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
-
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
-
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
If activation fails, you might need to change the execution policy:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
-
Deactivate the virtual environment when you're done:
deactivate
- 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