Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Modules and Packages
Lecture: Isolation with virtual environments

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's talk about isolating your dependencies
0:03 and your versions for your external packages,
0:05 from all the other Python projects on your OS.
0:09 Here we are in the terminal,
0:10 and I am in this directory here that I have a bunch of Python environments,
0:14 and I'd like to create a new Python installation basically,
0:18 for a new web app I am going to work on, or any kind of app, really,
0:23 I'd like to be able to control the version of Python is there
0:25 as well as the packages that are installed
0:28 and their versions independent from everything else on my system.
0:31 So I can use virtual environments for this,
0:34 in Python 3, we have a "venv" command
0:37 and we can also use the virtual environment's external package,
0:40 we can also use the virtual environment's external package,
0:43 which works with all versions of Python.
0:45 So I'll go and use that here but they work exactly the same.
0:48 So I want to create a directory here that's going to contain my Python environment
0:52 so I'll say "virtual"- let's make sure we have the right one first, "virtualenv", OK,
0:57 this is going to be the one for Python 3.5, so good,
1:01 so I'm going to say this, and I'm going to give it a folder,
1:03 let's just say sample_web_env, so we know it's an environment, not the web app itself.
1:12 Spelling is hard,
1:16 so we can see it's created a version of Python here based on Python 3.5
1:21 and installed the necessary tools, basically that we need to add more packages.
1:25 So, now if I say "which pip", it doesn't yet have this environment active,
1:31 the creation of the environment doesn't activate it,
1:34 so the next thing we need to do is say dot(.), so the first dot(.) means
1:38 apply this to this shell rather than a separate one for the execution,
1:41 and we'll go in the "sample... bin activate", we run this,
1:44 now our prompt changes and more importantly,
1:47 if we ask the same question - "which pip" - now it's this one,
1:50 if we ask "which python", it's this one.
1:54 OK, so we're ready to use that, let's say pip list to see what we have installed,
1:57 we just have those 3 that were installed there so we could go install
2:01 something like I could say "pip install requests", and it's gone and downloaded,
2:10 now if I ask what's installed, I just have this.
2:13 So I can have version 2.10.0 of request regardless of
2:16 whatever else is installed, upgraded, downgraded, in the rest of my OS.
2:21 All right, let's go and upgrade pip.
2:27 OK, so here is our sample environment,
2:31 we'll be using this in the next section.