#100DaysOfCode in Python Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Virtual environments
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
One of the challenges of installing packages globally has to do with the versioning.
0:06
The other really has to do with managing deployments and dependencies. Let's talk about the versioning part first.
0:12
Suppose my web application I am working on right now requires version 2.9 of requests.
0:19
But somebody else's project required an older version with older behavior, version 2.6 let's say. I don't think those are actually incompatible,
0:26
but let's just imagine that they were. How would I install via pip version 2.6 and version 2.9 and keep juggling those,
0:34
how would I run those two applications on my machine without continually reconfiguring it- the answer is virtual environments.
0:40
And, virtual environments are built into Python 3 and are also available through a virtual env package that you can install for Python 2
0:49
and the idea is this- we can crate basically a copy, change our paths and things like that around so that when,
0:56
you ask for Python or this looks for Python packages, it looks in this little local environment, we create one of these small environments
1:03
just for a given application, so we would create one for our web app that uses request 2.9 and another one for the one that uses request 2.6
1:11
and we would just activate those two depending on which project we are trying to run, and they would live happily side by side.
1:18
The other challenge you can run into is if you look at what you have installed on your machine, and you run some Python application and it works,
1:25
how do you know what listed in your environment is actually required to run your app, if you need to deploy it or you need to give it to someone else,
1:32
that could be very challenging. So with virtual environments we can install just the things a given application requires to run
1:38
and be deployed so when we do something like "pip list", it will actually show us exactly what we need to set up
1:45
and use for our app to run in production. Typically we tie virtual environments one to one to a given application. So how do we create one?
1:54
This example uses virtual env which we would have to install via pip, you could also use venv, just change virtual env to venv in Python 3
2:02
and it will have the same effect, but this works, like I said in Python 2 and 3, so here you go.
2:07
So we are going to run Python 3 and we are going to say run the module, virtual env, and create a new environment into ./localenv.
2:15
Here you can see it creates a copy from Python 3.5. Then we go into that environment, there is a bin directory
2:21
and there is an activate program that we can run and notice, we'll use the . (dot) to apply that to this shell
2:27
and not to create a new separate shell environment for that when it runs because we wanted to modify our shell environment, not a temporary one.
2:35
So we say . activate and that will actually change our environment, you can see the prompt change, if we say "pip", we get the local pip,
2:42
if we ask "which Python", you'll see it's this one that is in my user profile not the one in the system.
2:48
Now, few changes for Windows, if I did exactly the same thing in Windows, I would have .\localenv of course, I might not use Python 3,
2:57
I just say Python and make sure I have the right path to Python 3 because that is not a feature in the Python 3 that comes on Windows,
3:04
and I wouldn't use the source activate you don't need to do that in Windows, but you would call activate.bat, otherwise,
3:11
it's pretty much the same. Also, the "which" command doesn't exist on Windows, use "where" and it gives you the same functionality.
3:17
So we can create one of these virtual environments in the terminal, but you might suspect that PyCharm has something for us as well,
3:22
and PyCharm actually has the ability to create and manage virtual environments for us, basically it does what you just saw on the screen there.
3:31
So here we give it a name, we give it a location here, we say blue_yellow_Python, this is going to be for a Blue / Yellow band web application,
3:38
we are going to base this on Python 3.5.1 and we are going to put into my Python environments and under this name.
3:44
Then I just say OK and boom, off it goes, set it as the active interpreter and manage it just the same as before in PyCharm
3:50
using its ability to install packages and see what is listed and so on.