Windows venv setup for Python

Posted by Daksh on Sunday, January 23, 2022

Windows Python venv setup

One time setup (Optional)

Required only if the script is unsigned

In the start menu, right click on “Windows PowerShell”. Select “Run as an administrator” In the PowerShell terminal window, that opens, type: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned Then, type Y for Yes.

Checking for Python 3.11 (or any newer version)

Using the same PowerShell window from earlier, type: py -3.11

This should open a REPL window with a prompt. Press Ctrl + Z followed by Enter to exit this screen and go back to your prompt.

If py -3.11 throws an errror, then visit https://www.python.org/ to download the required version.

Using the same PowerShell terminal from earlier, type the following commands in one by one:

> cd $home
> mkdir pythonProject
> cd pythonProject
> py -3 -m venv env
> env\scripts\activate

Your prompt should now look like this, but with your own username.

(env) PS C:\Users\Daksh\pythonProject>

Setting up VsCode

# activate the virtual environment
> env\scripts\activate

# make a new empty python file called project.py
(env)> fc > project.py
# Open VS Code using the current directory as a project.
(env) $ code .

Run a python file from terminal in virtual enviornment

(env) -> python name_of_file.py