Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

My Virtual Environments Workflow

Shell ~/.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.

Avatar image for AugustoVal

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

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

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

Avatar image for Dan Bader

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 :)

Avatar image for Dan Bader

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.

Avatar image for jamesdrabinsky

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?


Avatar image for Damian

Damian on April 17, 2020

wow.. this worked like charm! wooo!

Avatar image for umbertomorelli1988

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
Avatar image for Dan Bader

Dan Bader RP Team on July 6, 2020

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

Avatar image for Patricio Urrutia

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?

Avatar image for Dan Bader

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 :)

Avatar image for djholty

djholty on April 23, 2022

Thanks umbertomorelli1988 for sharing that. If you’ve already created a folder called venv like in the example provided this script won’t work. It creates a new folder ‘.venv’. So if you remove the ‘.’ from the script he provided in line 1 and 3, then it will work as advertised.

Avatar image for Bobby Tagget

Bobby Tagget on Oct. 28, 2023

What does the “&> /dev/null;” mean? I feel that it’s similar to the conditional syntax of “(condition) ? true : false” in other programming languages.

Avatar image for robertprzydatekcl

robertprzydatekcl on Oct. 28, 2023

Bobby Tagget

It redirects all types of console output to /dev/null (basically it removes console output instead of showing it to user/logging it in the terminal)

If you are using Linux, you can copy this code to check it:

echo "Hello Bobby"
echo "Hello Bobby" &> /dev/null

Become a Member to join the conversation.