Home Coding & Scripting Virtual Environment in Python and How to Set One Up

Virtual Environment in Python and How to Set One Up

378
0

Python is one of the most popular programming languages in the world—but if you’ve ever run into dependency conflicts, you know it can get messy fast. One project wants version 1.0 of a library, another needs 2.0, and suddenly your system Python is a battlefield.

That’s where virtual environments (venvs) come in. They let you isolate dependencies per project so you don’t step on your own toes.


🔹 What Is a Virtual Environment?

A virtual environment is a self-contained folder that holds its own Python interpreter and installed packages.
Think of it as a sandbox: anything you install stays inside, away from the global Python setup on your computer.


🔹 Why Use One?

  1. No dependency conflicts: Different projects can use different versions of the same library.
  2. Keep system Python clean: Avoid breaking tools that rely on global Python.
  3. Easy collaboration: Share your requirements.txt, and anyone can recreate your exact environment.
  4. Quick reset: If something breaks, just delete the environment folder and start fresh.

🔹 Setting Up a Virtual Environment

🧰 Prerequisites

Before diving into virtual environments, make sure you’ve completed the setup steps in the study note Getting Started with Python.

  • Python installed on your local machine (you should be able to run python --version or python3 --version).
  • We need a project folder (e.g. hello-python) on your system.
    💡Tip For Windows: To Open PowerShell (or Command Prompt) Directly in Your Project Folder, in File Explorer, navigate to your project folder, right-click on the folder background (not on a file), choose “Open in Terminal” from the context menu, and a Terminal window opens directly at that folder path, ready for you to run Python or venv commands. In older windows operating systems, you will need to press SHIFT command before right clicking.
    Important! If you are following this note as a prerequisite for another note, then you may name the project folder as per that note. You should create a different project folder for every project you work with.

Once those are in your project folder, you’re ready to move on and create your first virtual environment.

1. Create a venv

Run one of the following commands based on the operating system.

# macOS/Linux
python3 -m venv .venv

# Windows (PowerShell)
python -m venv .venv

This creates a folder called .venv inside your project.


2. Activate the venv

For Windows (on PowerShell), we can follow the below process.

When we create a Python virtual environment (venv), it generates an activation script (.venv\Scripts\Activate.ps1). By default, PowerShell blocks unsigned scripts, so attempting to run this file may result in a security error. To allow locally created scripts like this to run, update the execution policy for your current user before activating the venv. Below command is needed only once in your system.

# Windows (PowerShell)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Now activate the venv using the following command.

# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1

You’ll know it’s active because your terminal prompt will show (.venv) at the start.

Important Notes!

  • The Set-ExecutionPolicy command needs to be executed only once for a windows machine. You not have to do it for every project folder.
  • Creating a virtual environment needs to be done for every project folder. When we do this a .venv folder is created. Confirm this by checking if the .venv folder is present.
  • Activate it every time when you want to work on virtual environment. Confirm this by checking if (.venv) is shown before your command.
  • To activate on Windows (CMD), use the command .\.venv\Scripts\activate.bat

For macOS / Linux, you can execute below command.

# macOS/Linux
source .venv/bin/activate

You’ll know it’s active because your terminal prompt will show (.venv) at the start.


3. Installing AND Uninstalling packages inside the venv

Important! If you are following this note as a prerequisite for another note, then you may skip installing below packages and install the ones needed for your note as mentioned.

We can install a package inside a venv as follows (make sure the terminal prompt shows (.venv) at the start denoting that you are in the virtual environment):

pip install flask

Now these libraries are installed only for this project, not globally.

We can uninstall the package inside a venv as:

pip uninstall flask

You will need to confirm the uninstall.

Similar to how we installed flask, install numpy and pandas using following commands:

pip install numpy
pip install pandas

Now these libraries are installed only for this project, not globally.


4. Freeze dependencies into a requirements file

pip freeze > pyforai-requirements.txt

This captures all installed packages and versions. You can share it with others including team mates so that they can install the same dependencies in their environment.


5. Optional: Recreate the environment elsewhere (E.g. Team Mate)

On the other machine, setup virtual environment following steps until 2. Then copy the pyforai-requirements.txt into from your machine to that machine and run:

pip install -r pyforai-requirements.txt

Now you have the exact same environment.


🔹 Bonus: Deactivate the venv

When you’re done working:

deactivate

Your terminal goes back to normal, and the environment is safely tucked away.

So, the next time, just activate the virtual environment and work on the project.


✅ Final Thoughts

Using a virtual environment is one of those small habits that saves you endless headaches. It keeps your projects clean, portable, and reliable—whether you’re coding solo or collaborating with a team.

So, the next time, just activate start the virtual environment.

Previous articleGetting Started with Python
Next articlePython Variables, Data Types, and Operators
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here