
    
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/shell","feed_url":"https:\/\/mathspp.com\/blog\/tags\/shell.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"TIL #107 \u2013 Automatically activating virtual environments","date_published":"2024-11-19T18:22:00+01:00","id":"https:\/\/mathspp.com\/blog\/til\/automatically-activating-virtual-environments","url":"https:\/\/mathspp.com\/blog\/til\/automatically-activating-virtual-environments","content_html":"<p>Today I learned how to use shell scripting to activate my virtual environment automatically when I change directories.<\/p>\n\n<h2 id=\"automatically-activating-virtual-environments\">Automatically activating virtual environments<a href=\"#automatically-activating-virtual-environments\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Since I can remember, whenever I <code>cd<\/code> into a directory that contains a virtual environment folder <code>.venv<\/code>, I always want to activate that virtual environment.\nI typed <code>source .venv\/bin\/activate<\/code> so often that I even created an alias so that typing <code>activate<\/code> was enough.<\/p>\n<p>Today, I wondered if there wasn't a way in which I could automatically run the command <code>activate<\/code> whenever I <code>cd<\/code> into a directory that contains a directory <code>.venv<\/code>.\nI was sure there was, so I opened ChatGPT and typed my question:<\/p>\n<blockquote>\n<p>\u201cIn iterm2, how can I run the command <code>activate<\/code> automatically whenever I <code>cd<\/code> into a directory that contains a folder <code>.venv<\/code>?\u201d<\/p>\n<\/blockquote>\n<p>(<a href=\"https:\/\/iterm2.com\/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">iTerm2<\/a> is the terminal emulator I use.)<\/p>\n<p>The output of the model turned out to be 99% of what I needed, and with a small tweak I ended up with this shell script that I added to my shell configuration file <code>.zshrc<\/code>:<\/p>\n<pre><code class=\"language-zsh\">## Generated by ChatGPT.\n## This will automatically run the command `activate` when I\n## `cd` into a directory that contains a directory `.venv`.\ncd() {\n    builtin cd \"$@\" || return  # Use the built-in cd command and handle errors\n    if [ -d \".venv\" ]; then\n        if [ -f \".venv\/bin\/activate\" ]; then\n            echo \"Activating virtual environment in $(pwd)\"\n            activate\n        else\n            echo \".venv directory found, but no activate script exists.\"\n        fi\n    fi\n}<\/code><\/pre>\n<p>Now, whenever I <code>cd<\/code> into a directory with a <code>.venv<\/code>, I see the message \u201cActivating virtual environment in (...)\u201d and the environment activates!\nIsn't technology wonderful?<\/p>","summary":"Today I learned how to use shell scripting to activate my virtual environment automatically when I change directories.","date_modified":"2025-10-20T22:34:56+02:00","tags":["llm","productivity","python","shell"],"image":"\/user\/pages\/02.blog\/04.til\/107.automatically-activating-virtual-environments\/thumbnail.webp"}]}
