Managing Python Dependencies Transcripts
Chapter: Isolating Dependencies With Virtual Environments
Lecture: My Virtual Environment Workflow

Login or purchase this course to watch this video and the rest of the course contents.
0:00 There are a few command line tricks you can use to make your life easier when you're working with these virtual environments.
0:07 So I want to show you a quick walkthrough the virtualenv setup or the virtualenv workflow, that I use personally for working on Python projects.
0:16 So I am in a fresh terminal session here and let's pretend I want to work on a project called test project, the one we created earlier.
0:25 So the first thing I would do is to switch into the project folder, and then I have this alias command here in my shell that I called ae
0:33 for activate, and I can use that to super quickly activate virtual environment without having to type out this source command.
0:43 So I am going to run this, and what this does is it's automatically activating the virtual environment because
0:49 I use this naming convention, where I just call all of my virtual environments venv, so the command is just going to look for venv virtual environment,
0:58 and it's going to activate it. Next, I would do my actual work on the project, run my tests, work with the Python interpreter, run the actual project,
1:08 and then when I am done with the project, I would just type de for the deactivate and that deactivates the virtual environment.
1:16 I am going to show you how these aliases work, in a minute. There is one more thing I wanted to show you first though.
1:22 And that is how they allow me to quickly switch between different virtual environments.
1:26 So I am going to activate the virtual environment for test project again. And now let's imagine I wanted to switch and work on a different project
1:35 with its own virtual environment, so I am here now in the different project folder, and now I would need to switch to its new virtual environment.
1:44 And this project's virtual environment uses the same naming convention, so now I can just go ae again and what that is going to do,
1:52 is it's going to deactivate the previous virtual environment and then activate the new one.
1:57 So I don't have to type deactivate first, and then of course, I could do my work and just deactivate the virtual environment again.
2:05 So I am using these ae and de shortcuts or aliases to make it a little bit easier to work with virtual environments from the command line.
2:13 And now I am going to show you how those work behind the scenes. So what I have done here is I've modified the config file from my shell,
2:21 I am using bash so this would be ~/.bash_profile And I've added these two lines here, that configure an ae and a de alias.
2:29 Here is what the ae alias does. It tries to deactivate any virtual environments that I am already in,
2:36 and then it sources the activate script in the local venv folder, so if I am using the same naming convention for my virtual environments,
2:45 this will just work every single time I am working on a project. And the de alias just points to deactivate
2:51 which is going to save me some typing in the long run. So the usage for this is really easy, I just switch to a project folder, I punch in ae,
2:59 hit return, then I can work on the project and when I am done, I just type de and I leave the virtual environment.
3:05 And this makes working with virtual environments just a little bit smoother so this is something you could try out and see
3:12 if you want to incorporate that into your own workflow.


Talk Python's Mastodon Michael Kennedy's Mastodon