how to create virtual environment in python
Here’s how to create and use a Python virtual environment in a clean, step‑by‑step way, plus a bit of context to make it “Quick Scoop” friendly.
How to Create Virtual Environment in Python
A virtual environment is a self-contained folder that has its own Python interpreter and its own installed packages, separate from your system Python.
Mini Section: Why Virtual Environments Matter
- They prevent version conflicts between projects (one app can use Django 3, another Django 5, etc.).
- They keep your global Python installation clean and stable.
- They make it easier to ship or deploy projects, because requirements are isolated per project.
Think of a virtual environment like a dedicated sandbox for each project, where changing the sand in one box never messes up another.
Mini Section: The Modern Standard – venv
On current Python 3 versions, the recommended way is using the built-
invenv module, not third‑party tools.
1. Open a terminal / command prompt
- Windows: Command Prompt or PowerShell
- macOS/Linux: Terminal app
2. Go to your project folder
bash
cd path/to/your/project
3. Create the virtual environment
Use python -m venv followed by your env name (common names: venv, .venv,
env).
bash
python -m venv venv
- This creates a folder named
venvinside your project. - Inside it you’ll see subfolders like
Lib(orlib),Scripts(orbin), and apyvenv.cfgconfig file.
Mini Section: How to Activate the Environment
Activation changes your shell so that when you type python or pip, they
point to the virtual environment instead of the system Python.
On Windows (Command Prompt)
bash
venv\Scripts\activate.bat
or in PowerShell:
powershell
venv\Scripts\Activate.ps1
On macOS / Linux
bash
source venv/bin/activate
After activation, your prompt usually shows the environment name in parentheses, for example:
text
(venv) C:\Users\You\project>
Now any pip install ... goes into this venv only.
Mini Section: Installing Packages Inside the venv
Once your environment is active:
bash
pip install requests
- The
requestspackage (or whatever you install) is stored inside thevenvfolder. - Your system Python remains untouched.
You can also generate a requirements.txt later:
bash
pip freeze > requirements.txt
And reinstall all requirements on another machine/env with:
bash
pip install -r requirements.txt
Mini Section: Deactivating and Reusing the venv
When you’re done working:
bash
deactivate
- This returns your shell to the normal system Python.
- The environment still exists on disk; you just reactivate it next time.
To use it later, go back to your project folder and activate again:
-
Windows:
bash venv\Scripts\activate -
macOS/Linux:
bash source venv/bin/activate
Mini Section: venv vs virtualenv (Forum-Style Angle)
In many forum discussions from the last few years, you’ll see people compare two main options:
venv- Built into Python 3.3+.
* No extra install needed.
* Recommended for most modern setups.
virtualenv- Older, third‑party package you install with
pip install virtualenv.
- Older, third‑party package you install with
* Historically used for Python 2 and early Python 3.
* Still useful in some advanced or legacy setups.
In 2026, for a normal “How do I set this up?” question, guides, tutorials, and
Q&A threads typically point beginners to python -m venv as the default
choice.
Here’s a quick HTML table that fits your post’s rules:
html
<table>
<tr>
<th>Method</th>
<th>How to create</th>
<th>When to use</th>
</tr>
<tr>
<td>venv (built-in)</td>
<td><code>python -m venv venv</code>[web:3][web:5]</td>
<td>Default for Python 3 projects, simple and included by default.[web:7][web:6]</td>
</tr>
<tr>
<td>virtualenv (package)</td>
<td><code>pip install virtualenv</code> then <code>virtualenv my_env</code>[web:1][web:2]</td>
<td>Legacy projects, special workflows, or when specifically required.[web:2][web:1]</td>
</tr>
</table>
Mini Section: Example “Story” Flow (Like You’d See in a Blog)
Imagine you’ve just cloned a GitHub project that has a requirements.txt
file:
-
You open a terminal in the project folder.
-
You create a virtual environment:
bash python -m venv .venv -
You activate it:
- Windows:
.venv\Scripts\activate - macOS/Linux:
source .venv/bin/activate
- Windows:
-
You install everything the project needs:
bash pip install -r requirements.txt -
You run the app, confident it’s not messing with other Python projects.
That’s the typical, modern workflow people describe in tutorials and forum threads when they talk about “setting up a project environment.”
Quick TL;DR (for your “Quick Scoop” section)
- Use
python -m venv venvin your project directory to create a virtual environment.
- Activate it:
- Windows:
venv\Scripts\activate - macOS/Linux:
source venv/bin/activate
- Windows:
- Install packages with
pip install ...and, if needed, export them withpip freeze > requirements.txt.
- Use
deactivateto exit the virtual environment when you’re done.
Information gathered from public forums or data available on the internet and portrayed here.