Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

My Virtual Environments Workflow

Give Feedback
# ~/.bash_profile
alias ae='deactivate &> /dev/null; source ./venv/bin/activate'
alias de='deactivate'
$ cd project
$ ae
(venv) $ # Work on the project...
$ de

00:00 There are a few command line tricks you can use to make your life easier when you’re working with these virtual environments. So I want to show you a quick walkthrough of the virtual env setup or the virtual env workflow, that I use personally for working on Python projects.

00:15 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. 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 (for “activate”) and I can use that to super quickly activate virtual environment without having to type out this source command.

00:42 So I am going to run this, and what this does is it’s automatically activating the virtual environment because I use this naming convention, where I just call all of my virtual environments venv, so the command is just going to look for the venv virtual environment, and it’s going to activate it.

01:00 Next, I would do my actual work on the project, run my tests, work with the Python interpreter, run the actual project, and then when I am done with the project, I would just type de (for the “deactivate”) and that deactivates the virtual environment.

01:15 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. And that is how they allow me to quickly switch between different virtual environments.

01:25 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 with its own virtual environment. So I am here now in the different project’s folder (project-2), and now I would need to switch to its new virtual environment.

01:43 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, is it’s going to deactivate the previous virtual environment and then activate the new one.

01:56 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. 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.

02:12 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, I am using bash so this would be ~/.bash_profile.

02:23 And I’ve added these two lines here, that configure an ae and a de alias. Here is what the ae alias does. It tries to deactivate any virtual environments that I am already in, and then it sources the activate script in the local venv folder.

02:41 So if I am using the same naming convention for my virtual environments, this will just work every single time I am working on a project. And the de alias just points to deactivate which is going to save me some typing in the long run.

02:53 So the usage for this is really easy, I just switch to a project folder, I punch in ae, hit return, then I can work on the project and when I am done, I just type de (+ return) and I leave the virtual environment.

03:04 And this makes working with virtual environments just a little bit smoother so this is something you could try out and see if you want to incorporate that into your own workflow.

AugustoVal on Sept. 27, 2019

Hello Dan, I hope this message finds you well, I was wondering if you could help me with something. I am trying to re-create the short alias command that you teach on the managing package with python course. I completed the section about env and I am having a bit of a hard time setting the whole env topic in this course and also outside this course. Anyway. Here is my situation: RP_Material/Python_Dependency_Course/test-project

.bash_profile
 alias ae='deactivate &> /dev/null; source .venv/bin/activate'
alias de='deactivate'
ae
zsh: command not found: ae

Would you be able to tell me why am I getting this message? Thanking you in advances

Dan Bader RP Team on Sept. 27, 2019

It looks like you’re using zsh as your shell whereas I’m using bash in my example. So if you configure the alias in .bash_profile it won’t work because that’s the configuration file used by the bash shell.

To get the ae alias to work in zsh you need to define it in your .zshrc file. The alias syntax should be the same. I don’t use zsh myself but according to this Stack Overflow answer the alias syntax should be the same.

Hope that helps you out :)

Dan Bader RP Team on Sept. 27, 2019

I also just noticed that there’s a typo in your activate alias. The important command is:

source ./venv/bin/activate

In your example above there’s no ./ before venv. That’ll also cause it to fail because it can’t find the activate script inside the virtual environment.

Also my ae/de aliases assume that your virtual environment is called venv and that it lives inside the project folder. So in order for this to work your virtual environment folder must use the same name. But of course you can adapt the alias if you’d like.

jamesdrabinsky on Jan. 9, 2020

Hi Dan,

I am trying to create this shortcut on Windows 10 and I get his error message:

bash: ./venv/Scripts/activate: No such file or directory

Do you know if any changes need to be made for this to work on Windows?

Thanks!

Damian on April 17, 2020

wow.. this worked like charm! wooo!

umbertomorelli1988 on July 5, 2020

Hi all, with Powershell (Win 10) you can use one/all of the following:

function ce { cmd /c python -m venv .venv }
function ae { cmd /c deactivate > $null 2>&1; .\.venv\Scripts\Activate.ps1 }
Set-Alias -Name de -Value 'deactivate'

‘ce’ creates the virtual environment; ‘ae’ and ‘de’ activates and deactivates it, respectively. To persist the aliases, I followed a guide on how to create a Powershell profile; then pasted the aliases within. To create a Powershell profile, issue the following:

  1. Test-Path $Profile (if the output is True, jump to 3)
  2. New-Item –Path $Profile –Type File -Force
  3. notepad $Profile

Dan Bader RP Team on July 6, 2020

Ah nice, thanks for sharing your PowerShell code with us Umberto :)

Patricio Urrutia on Aug. 6, 2020

Hi Dan,

Just for curiosity, when you are writing the code do you have the virtual enviroment activated, or do you only activate the virtual enviroment when you are running the scripts?

Dan Bader RP Team on Aug. 6, 2020

I usually have a terminal window open alongside my editor with the virtual environment activated the whole time so I can quickly run and re-run code as needed.

Typically the first thing I do when I set up my coding environment at the start of the day to begin working on a project I’ll:

  1. open my terminal app (I like iTerm2 on macOS)
  2. cd to the project folder
  3. open my editor with subl . (Sublime Text) or code . (VS Code)
  4. open Sublime Merge with smerge .
  5. and then run my ae alias to activate the Python virtual environment.

And then I’m all set up and ready to go :)

Become a Member to join the conversation.