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 trialBruce Whealton
3,619 PointsI don't understand the idea that one doesn't lose one's scripts when using virtualenv?
If I activate a virtualenv and install django and create a django app within the activated virtualenv, and then I destroy that directory, I will lose my script, my project's code.
What is meant by not installing the the scripts within the virtualenv?
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsThe virtual environment created by virtualenv
provides a mechanism to change the order of directories searched to find files to execute. Without the virtualenv, all files executed would come from the global system directories. Therefore, only files placed into the venv
directory through pip install
are at risk of being removed when a venv
directory is deleted. Your code created and held in other directories is independent of the venv
directory or the whether it is "activated" or not.
Let's assume you create a virtualenv using virtualenv venv
. This command simply creates a directory to hold specific versions of executables: python
, pip
, site-packages
, etc. This directory, by itself, means nothing.
When the .\venv\Scripts\activate
(windows) or the . .\venv\bin\activate
(linux) is run, these directories are added to the beginning of the search path so these files are found before system-level files.
Typically, the additional files in the venv
directory come from running pip install
while the venv
is activated, meaning that the venv
directory is in the search path, causing the venv
version of pip
to run which knows to install the modules in the venv
site-packages directory. This is where they will be found when running the venv
version of python
.
Your local app.py
, Django, Flask, or other ..py
files don't know about the venv, it is the command line interface that is working the magic via the path
variables.
Therefore, you don't put your own code in the venv
directory. Instead you place your code in what ever development directory you wish as if there wasn't a venv
.
Finally, using activate
and deactivate
only determines whether the local venv
versions or the system-level versions of files are run.
Bart Bruneel
27,212 PointsHello Bruce,
When you start a Django-project from inside a virtualenv all your scriptfiles will be stored in the designated directory on your computer. You can access them and modify them in a text editor even when not inside the virtualenv in the shell. However all the packages you install inside a virtualenv using the shell (even if you install django inside the virtualenv) will be specific to that virtualenv. It is for example possible to have two Django-projects that use two different versions of Django (1.6 vs 1.8) inside two different virtualenvs. This prevents errors from 'cross-contamination' between these two projects.