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...ย โ†ฉ

Become a better Python ๐Ÿ developer ๐Ÿš€

+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!

My book โ€œPydon'tsโ€ teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here ๐Ÿ๐Ÿš€.

Previous Post Next Post

Blog Comments powered by Disqus.