PowerShell and Creating Python Virtual Environments
- Bence Danko
- Jul 25, 2021
- 2 min read

It only recently came to my attention what the function of environments actually are, rather then being an option I blaze through in IDE setups. Python actually comes set with its own virtual environment manager, venv, and it actually lead me into my first experience with the Command Prompt as well as cmdlets.
Firstly, its important to know that PowerShell defaults to restricting the execution of scripts. We can run PowerShell as an admin (Win + x + a), and set the execution policy to something more lenient at our convenience. The options range from restricted to absolute freedom. I set mine to Remote Signed as to only allow officially signed scripts to run, but there are other options as well.
$ set-executionpolicy <remotesigned>This permanently sets the Execution Policy. Upon doing so, we're able to execute the venv scripts.
To create the virtual environment, we call on it with
$ py -m venv <env_name>to create it within whatever folder you're currently residing in. This by default involves the most up-to-date version of Python you have involved, so you'll have to access other Python directories manually if you want to use older versions.
In order to activate the environment, you can use
$ .\\<env_name>\Scripts\activate.ps1To access the specific PowerShell activate file. The .\ allows you to maintain the directory we're on and then execute the Script further in the directory.
Upon creating the environment, we can now install dependencies localized entirely within that environment. Typically, people place those dependencies within a requirements.txt file that you can automatically call on within PowerShell, using
$ pip install -r <requirements.txt>that uses PyPI to access the Python repositories.
With your environment activated and dependencies resolved, you can run your .py files using
$ py <filename>.pyand have any results appear in the terminal.
It was pretty cool to learn more about the Command Line and how programs work beyond the GUI. Beyond environments I learned some other cool shortcuts as well as how to manipulate text files using type.


Comments