Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Using virtualenv5:35 with Kenneth Love
As you build more and more things with Python, using more and more third-party apps, you're eventually going to have a conflict of package versions. Some project is going to need Django 1.7 while another just has to have Django 1.4.
virtualenv is a great tool that keeps these conflicts from destroying your sanity and productivity.
Windows Users should install with just
pip install virtualenv. Everyone else will want to install
pip install --user virtualenv. The
pip to install
virtualenv to your user space instead of system-wide. (Windows doesn't need this distinction)
Once you activate the virtualenv, though, you can use
pip install without the
--user since it'll only be installed to your virtualenv.
Activating a virtualenv
On a Mac, to change the
PATH variable, you'll edit
~ stands for
/Users/<your username>. You can edit this file with any text editor.
If the file is blank, or at least doesn't contain a line about
PATH, add the line below.
~/.bash_profile does have a
PATH line, edit it to include the
~/Library/Python/3.4/bin bit above.
You can check that the
PATH has now been set correctly by starting a new Terminal and typing
virtualenv. It should find the executable and show you the help documentation.
For even more information, check out this article from A List Apart.
Once you start working locally,
you're going to run into a problem that plagues all python developers.
Packages that you install normally through pip always get installed to
a common location.
This location is shared by all of your projects.
When you only have one or two,
especially if they're really different, this won't be a problem.
But as soon as you have two projects that use two versions of the same library,
you'll have to deal with version conflicts or
reinstalling the package every time you switch to the other project.
Luckily, we have another handy tool named Virtual ENV that helps us with this.
Let's look at what Virtual ENV does.
You can think of your common location, which most Python developers refer to as
global site-package as being a zoo with no enclosures.
Sure, most things work out okay, but the animals, or
your packages, are going to fight for supremacy and
a tourist, or one of your projects, might get eaten along the way.
Bringing in virtual EMV lets us create enclosures for
the animals, or our projects, and give them environments better suited for
their situation, like providing water and ice floats for the penguins.
We don't need to use virtual EMV in work spaces, but
you'll want to use it on your computer.
So let's look at how to install it, and how to use it.
So since we're not going to be using work spaces for
doing virtual ENV, I'm going to show you on this computer.
So we're going to do, pip3, since this is a Mac, if you installed Python and pip
through a way other than the installer, you may be using pip instead of pip3, or
if you're on Windows or Linux you may be using pip instead of pip3.
You should know what your system has based upon how you installed it.
So we are going to do pip3 install --user virtualenv then we press Return.
So, this will install the virtual ENV binary or program to our computer so
we'll be able to create virtual environments wherever we need them.
So to actually run our virtual ENV, for me,
it's gets installed into this Library/Python/3.4/bin/virtualenv,
you'll want to add this to your path, if you're on Windows it should be installed
to where it's already on your path.
So anyway, virtualenv, and then we give it a name.
So I'm going to call this one venv.
And you can see that it's using It's adding its own Python executable,
and it's installing setup tools and pip.
And if we do an ls, we now see that there's a directory here called venv.
So, this name venv isn't important, but whatever name you give it,
is the name that's going to be used for that directory.
You can think of that as it's you're naming the virtual environment.
So, perhaps you're working on a project for the OO Python course.
So maybe you call your virtual ENV oopython.
Or, maybe you're working on a project for workman markdowns.
So you call your virtual ENV Markdown project.
But you don't put your scripts into the venv folder.
The virtual environment isn't meant to hold your custom scripts.
You can put them in there if you want, but virtual envs are meant to be disposable.
You can throw them away and rebuild them whenever you need to.
So, putting your code in there makes it to where you can't throw them away.
So, to use the virtual ENV, we're going to source the ENV, then activate.
For Windows users, you'll activate a virtual ENV using something like
where the venv is the name you gave to your virtual environment.
So when we run this, you see that now our prompt has this venv over on the side.
This tells us that our virtual environment has been activated.
So whatever it is that we do when we use pip,
when we use Python, whatever, it's going to be inside of this virtual ENV.
If we don't do this, if I was to say, install something with Pip, it would
install it to my global or my user site packages instead of to the virtual env.
So let's install something to the virtual env.
We'll do pip install requests.
Now you notice this time I did not do the hyphen hyphen user.
I don't need to because it's installing to the virtual environment instead of to my
user packages or my system packages.
Okay so let's go into Python.
So we're gonna do python, if I do which python.
You notice that it has this users venv and python.
So I can just do python, and I'm inside my virtual environment's python.
So let's import requests.
And then let's just make sure that it's working.
What we'll do, req = requests.get.
And we pass it a string of a website to go to, httpbin.org/get.
And it makes the request, you saw the pause there.
And if we do req.status_code, we should get back a 200, which we did.
So we're able to make a request to httpbin.org, which is a site for
testing HTTP requests.
And we got back a 200, which means it was successful, as it should be.
Virtual ENV isn't the end of building custom environments.
Once you get used to working with it, look at Virtual ENV Wrapper,
which gives some other handy shortcuts to you.
I'll link to it in the teacher's notes.
You need to sign up for Treehouse in order to download course files.Sign up