Use a Python virtual environment (venv)

There are a lot of options to isolate your Python environment so you don’t affect all your system packages while just playing around with tutorials online. Feel free to use your own if you have a favorite already, but for those that don’t, keep reading!

We will stick to the simplest and built-in method of using the provided venv module. This creates a new directory in the folder it has been run in, and becomes a clean and disposable place to install random packages.

Open a terminal and navigate to a directory you are comfortable adding another folder in that you can play around with. Then run the command to create a new virtual env directory python -m venv <new venv dir> I usually just call mine ‘venv’ because I am so original.

python -m venv venv

Now you need to activate it. That will replace the calls to python and pip and any other executable in that venv in the current terminal where it’s activated.

Linux

. ./venv/bin/activate

Windows Powershell

venv\Scripts\Activate.ps1

If you get an error that you don’t have permissions to run scripts, you may need to allow running scripts.

set-executionpolicy remotesigned

Then try running venv\Scripts\Activate.ps1 again.

Windows Command Prompt

venv\Scripts\activate.bat

Now in your terminal the lines should start with (venv) and you’re free to install whatever crazy packages you want to try out!