Home > Software design >  Different $PATH in Virtual Environments
Different $PATH in Virtual Environments

Time:05-04

I'm on Mac using only terminal.

Running echo $PATH returns

/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/.cargo/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/username/Venvs/default/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/username/.nvm/versions/node/v17.4.0/bin:usr/local/bin:/Users/username/.cargo/bin

After activating the venv, echo $PATH only returns

/Users/username/Venvs/default/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/username/.nvm/versions/node/v17.4.0/bin:/usr/bin:/bin:/usr/sbin:/sbin

How do I set i.e. /Users/username/.cargo/bin to be loaded by all venvs by default? (now it's set in .zshrc)

I can't find any documentation of how do Virtual environments set the $PATH and why. I can't even figure out where do most of the entries come from.

This is a related question, but does not address why the situation happens when I'm in the terminal the whole time: Environment $PATH different when using venv

I have tried to append the PATH using all these places with no difference:

/etc/bashrc
/etc/profile
~/.bashrc
~/.bash_profile
~/.profile
~/.MacOSX/environment.plist
~/.zshrc
~/.zprofile

CodePudding user response:

The whole point of a virtual environment is to configure your shell to use a specific Python installation.

While details might vary depending on the tool used to create the virtual environment, typically there is a shell script activate that you source which explicitly adds the virtual environment's bin directory to your PATH. (It also saves the old path, and defines a shell function deactivate which restores PATH to its previous value.)

CodePudding user response:

The only way to make $PATH different seems to be to activate a virtual environment and then append to it.

I use a default venv that I'm setting in .zshrc, but I have also been setting it in .zshenv, which was loaded first. Removing the venv activation from .zshenv solved the issue.

So in case there's a difference between your $PATH in different venvs, make sure the order of activation and appending is correct.

  • Related