Python Virtual Environments with venvSee Python: Tips and Tricks for similar articles.
In this tutorial, we will be running some commands at the prompt. The prompt text varies by operating system, terminal type, and settings.
We will be using a
$. Your prompt text will likely be different.
$ sample command
A virtual environment is a self-contained directory tree that contains a Python interpreter and all the additional non-standard Python packages and modules required for your project.
The major advantage of using virtual environments is that you can set up each project with the specific packages and modules it requires. Another big advantage is that you can easily share that setup with other developers so they can quickly make exact copies of the development environment of your project.
For this tutorial, we are assuming you’re using Visual Studio Code, but you should be able to follow along with any IDE (or no IDE at all).
Create the Virtual Environment
- Create a new folder to host your project. Give it a name that makes sense for your project.
- Open Visual Studio Code and open the folder you just created.
- Open a terminal by selecting Terminal > New Terminal or pressing Ctrl + `.
- At the terminal, run the following command:
$ python -m venv .venvYou can name this directory whatever want, but it is commonly called “.venv” (notice the prepended dot) to indicate that it is a special directory for holding a virtual environment. This will create and populate a new .venv directory. The contents of that directory will differ by operating system:
- Scripts – Contains the Python executable.
- bin – Contains the Python executable.
- To work within your virtual environment, you must first activate it. The command for activating a virtual environment
varies by operating system. Run one of the following:
$ source .venv/bin/activate
- When the virtual environment is activated, its name will always appear enclosed in parentheses before the prompt:
(.venv) $If you don’t see the virtual environment name in parentheses before the prompt, you are not in the virtual environment.
- You can now invoke the Python interpreter and/or install additional packages (using
pip) within the virtual environment. Because the
$PATHenvironment variable is modified when a virtual enviroment is activated, the executable files are resolved within the virtual environment. Let’s take a look at the value of this environment variable. Run the following command for your environment:
Windows: Command Prompt
$ echo %PATH%
Mac / Linux
$ echo $PATHYou should see the path of the Scripts (Windows) or bin (Mac) directory prepended to the list of paths:
(.venv) $ $Env:PATH C:\Webucator\tutorials\.venv\Scripts;C:…Because it is at the beginning of
$PATH, the Scripts or bin directory will be scanned first to resolve references to executable files. Once you deactivate the virtual environment, the Scripts and bin directory will be removed from
- Now, you can install any packages or modules you need for your project. For example, if you want to install Django, just run:
(.venv) $ pip install djangoAfter it finishes installing, you will see Django, along with some of its dependencies, listed in the .venv/lib directory:
- To deactivate (exit) the virtual environment, run:
(.venv) $ deactivateNow, you are back in the original prompt.
- When you no longer need a virtual environment, you can delete it by using operating system commands to delete the folder (e.g., .venv) that was automatically built when you created the virtual environment.
For those of you who prefer videos, I do a quick walk through of creating a virtual environment in the video below. Check it out. And also, check out our Python courses here.
- Scientific Notation in Python
- Understanding Python’s __main__ variable
- Associate Python Files with IDLE
- Python: isdigit() vs. isdecimal()
- Python Color Constants Module
- Python: pow(x, y, z) less efficient than x**y % z
- A Python Model for Ping Pong Matches
- Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
- Collatz Conjecture in Python
- Finally, a use case for finally – Python Exception Handling
- Python Clocks Explained
- Python’s date.strftime() slower than str(), split, unpack, and concatenate?
- Bi-directional Dictionary in Python
- Maximum recursion depth exceeded while calling a Python object
- Basic Python Programming Exercise: A Penny Doubled Every Day
- Creating an Email Decorator with Python and AWS
- How to Create a Simple Simulation in Python – Numeric Data
- Python Coding Challenge: Two People with the Same Birthday
- How to find all your Python installations on Windows
- Change Default autosave Interval in JupyterLab
- Interactive Quiz using IPython Notebook
- When to use Static Methods in Python? Never
- Converting Leading Tabs to Spaces with Python
- Simple Python Script for Extracting Text from an SRT File
- Python Virtual Environments with venv (this article)
- Mapping python to Python 3 on Your Mac
- How to Make IDLE the Default Editor for Python Files on Windows
- How to Do Ternary Operator Assignment in Python
- How to Convert Seconds to Years with Python
- How to Create a Python Package
- How to Read a File with Python
- How to Check the Operating System with Python
- How to Use enumerate() to Print a Numbered List in Python
- How to Repeatedly Append to a String in Python
- Checking your Sitemap for Broken Links with Python
- How to do Simultaneous Assignment in Python
- Visual Studio Code - Opening Files with Python open()
- How to Slice Strings in Python
- How Python Finds Imported Modules
- How to Merge Dictionaries in Python
- How to Index Strings in Python
- How to Create a Tuple in Python