DEV Community

Cover image for Setting up a python virtual environment on Windows
Tawanda Nyahuye
Tawanda Nyahuye

Posted on

Setting up a python virtual environment on Windows

Why create a virtual environment?

Alt Text
We create a virtual environment in order to isolate each Python project from other projects on our computer so that each project has its own dependencies.

Open your CMD and run the following command:

python -m venv c:\path\to\yourenvironment

Example: python -m venv c:\pythonprojects\myproject
This will create the following folders/files in your environment

Alt Text

Creating our python project

You can create your virtual environment inside an existing project, it will work the same

Create your python script in your virtual environment (in this case I created a virtual environment first). You can also create the script within a folder inside your virtual environment.
Alt Text

Running your project

If your script is inside a folder within your virtual environment, go inside the folder first using your CMD.

cd your-folder-name

Activate your virtual environment using your CMD while inside your virtual environment folder using the following commands

First, navigate to the folder with your virtual environment:

cd c:\path\to\yourenvironment

Mine looks like this: c:\pythonprojects\myproject>.\Scripts\activate
Run your script using the following command


python in this case my script is

Discussion (0)