How to create a python virtual environment for windows

Example of Virtual Environment folder

You will need VScode to make edits to python scripts, so install that on your windows machine.

Screenshot of VScode

In VScode, open up the terminal along the top, you should see boxes appear along the bottom

Screenshot of VScode Terminal

if you haven’t done this yet you need it for python virtual environments.

https://www.python.org/downloads

python install
installationprocess

make sure you select pip in the install wizard.

pip stands for “pip Install Packages”, not very creative.

pip is a tool/software that allows python and your computer to download and access the “Python Package Index”!!!

You can think of it as a library of tools, you can check in and out. Mostly checking out software packages by installing them to your machine and using them with python.

in your terminal type in pip install virtualenv.

if you get pip not found follow this guide.

pip install virtualenv
python -m venv example

Why? create virtual environments? and why is the first thing I teach students?

Well imagine every time you check out a book of library it could be a different Edition of that book.

In software we have different versions of software they get updated over time by other coders. For example we had

  • Windows 7
  • Windows 8
  • Windows 9x series (back in the 90s) its just bad branding
  • Windows 10
  • Windows 11

Every release comes with bug fixes, new features, and security updates.

It also a way for software companies to make money through licenses.

Now imagine these books you check out can magically work together or even built onto one another into new books. This is called interdependence!!! or commonly known as software dependency.

Now the reason to have a virtual environment is to ensure that software packages you install are compatible.

See if one of the books are broken or out of date with the latest version of windows OS, python version 3.11 or another package it breaks your code.

Virtual environments are just a way to install and uninstall software packages confined to their “environment” instead effecting the entire operating system environment.

Example of creating venv

first I would always list out the folders in the terminal by typing ls and hit enter.

List of items in a folder directory

this will make sure you are in the right directory level to access and run the virtual environment.

to enter the virtual environment

.\example\Scripts\activate

this is running the activate.bat script contain under the virtual environment folder

Example of Virtual Environment folder

You should see name of your virtual environment in parentheses. (example) or (venv) is a common name.

This indicates you are in the virtual environment.

Inside of virtual environment

now you have ability to contain your installing software and reduce package conflict inside your virtual environment.

installing numpy package

you can save your specific environment with

pip freeze > requirements.txt

this how you share your install list with ither software engineers to run and test your code.

Simply type into the terminal “deactivate” and the parentheses should disappear stating you are out of python virtual environment.

deactivate
Deactivate

If you are interested in more tutorials, please subscribe below to our newsletter.

You will get articles on code, design, and investing.

Discover more from lesliemwubbel

Subscribe now to keep reading and get access to the full archive.

Continue reading