A virtual environment is an isolated Python environment that allows you to manage project-specific dependencies without interfering with your system's global Python installation.
This guide:
- Uses the built-in
venv
module - Creates a new subfolder in your project repository folder
- Names the new virtual environment folder
.venv
(pronounced as "dot venv")
Explore the .venv folder as you create, activate, and install additional Python packages into that folder.
To learn more about why this process (over many other possibilities), see WHY.md. Join the discussion at discussions.
We type these commands in a terminal. PowerShell Core
works well on all operating systems.
You may also use Terminal (Mac/Linux) or Windows PowerShell or Command Prompt (Windows).
The commands have been tested in PowerShell Core. Copy this repo and update as needed for your process.
Open your terminal and navigate to your project directory. Create a new local virtual environment named .venv
by typing the following command and hitting Enter (or Return):
python -m venv .venv
Verify the sub folder .venv
is created. You can replace .venv with any name you prefer.
We like the leading dot because it keeps environment files away from project code.
To use the virtual environment, you need to activate it using a command found in the new virtual environment. The activation process differs based on your operating system.
.venv\Scripts\activate
source .venv/bin/activate
Verify your terminal prompt changes, indicating that you are now working within your virtual environment.
With the virtual environment active, you can install the required packages for your project. What you install is specific to your project.
You can:
- Install and upgrade multiple packages in a single line
- Install packages one at a time
- Install all packages listed in a
requirements.txt
file using the-r
flag
Examples are shown below.
Verify your virtual environment appears in your terminal prompt before running installation commands. Let each command finish before running another.
python -m pip install --upgrade pip wheel
python -m pip install requests
python -m pip install pandas
python -m pip install -r requirements.txt
If you want to share your project with others or deploy it on a different system, it's a good practice to include the virtual environment details. This ensures that anyone working on your project has the same environment and dependencies.
To create a new requirements.txt with the packages in the virtual environment, run the following command:
pip freeze > requirements.txt
deactivate
These two commands create and activate a new virtual environment in PowerShell Core.
python -m venv .venv
.venv\Scripts\activate
- Practice creating, activating, deactivating virtual environments.
- Practice installing, uninstalling, and freezing requirements.
- Try different approaches. When a course uses
pip
, try pip. When a course usesconda
, try conda. - Be adaptable. Different teams do things differently, stay open to quickly learning new tools and methods.