Thoughts on automating automation itself.
The most dependable method of improvement is reflection. If you can identify something that gives you a hassle, then you should give yourself the freedom to do something about it. If you’re a developer, a lot of this boils down to the question, “what can I automate this week?”. Usually, at least for me, this notion of automation results in something that I can do from the command line. It might be investing in a better habit, a new way of working, or something that “automates the boring stuff.”
I figured it’d nice to demonstrate an example of something I did a while ago that made the whole “python and virtualenv” relationship a whole lot easier for me. It’s just an example but one that made my life a whole lot easier.
I use a mac and a raspberry for my development. Both offer a terminal which you can fully customise. You can do this by editing a file called .bash_profile
(or if you’re into zsh it’s .zshrc
). These files exist in the home directory can you can usually see the contents by typing cat .bash_profile
. This is a file that is run every time you open a terminal. You can add settings here, but also functions and links to apps you find useful. Here’s a snippet from my .zshrc
.
function mkvirtualenv() {
virtualenv -p $(which python3.6) venv
source venv/bin/activate
}
function pysrc() {
source venv/bin/activate
}
export PIP_REQUIRE_VIRTUALENV=true
gpip() {
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
There’s a few things that happen in this block:
mkvirtualenv
is now a function that I can call that creates a virtualenv in my local folder called venv
, once this is created the virtualenv is also activatedpysrc
is now a function that I can call that actives the virtualenv that is in the current folderPIP_REQUIRE_VIRTUALENV
is an environemnt variable which is recognized by pip which ensures that I don’t accidentally pip install
anything in my global environment (this was the biggest source of errors)gpip install
A nice thing about these settings is that these settings encourage me to adhere to the good habit of making virtualenv early and often. It also automatically prevents any accidentaly intalls into my global environment that might cause confusion in my local environment. I’ve noticed too often that tools like pytest
or jupyter
were looking for the globally scoped instances which caused all sorts of dependency confusion across environments.
With this change to my terminal the number of virtualenv errors that I experience is now zero.
Here’s an asciinema that gives a demonstration.
Your terminal might look a bit difference since I am using zsh but the commands should work in the same way. Note that a bash profile can be extended with other amazing things that are useful too.
This line of code ensures that I can quickly open and edit a file with sublime text in my local directory by typing subl filename.txt
.
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Not reflecting on what you do in your day to day is a dangerous thing; it slows down automation and enjoyment of work. Figuring these settings out took maybe an hour to get right but I’ve earned 100x the investment by no longer being haunted by virtualenv issues.
The interesting thing here is that people like to make a habit out of automation but fail to recognize the reflection that is needed beforehand. If you’re going to invest into any habit here, focus on the reflection part. It is the hardest to get right but you cannot automate automation without it.
There are loads of habbits that you can introduce to your workflow that might make you more productive and more happy. There are three paid apps that I can highly recommend here too (if you’re using a mac):