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