Skip to main content

Python Virtual Environment

Imagine giving every Python project its own private workspace on your computer. That is a virtual environment: an isolated place to run and test a project.

Each project's dependencies stay inside it, so they cannot get in the way of other projects or of your main Python installation.

You can picture each environment as a separate box. Every box:

  • Comes with its own Python interpreter
  • Holds its own collection of installed packages
  • Stays cut off from all other virtual environments
  • May use a different version of a package than another box

Why bother? Virtual environments:

  • Stop projects from clashing over package versions
  • Make a project easier to move and to reproduce
  • Leave your system-wide Python installation tidy
  • Let you test against several Python versions

Creating a Virtual Environment

Python already includes the tool you need for this: the built-in venv module.

To create an environment, open a command prompt, go to the folder that should hold your project, and enter this command:

Python then builds the environment for you, in a new "myfirstproject" folder filled with subfolders and files.

main.py
python -m venv myfirstproject

Activate Virtual Environment

An environment does nothing until it is activated, which is what this command does:

Once it runs, your prompt changes to tell you which environment you are now working in.

main.py
# Windows:
myfirstproject\Scripts\activate

# macOS/Linux:
source myfirstproject/bin/activate

Install Packages

While the environment is active, you can use pip to install packages directly into it.

Let's try that with a package named 'cowsay':

main.py
pip install cowsay

Using Package

With 'cowsay' now available inside the environment, let's make it draw a talking cow.

Start by creating a file named test.py. Any location on your computer works; this lesson keeps it next to the myfirstproject folder, not inside it.

Open the file and add the lines below:

Then, with the environment still active, run the file:

Example

The contents of test.py:

Prompt
Write a Python program that uses the cowsay package. Import cowsay and use cowsay.cow() to display "Good Mooooorning!".
Output: complete Python program
main.py
python test.py

Deactivate Virtual Environment

When you are finished working, this command deactivates the environment:

You are then back at your regular command line.

Running test.py from here, outside the environment, fails with an error, because 'cowsay' exists only inside the virtual environment.

Note: Deactivating does not remove myfirstproject. Activate it again and test.py will run and show the diagram once more.

main.py
deactivate

Delete Virtual Environment

Virtual environments are also easy to get rid of, if for some reason you no longer want one. No other project relies on one, so deleting it removes only the modules and files that live inside it.

To remove an environment, all it takes is deleting the folder and everything in it, either directly in your file system or from the command line:

main.py
# Windows:
rmdir /s /q myfirstproject

# macOS/Linux:
rm -rf myfirstproject