Python Virtual Environment

๐Ÿ Python Virtual Environment

A virtual environment (venv) in Python is an isolated workspace where you can install packages specific to a project without affecting the global Python installation or other projects.


โœ… Why Use a Virtual Environment?

Without venv With venv
Packages install globally Packages isolated per project
Version conflicts may occur Each project can have different versions
Hard to manage dependencies Easy and clean dependency management

๐Ÿ›  Create a Virtual Environment

Step 1: Check Python version

python --version

or (on some systems)

python3 --version

Step 2: Create a virtual environment

python -m venv myenv

Here, myenv is the environment name โ€” you can rename it.


โ–ถ Activate the Virtual Environment

OS Command
Windows (CMD) myenv\Scripts\activate
Windows (PowerShell) .\myenv\Scripts\Activate.ps1
Mac / Linux source myenv/bin/activate

๐ŸŽ‰ After Activation

You will see something like:

(myenv) C:\Users\User\project>

Now, anything you install will go inside myenv.


๐Ÿ“ฆ Installing Packages

pip install flask

๐Ÿ“ƒ Freeze Installed Packages

To save installed dependencies (useful for sharing projects):

pip freeze > requirements.txt

๐Ÿ“ฅ Install Packages from requirements.txt

pip install -r requirements.txt

โ›” Deactivate the Virtual Environment

deactivate

๐Ÿงน Remove a Virtual Environment (Optional)

Just delete the folder:

myenv

Quick Workflow Summary

cd project-folder
python -m venv myenv
myenv\Scripts\activate (or source myenv/bin/activate)
pip install package_name
pip freeze > requirements.txt
deactivate

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

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