How to Create and Activate a Virtual Environment π β
Now that weβve established virtual environments are like personal pan pizzas (no one touches your toppings!), letβs actually cook one up. ππ₯
π§ͺ Step 1: Check if You Have venv
Installed β
You probably do! Python 3.3+ comes with it baked in like extra cheese.
Just to be sure, run this in your terminal:
python3 -m venv --help
If you see a bunch of options instead of errors β congrats, youβre all set!
ποΈ Step 2: Create a Virtual Environment β
Pick a name for your environment. People usually go with:
venv
.venv
env
my-cute-python-bubble
Letβs roll with venv
for now:
python3 -m venv venv
This will create a new folder called venv
with all your Python goodness neatly packed inside.
β‘ Step 3: Activate the Environment β
Hereβs where it gets OS-specific. Donβt worry, it's like choosing your starter PokΓ©mon.
πͺ Windows β
venv\Scripts\activate
If it works, your terminal will suddenly have (venv) in front of the prompt like this:
(venv) C:\User\YourComputer>
This means you are now inside your own little Python universe. Welcome, astronaut. π§βπ
π§ Linux / π macOS β
source venv/bin/activate
And again, youβll see (venv)
in your terminal. Boom. You're now operating in a bubble thatβs isolated, fresh, and drama-free.
π§ Bonus: Confirm Youβre Using the Right Python β
After activating, run:
which python
# or
where python
You should see the path pointing to something like venv/bin/python
. That means everything β including pip install
β stays inside your pizza box. π
π‘ Pro Tip β
You can make a requirements.txt
file (remember that thing?) and install everything like this:
pip install -r requirements.txt
Just don't forget: Activate before you pip, or else your laptop might start sprinkling packages all over your system like parmesan cheese in a windstorm.