Take full control of your shell environment variables
Why control your environment variables
If you're not sure what environment variables are, I have an essay on them that you can reference. Mastering environment variables is crucial for data scientists!
Your shell environment, whether it is zsh or bash or fish or something else, is supremely important. It determines the runtime environment, which in turn determines which Python you're using, whether you have proxies set correctly, and more. Rather than leave this to chance, I would recommend instead gaining full control over your environment variables.
How do I control my environment variables
The simplest way is to set them explicitly in your shell initialization script. For bash shells, it's either .bashrc
or .bash_profile
. For the Z shell, it'll be the .zshrc
file. In there, step by step, set the environment variables that you need system-wide.
Global environment variables
Some environment variables need to be available globally across all your projects and sessions. The most important of these is your PATH
environment variable, which determines where your shell looks for executable programs.
For example, explicitly set your PATH
environment variable with explainers that tell future you why you ordered the PATH in a certain way:
# Start with an explicit minimal PATH
export PATH=/bin:/usr/bin:/usr/local/bin
# Add in my custom binaries that I want available across projects
export PATH=$HOME/bin:$PATH
# Add pixi global binaries
export PATH=$HOME/.pixi/bin:$PATH
# Add uv tool installations
export PATH=$HOME/.cargo/bin:$PATH
# Add more stuff below...
Organizing your environment variables
If you want your shell initialization script to be cleaner, you can refactor it out into a second bash script called env_vars.sh
, which lives either inside your home directory or your dotfiles repository (see: Leverage dotfiles to get your machine configured quickly). Then, source the env_vars.sh
script from the shell initialization script:
source ~/env_vars.sh
There may be a chance that other things, like package managers, will give you an option to modify your shell initializer script. If so, be sure to keep this in the back of your mind. At the end, of your shell initializer script, you can echo the final state of environment variables to help you debug.
For environment variables that should be available everywhere on your system, consider using dotfiles to manage your environment. This approach helps you keep global settings consistent across machines.
For environment variables that should only apply to a specific project, see how to manage environment variables for individual projects. This helps you keep project-specific settings isolated from your global shell environment.
Environment variables that need to be set on a per-project basis are handled slightly differently. See Create runtime environment variable configuration files for each of your projects.