Python 3, an Illustrated Tour Transcripts
Chapter: Virtual Environments
Lecture: Walk-through: Virtual Environments and Pip (Unix)
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 look at the venv test assignment and we're going to do on Unix system so that'll work on Linux or Mac systems.
0:09 I've got the files downloaded from the labs here. Let's look at the venv test lab and you can see there's the assignment in that comment
0:18 under the test venv function that says use pipenv to create a virtual environment and install pytest
0:23 activate your virtual environment and run pytest on this file by invoking Python venv test.py
0:31 You'll note that this will run pytest because at the bottom there we've got the check for if the __name__ attribute is the string __main__
0:38 import pytest and invoke pytest.main on the file. So let's do that, I'm going to create what's called a user install.
0:47 If you don't have access right to your system or install into your system Python,
0:52 you might need to do a user install, it has a few more steps, so this is how we do it.
0:56 We say Python 3 -m pip install --user and we're going to install pipenv. Okay, it looks like that installed.
1:08 Now what I need to do is be able to access the pipenv command-line utility
1:13 and because I installed that in a user install, Python installed in a certain place,
1:18 I'll show you where it came. Python -m I'm going to invoke the site module and say user-base, that says that it put it in my home directory
1:29 in library Python 3. So in there, I should see a bin pipenv executable. Let's see if that exists in the bin directory and there is a pipenv.
1:42 So now what I want to do is I want to make sure that this bin directory is in my path. So I'm going to update my .bash profile.
1:49 I'll just show you how I update it. It looks like that, and once you've updated that
1:58 you can source that file and you should be able to say which pipenv and it should tell you that it's using the user-installed pipenv.
2:10 Great, at this point, I'm ready to use pipenv and note that I'm in my directory, where I want to create the virtual environment,
2:17 I want to do it in the lab directory. If you don't create it from inside the directory you want to
2:22 you might have problems activating it automatically using the pipenv tool. So make sure you're in the right directory here.
2:29 All you have to say is pipenv install pytest. So it says it's creating a virtual environment for the project and it's installing pytest.
2:42 You can see up at the very top it says where the directory is in the virtual environment, it's in my home directory in the .local folder.
2:50 So if I just list the contents of the home directory, I'm not going to see that because it starts with a period
2:56 so I have to do an ls-al to actually get the local directory there. And you can see up at the top here, there's a .local directory there.
3:15 Okay, so inside of that local directory, there's a shared directory and then there's virtual environments inside of there
3:24 and it created this guy right here, talkpy3 -labs and if you look, there's a bin directory and there's an activate guy in there.
3:32 We're not going to run this the traditional way, we're going to use the pipenv tooling to activate this.
3:36 So how we activate with pipenv is we say pipenv shell. And you'll see that it says it's spawning an environment and it says use exit to leave.
3:46 So in this case, we don't use deactivate which is the traditional virtual environment command to leave the shell
3:51 we're going to use exit and also note that it updated our prompt here and it said that we're using this environment here.
3:58 You'll also note that when we ran pipenv, it created these two files here pipfile and pipfile.lock
4:04 Let's just look at pipfile and you can see that it says that it has installed pytest, we can look at the lock file.
4:15 The lock file has hashes for all the different libraries that we've installed. So if we want to get specific libraries reinstalled,
4:22 it's going to check those hashes to make sure that they're the same versions, nice little feature here.
4:27 Okay at this point, I should have pytest, let's see if pytest is found and it's found in my virtual environment, cool.
4:35 Let's say which Python to make sure that I'm using the Python from my virtual environment,
4:41 which Python, and in this case, I don't need to say Python 3 anymore because when we created the virtual environment
4:48 it made an executable called Python that is Python 3 there. I can also say Python 3, but those should be the same,
4:53 just to show you if I type Python now it says Python 3.6. Okay, and finally, let's run venv test and it says it ran one passed in 0.1 seconds.
5:07 So that's how you create a virtual environment and install pytest using pipenv. I'm also going to exit from this.
5:15 I'm going to say exit and you'll see that now I am out of my virtual environment.
5:20 So this video showed you how to use pipenv to create a virtual environment
5:25 and to install files, how to activate the virtual environment and how to exit from it.