Using venv in Python – Python virtual environments

andreasBlog3 weeks ago25 Views

Using venv in Python

A virtual environment is an isolated workspace for your Python projects.
It lets you install and manage packages without affecting your system-wide Python installation — perfect for avoiding dependency conflicts.


1. Create a Virtual Environment

From your project folder, run:

python -m venv venv

Here:

  • python is your Python interpreter
  • -m venv tells Python to use the built-in virtual environment module
  • venv is the folder name where the environment will be stored (you can call it anything)

2. Activate the Virtual Environment

Windows:

venv\Scripts\activate

macOS/Linux:

source venv/bin/activate

You’ll notice (venv) appears in your terminal prompt — meaning it’s active.


3. Install Packages Inside venv

Once activated, you can install packages just for this project:

pip install requests

These will be stored inside the venv folder, not globally.


4. Deactivate the Virtual Environment

To exit:

deactivate

5. Recreate an Environment from Requirements

If someone shares a project with a requirements.txt file:

python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
pip install -r requirements.txt

Tip: Always add venv/ to your .gitignore file so it’s not uploaded to version control.

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Loading Next Post...
Loading

Signing-in 3 seconds...