Introducing Actifish: Easily Activating Python Virtual Environments in the fish Shell
Introducing Actifish: Easily Activating Python Virtual Environments in the fish Shell
My friend was complaining to me that uv
couldn’t activate its own Python virtual environments and instead required him to run the appropriate activation script in the shell manually. This is necessary because uv
runs as a child process of the shell and thus cannot affect the shell’s …
My friend was complaining to me that uv
couldn’t activate its own Python virtual environments and instead required him to run the appropriate activation script in the shell manually. This is necessary because uv
runs as a child process of the shell and thus cannot affect the shell’s (its parent’s) environment, including the various changes made by the activation script.
He also mentioned he wanted a replacement for parts of VirtualFish, particularly the ability to activate “global” virtual environments stored in some central location in his home directory.
I decided to write a small fish shell function to do this, in order to save the repetitive typing of source .venv/bin/activate.fish
. Taking inspiration from tools like Git, I decided to search upwards in the directory tree from the current directory and activate the first virtual environment it encountered.
For the global virtual environment functionality, I wanted configurability for the storage location of the actual environments, so I used a PATH-like variable to allow the user to set as many storage locations as desired.
Once the function is stored in a file at .config/fish/functions/activate.fish
, it is immediately available in any running instances of fish. Simply running activate
will find the nearest virtual environment and activate it!
I have a bit of a suspicion that the code could be made more efficient somehow (but I don’t know exactly how), and, since this is just a quick script, there’s definitely a chance that I missed an edge case or two. However, I purposely used only fish built-ins, meaning that there are no external dependencies and it should run without any process-creation overhead or anything like that.
The code is available at https://github.com/scolby33/actifish. There’s also a README with further instructions, particularly around the global environment functionality. Feel free to reach out there with any issues or suggestions.
I hope this is useful to some fish and Python users out there!