Python 3, an Illustrated Tour Transcripts
Chapter: Virtual Environments
Lecture: Virtual Environments and Pip

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video we're going to talk about virtual environments and pip. A virtual environment is a mechanism that Python provides
0:08 to allow per project dependency. When we install Python, we get what we call the system Python installed
0:14 or if you have a machine that comes shipped with Python there is a version of Python installed and you can install packages into that,
0:21 but you probably don't want to and we'll explain why. Virtual environment allows us to isolate our dependencies based on a project,
0:29 and it allows us to easily install and upgrade those dependencies. One example of why we might not want to use our system Python is illustrated here
0:39 assume that I'm working on a project that requires Django 2.0 and later a year or so after that, my boss comes in and tells me
0:47 he wants me to work on a new project and I want to use Django 3.4 the current version of Django at the time.
0:54 If I've installed Django 2 on my system environment. I now need to install Django 3.4 on top of that
1:00 and that can bring in the whole bunch of dependencies and whatnot essentially making it so my old project won't work anymore,
1:07 which could be fine, if I'm working on a new project. But what happens when my boss says, oh, I need you to fix that bug in the old project.
1:14 Well, then you've got to go in and uninstall Django 3 and all the dependencies and install Django 2, it turns into somewhat of a headache.
1:21 So solution to that is to use these virtual environments and that allows us to on a per project basis
1:26 create an environment for Python and so we can have one for our old Django 2.0 project and have another one for our new one
1:34 that our boss tells us to create. Using these virtual environments we can easily switch
1:38 between these two different versions and use the appropriate one very easily. Here's how we create a virtual environment.
1:45 I'm going to run Python with -m switch, the -m swicth allows me to run a module that's included in the standard library in the Python path,
1:54 in this case it's going to run the venv module. And we're going to pass in a path where we want the virtual environment to live.
2:02 This can be any path and I like to put it in a directory called .venv in the same directory as my project, I'll tell you why in a minute.
2:16 After we've created this virtual environment, it might take a while. What Python is going to do is it's going to make
2:21 a directory structure in there that has a binary directory where there's a Python and a pip in there and it's going to make a library directory
2:31 and it's going to also give us a tool that allows us to activate our virtual environment and when we activate it, what it's going to do,
2:37 it's going to shim in our path where our new binaries are found into our path variable.
2:42 So when you run Python, we're no longer running the system Python, but we're running the Python in a virtual environment.
2:48 And you can see on bash systems when we run this command source with the path to this new environment and then there's an activate script in there
2:57 when we run that, we can see that our shell will tell us the name of the virtual environment we are in.
3:02 In this case, we can see that the env matches the env that we passed in to the command line.
3:09 On Windows, we do a similar thing, we can pass in the full path. If you have multiple versions of Python installed you can use the full path to Python.
3:17 And again, we're going to do the -m with the venv module, and we give it the path to where we want to create our virtual environment.
3:24 Once we've created that virtual environment in Windows, because we don't have source we run this bat file here, which is in a scripts directory.
3:34 And if you run that, you'll see that that updates your prompt as well. Just to clarify the -m swich, this will execute a module
3:40 that's found in your Python path, why we use this instead of using a pip executable or virtual env executable is it allows us to make sure that we know
3:51 which version of Python we're using to create our virtual environment on systems where you might have multiple versions installed.
3:57 this can be pretty handy. Once we have a virtual environment what we do inside of it? Typically we install things, so there's a couple commands
4:04 we can say pip install foo to install package foo, we can create a requirements file if we want to
4:10 we can say install -e and that will install our package in edit mode, it allows us to have our package in the Python path, but also update it
4:18 and when we update it will get live code essentially in our Python path. We can also say pip freeze and that will list
4:25 all of the packages that are installed in a virtual environment and we can also uninstall libraries, which can be nice.
4:31 One thing to be aware of is if you've created a virtual environment just running pip doesn't ensure that you're going to install something
4:38 into that virtual environment, you either need to be using the pip that's located in the virtual environment or have your virtual environment activated
4:46 so that the pip for that virtual environment is shimmed in the path and that's what's getting called when you call pip,
4:52 so just make sure that you know which pip you're running. Let's talk about a tool called pipenv, as of post Python 3.6 pipenv is now
5:01 according to the Python.org documentation the recommended tool for managing dependencies. What is pipenv?
5:08 Pipenv is a tool that basically allows you to do virtual env and pip in one command line, we'll see some examples of that.
5:16 Now this is a third party tool, so it's not included in Python 3.6. So you need to install it, again, we're going to use this -m module
5:26 we're going to say Python 3 -m pip install and we're going to also use a --user option on the command line here
5:34 what the -- user option says, is it says I want you to install this package, but I don't want you to install it into the system Python
5:42 I want you to install it into a user directory that your user has access to but other users might not have access to.
5:50 It allows you to get around having to be root to install packages. Now that might be problematic,
5:56 because probably wherever Python is going to install this pipenv user package is not going to be in your path
6:06 and you want the pipenv tool to be in your path. So you're going to have to do some things after that.
6:11 Yeah, this is a little bit more convoluted and not quite as easy as it could be but bear with me and you'll only have to do this once.
6:19 You want to know where your user base is or where this user path is you can run this command here Python 3 -m site -- user-base
6:27 and that will tell you the directory of where your user base is and on Unix systems, if you add bin to that
6:35 or on Windows systems, if you add Python 3.6 scripts to that that will be the path where pipenv will be located.
6:42 So you'll need to do something like this on a Unix system in your .bash profile file,
6:47 you'll need to update that and add into the path this new directory here and then you can source that and you'll have access to your user pipenv.
6:57 On Windows systems, it's a similar process though typically not done through editing files.
7:03 You need to go down to your search box and type env in there in the path environment variable, you want to update that
7:10 and add the user base with the addition of Python 3.6 and scripts on the end of that.
7:16 That point, if you relaunch the command prompt by typing cmd in the search box, you should be able to type pipenv and have access to it.
7:22 Here's an example of using pipenv. I've created a directory called blockchain, this is on Unix, but you can do similar things on Windows.
7:29 I'm going to change into that directory, and now, inside of that directory, I'm going to say pipenv install py.test
7:36 what that does, is it goes out and because I called pipenv it's going to create a virtual environment for me
7:42 and it's going to install py.test into that virtual environment. If I look in the current directory after I run this,
7:49 this directory was brand-new, I just created it, I'll see two files, I'll see a file called pipfile and the file called pipfile. lock.
7:56 That's what pipenv has created for me. It's also done some other things behind the scenes. It's created a virtual environment for me.
8:02 Let's talk about pipfile. Pipfile is a new way of specifying dependencies. It supersedes the old requirements.txt file.
8:11 One of the nice things about pipfile is that it allows you to support a dev environment and a default or production environment in a single pipfile
8:20 and this basically says if you're developing this library, then you'll want to install the dev dependencies.
8:27 If you're deploying it for production or whatnot, you'll just want the default dependencies. There's the other file there, the pipfile.lock
8:33 and that stores the installation details about what libraries you have and it has hashes and versions in there, so you can make sure
8:41 that when you recreate a virtual environment using this file that you have the exact same libraries installed.
8:48 When you run this pipenv tool, it's also going to create a virtual environment
8:52 and on my system, it threw it into this .local\ share\ virtualenvs directory and inside of there, it created a directory called blockchain
9:01 with a unique identifier after it. If you want to have pip recognize another directory,
9:07 it will recognize a .venv directory that is in your current project, if you have this environment variable pipenv_venv in project set
9:18 so you can set that using the set command in Windows or you can just say pipenv_venv_in_projet=1 right before your pipenv command and it will
9:32 recognize .venv virtual environment if you've got one. A couple commands for using pipenv you can say pipenv run Python and note that
9:41 my environment here is not activated, my virtual environment, but because I'm using pipenv, pipenv has some logic to know
9:49 that I am in a directory that has a virtual environment associated with it and so will launch the right Python there.
9:56 If I want to activate my virtual environment, I can say pipenv shell and that will activate it, note that this command will work in Unix and Windows.
10:04 A couple of other commands that we can do with pipenv, we can say pipenv --venv and that will give us the location of where our virtual environment is.
10:12 We can say --py and that tells us which Python it's going to run we can install a package and we can install it
10:19 with a dev dependency by putting --dev after it. Cool thing that we can do with pipenv is we can say graph
10:24 and that will create a dependency graph to tell us what our dependencies depend on, we can lock our dependencies by calling pipenv lock.
10:32 And we can uninstall package as well. I hope that you've learned a little bit more about virtual environments and pip and pipenv.
10:40 If you haven't tried using these tools, please try them. Again, as I said, pipenv is now the recommended tool
10:46 to use moving forward so you should get used to using that tool.


Talk Python's Mastodon Michael Kennedy's Mastodon