loading...
Cover image for Setting up a python virtual environment on Windows

Setting up a python virtual environment on Windows

towernter profile image Tawanda Nyahuye ・1 min read

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
.\Scripts\activate

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

python script.py

python name-of-your-script.py in this case my script is sript.py

Discussion

pic
Editor guide