Skip to content

Creating python virtual environment

Illustration for Python Virtual Environment

A virtual environment is an isolated workspace for your Python projects.
It helps you manage dependencies for each project separately — so your global Python setup stays clean.

Step 1: Open Command Prompt or PowerShell

Navigate to your project folder using:

cd <project_directory>

(Replace <project_directory> with your actual project path, for example:)

cd C:\Users\YourName\projects\apiproject

Step 2: Create a Virtual Environment

Run this command:

python -m venv venv

✅ This will create a folder named venv inside your project directory, containing the isolated Python environment.

Step 3: Activate the Virtual Environment

Now activate it:

venv\Scripts\activate

Once activated, you’ll notice your terminal prompt changes to something like this:

(venv) PS C:\Users\YourName\projects\apiproject>

This means your virtual environment is active!
Any Python packages you install now will only apply inside this project.

Step 4: Fix “UnauthorizedAccess” or “PSSecurityException” Error

If you see an error like this:

+ CategoryInfo          : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess

It means PowerShell is blocking script execution.

To fix it, run this command (once):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Then try activating again:

venv\Scripts\activate

Step 5: Deactivate When You’re Done

When you finish working in the environment, simply deactivate it:

deactivate

Check it is working

With the environment active, run these commands. The paths should point inside your project’s venv folder:

python --version
python -m pip --version
where python

On Windows, where python lists every Python it finds; the first one should be inside venv\Scripts. On macOS or Linux, use which python instead.

On macOS or Linux

python3 -m venv venv
source venv/bin/activate
deactivate

Save and reinstall your packages

pip freeze > requirements.txt
pip install -r requirements.txt
Share

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

One short email when a new calculator or article goes live. No spam, and you can unsubscribe in one click.