Managing Python Dependencies Transcripts
Chapter: Isolating Dependencies With Virtual Environments
Lecture: My Virtual Environment Workflow
0:00 There are a few command line tricks you can use
0:03 to make your life easier when you're working with these virtual environments.
0:06 So I want to show you a quick walkthrough the virtualenv setup
0:10 or the virtualenv workflow, that I use personally for working on Python projects.
0:15 So I am in a fresh terminal session here and let's pretend I want to work
0:20 on a project called test project, the one we created earlier.
0:24 So the first thing I would do is to switch into the project folder,
0:28 and then I have this alias command here in my shell that I called ae
0:32 for activate, and I can use that to super quickly activate virtual environment
0:38 without having to type out this source command.
0:42 So I am going to run this, and what this does is
0:45 it's automatically activating the virtual environment because
0:48 I use this naming convention, where I just call all of my virtual environments venv,
0:53 so the command is just going to look for venv virtual environment,
0:57 and it's going to activate it.
1:00 Next, I would do my actual work on the project,
1:03 run my tests, work with the Python interpreter, run the actual project,
1:07 and then when I am done with the project, I would just type de for the deactivate
1:12 and that deactivates the virtual environment.
1:15 I am going to show you how these aliases work, in a minute.
1:18 There is one more thing I wanted to show you first though.
1:21 And that is how they allow me to quickly switch between different virtual environments.
1:25 So I am going to activate the virtual environment for test project again.
1:29 And now let's imagine I wanted to switch and work on a different project
1:34 with its own virtual environment, so I am here now in the different project folder,
1:39 and now I would need to switch to its new virtual environment.
1:43 And this project's virtual environment uses the same naming convention,
1:47 so now I can just go ae again and what that is going to do,
1:51 is it's going to deactivate the previous virtual environment
1:54 and then activate the new one.
1:56 So I don't have to type deactivate first, and then of course,
2:00 I could do my work and just deactivate the virtual environment again.
2:04 So I am using these ae and de shortcuts or aliases to make it a little bit easier
2:09 to work with virtual environments from the command line.
2:12 And now I am going to show you how those work behind the scenes.
2:15 So what I have done here is I've modified the config file from my shell,
2:20 I am using bash so this would be ~/.bash_profile
2:23 And I've added these two lines here, that configure an ae and a de alias.
2:28 Here is what the ae alias does.
2:32 It tries to deactivate any virtual environments that I am already in,
2:35 and then it sources the activate script in the local venv folder,
2:41 so if I am using the same naming convention for my virtual environments,
2:44 this will just work every single time I am working on a project.
2:47 And the de alias just points to deactivate
2:50 which is going to save me some typing in the long run.
2:53 So the usage for this is really easy, I just switch to a project folder, I punch in ae,
2:58 hit return, then I can work on the project and when I am done,
3:01 I just type de and I leave the virtual environment.
3:04 And this makes working with virtual environments just a little bit smoother
3:08 so this is something you could try out and see
3:11 if you want to incorporate that into your own workflow.