Here is how to create a virtual environment in Python.


On Windows:

Open Command Prompt (CMD), make sure the CMD is set to the folder where your project is, and execute:
py -3 -m venv env

That will create folder named env. To activate the environment execute:
env\Scripts\activate.bat

To use the Python interpreter of your virtual environment execute:
python


On Mac and Linux:

Open Terminal, make sure the terminal is set to the folder where your project is, and execute:
python3. -m venv env

That will create a folder named env. To activate the environment execute:
source env/bin/activate

To use the Python interpreter of your virtual environment execute:
python



Output

On Windows:



On Mac and Linux:



Explanation
The above code uses the Python venv standard library. In the above example, venv will create an env folder in the current working directory of the terminal/cmd. The  env folder contains a new Python installation/interpreter. That interpreter can be invoked by using the python command after having activated the virtual environment.


Related HowTos
Deploy a web app to Heroku
Schedule a Python script for execution at a specific time every day
Store Python passwords securely on Windows, Mac, and Linux
Do dictionary comprehension
Do list comprehension
Create a new file
Merge two lists
Extract items from two different lists into one list
Check if a text file is empty
Randomly select an item from a list
Generate a random integer
Break a while loop
Create a pandas DataFrame from a dictionary
Create a pandas DataFrame from a list
Get the last item of a list
Delete a column from a pandas dataframe
Access a column of a pandas dataframe
Create a class
Make a webpage request
Get the first two characters of a string
Loop through two lists at the same time
Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.


Mailing List

We will keep you Python-motivated if you subscribe below.