Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to USE virtual environments, Python

Hello,

I am having trouble understanding how Virtual Environments work. I understand the purpose of the virtual environment and why we should use it when creating multiple projects.. I am able to activate and create a virtual environment. But once it is activated, how do I run my script from it?

Do I need to write my scripts and save it under the directory of the virtual environment?

Or,

Am I able to change directory to my script folder in the command line and then run the script there?

I am probably over thinking this but I cannot find my answer anywhere online...

Once the virtual environment is activated and running, how do I USE it...?

1 Answer

Hi Julian,

Your second point is correct. Once you have your virtual environment activated, you simply cd into your working directory and run scripts as normal. There really is no difference in your workflow. The only thing is that everything Python related will stem from your virtual environment. Anything you install via pip will be contained within that virtual environment. As soon as you deactivate, you go back to the global environment.

EDIT: Just to clarify further, your virtual environment isn't necessarily married to your working directory. Once you activate your virtualenv, you are free to move around to any directory you want via the command line.

Anything you install via pip will be contained within that virtual environment.

This is really the key point. Virtual environments are for organizing requirements. They are meant to prevent you polluting your global environment with a bunch of programs / packages that are only needed for one project, or having different versions of a requirement than what's needed for another project. You don't have to do anything differently once you've activated your virtualenv.