Today I learned how to create an alias to activate my Python virtual environments with a single-word command.

Shell aliases

All1 shells allow you to create aliases, which essentially act as shortcuts for commands you type often. How you create an alias differs from shell to shell, but it is common to have a configuration file in which you type something like:

alias command="long or annoying command you want to create the alias for"

Activate a virtual environment with an alias

I usually create Python virtual environments with the command python -m venv .venv, which means I type the command source .venv/bin/activate to activate the virtual environment many times.

I knew about aliases for a while, but it just dawned on me that I don't need to type source .venv/bin/activate, which is FOUR words, if I can just type activate after creating a shell alias!

So, I went to my shell configuration file ~/.zshrc (I use Oh My Zsh) and I added the following:

alias activate="source .venv/bin/activate"

Now, I can activate my virtual environments with the single-word command activate! Neat, right?

That's it for now! Stay tuned and I'll see you around!

  1. Maybe, just maybe, there is a shell that does not allow you to create aliases... ↩

Thanks for reading ❀️

If you enjoyed this blog article, you will love the mathspp insider πŸπŸš€ newsletter! Join +16.000 others who are taking their Python 🐍 skills to the next level! πŸš€

Previous Post Next Post

Blog Comments powered by Disqus.