0

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
Jura Brazdil
  • 970
  • 7
  • 15
  • Well, you wouldn't expect the two PATHes identical, would you? So the question would rather be: Which PATH component are you missing in which of the two cases? – user1934428 May 03 '22 at 12:27
  • I would expect it to be indentical, having an installation of psql or rust is imho independent of Python. Plus I really can't find any info about what the relationship between venvs and $PATH is – Jura Brazdil May 03 '22 at 12:46
  • I've added an actual question. How do I get cargo to all venvs by default? (it's not by adding it to .zshrc) – Jura Brazdil May 03 '22 at 13:07
  • Are you interested in getting all definitions inside your `.cargo/bin` file, or only the environment variables? – user1934428 May 03 '22 at 14:41
  • 1
    Everything. I've found the issue already - I've had another venv activation in `.zshenv` that I didn't know about, so all the changes to `$PATH` happened in an activated venv and didn't persist. – Jura Brazdil May 03 '22 at 14:43

2 Answers2

0

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.)

chepner
  • 497,756
  • 71
  • 530
  • 681
  • the Python installation is not my point, my issue is the other components of the $PATH missing after I activate. – Jura Brazdil May 03 '22 at 12:48
  • I've added an actual question. How do I get cargo to all venvs by default? (it's not by adding it to .zshrc) – Jura Brazdil May 03 '22 at 13:07
  • You don't, but you also shouldn't need to. It's not a Python package, and your virtual environment just prepends the venv/bin to your path. Make sure your cargo directory is in `PATH` *before* you start the virtual environment. If you've recently changed your `.zprofile` (use that, not `.zshrc` for environment variables), you'll need to deactivate the venv, update `PATH` (either manually or be starting a new shell session) then reactivating the venv. – chepner May 03 '22 at 13:21
  • The entry in PATH is there before I activate the venv. After I activate, the entry disappears. Adding to `.zprofile` doesn't help. – Jura Brazdil May 03 '22 at 13:22
  • examine the contents of the `activate` script. It will do only 2 things: prepend some data to your PATH and make you wish to vomit. A stock activate script does not do very much, and if yours is tweaking your PATH more than simply prepending one directory to it, then something is wrong. – William Pursell May 03 '22 at 14:14
0

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.

Jura Brazdil
  • 970
  • 7
  • 15