2

I have 2 different versions of pipenvs in my $PATH:

$ where pipenv
/usr/local/Caskroom/miniconda/base/bin/pipenv #1
/usr/local/bin/pipenv #2

and I want to shadow pipenv #1 so that #2 can take precedence, while

  • keeping /usr/local/Caskroom/miniconda/base/bin before /usr/local/bin in $PATH.
  • no alias pipenv='/usr/local/bin/pipenv'.
  • no running mv /usr/local/Caskroom/miniconda/base/bin/pipenv{,.bak},
    or rm /usr/local/Caskroom/miniconda/base/bin/pipenv (conda will install pipenv in that location again when pipenv is upgraded in the future),
    or uninstalling pipenv #1 (it's a dependency of some conda packages),
    or something like that.

How do I do that?

Teddy C
  • 457
  • Remove the executable flag from the local pipenv, but this way you have to execute the local pipenv explicitly with preceded python interpreter. But maybe this will result in unpredictable errors ... – ChristophS Sep 01 '21 at 06:50
  • @ChristophS I think the executable flag will be reset by conda when pipenv is upgraded in the future, and that's also why I don't want to rm or mv it. – Teddy C Sep 01 '21 at 06:53

1 Answers1

4

Put another directory, such as ~/bin, ahead of all the others in your PATH. Make a symbolic link to your preferred pipenv in that directory. For example:

  • Put this line in your .profile:
    PATH=~/bin:$PATH
    
  • Do this once:
    mkdir ~/bin
    ln -s /usr/local/bin/pipenv ~/bin
    

You can also choose which pipenv to invoke dynamically if you need to. (For example if your home directory is shared between several machines and you want to have a different preferred pipenv on different machines, or if you want a convenient way to select your preferred pipenv.) If you want to do that, instead of a symbolic link, create a wrapper script with content like the following and make it executable (chmod +x ~/bin/pipenv).

#!/bin/sh
if [ -x /usr/local/bin/pipenv ]; then
  pipenv=/usr/local/bin/pipenv
elif [ -x /usr/local/Caskroom/miniconda/base/bin/pipenv ]; then
  pipenv=/usr/local/Caskroom/miniconda/base/bin/pipenv
else
  pipenv=/usr/bin/pipenv
fi
exec $pipenv "$@"
  • This is what I need, thank you! I did something like this for other executables before but never really liked it, because I don't know how to manage all those symlinks / wrapper scripts in ~/bin when I migrate to a new machine. How would you manage them? – Teddy C Sep 01 '21 at 07:04
  • @TeddyC I have two directories under $HOME in my path, both under version control: one that contains the same file on every machine, and one for which I have per-machine or per-group-of-machine variants. For a need like yours, I would probably put a wrapper script in the per-machine directory. – Gilles 'SO- stop being evil' Sep 01 '21 at 07:09