Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Modules and Packages
Lecture: Isolation with virtual environments
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:11 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, virtual env, 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:04 let's just say sample web env so we know it's environment, not the web app itself.
1:11 Spelling is hard,
1:16 so we can see it's created a version of Python here based on Python 3.5
1:22 and installed the necessary tools basically that we need to add more packages.
1:26 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 . 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 download it,
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 int he next section.