0

I am using a mac and my end goal is for which python to return

/usr/bin/python2.7

The above path exists and it is an executable that works fine.

At present which python incorrectly returns

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

echo $PYTHON returns /usr/bin/python2.7 correctly thus issue is about fixing the result from which command.

I am using zsh.

I have already tried source ~/.zshrc and also restarted the laptop. Same outcome.

This is what I get when I echo $PATH

/Users/myname/.nvm/versions/node/v16.16.0/bin:/usr/bin:/Users/myname/.sdkman/candidates/java/current/bin:/Users/myname/.nvm/versions/node/v16.16.0/bin:/usr/bin/python2.7:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/myname/tools/apache-maven-3.8.1/bin:/Users/myname/tools/apache-maven-3.8.1/bin

This is what I have in my .zshrc file.

export PYTHON="/usr/bin/python2.7"
export PATH="$PYTHON:$PATH"

Full file for reference

export PYTHON="/usr/bin/python2.7"
export PATH="$PYTHON:$PATH"

# Believe only the above 2 lines is relevant. Leaving the rest in just in case.

export M2_HOME=/Users/name/tools/apache-maven-3.8.1
export PATH=$PATH:$M2_HOME/bin
export NODEJS_ORG_MIRROR=https://myserver-proxy
export NVM_NODEJS_ORG_MIRROR=$NODEJS_ORG_MIRROR
export NODE_ENV=development
export NODE_CERTS="/Users/name/certs.pem"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="robbyrussell"
plugins=(git)
source $ZSH/oh-my-zsh.sh

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"
export JETBRAINS_LICENSE_SERVER="http://jetbrains.something.com:443"

What am I missing?

UPDATE:

Command: ls -lah /usr/bin/python2.7
Result: ls: /usr/bin/python2.7: No such file or directory

There is no result as shown above. But I can go to usr/bin. Then typing python2.7 opens interactive shell to write python code.

Reason I am looking to resolve which command for python here.

I am installing a node project and getting the following error.

Thus trying to fix the python path to get this installation to work.

gyp info using node-gyp@3.8.0
gyp info using node@14.18.2 | darwin | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb check python checking for Python executable "/usr/bin/python2.7" in the PATH
gyp verb `which` failed Error: not found: /usr/bin/python2.7
gyp verb `which` failed     at getNotFoundError (/Users/name/projects/fe/aa/node_modules/which/which.js:13:12)

gyp ERR! configure error 
gyp ERR! stack Error: Can't find Python executable "/usr/bin/python2.7", you can set the PYTHON env variable.
karvai
  • 2,417
  • 16
  • 31
  • 1
    Possibly typo: `export PYTHON="/usr/bin/python2.7"` should be `export PYTHON="/usr/bin/"`. `$PATH` takes directories not binaries. – Maurice Meyer Feb 13 '23 at 14:32
  • @MauriceMeyer Tried that but it seems like PATH has both the paths now. Updated the question with what $PATH echo looks like. Which command still produces same result. – karvai Feb 13 '23 at 14:37
  • Please edit your question and show the output of `ls -lah /usr/bin/python2.7` – Maurice Meyer Feb 13 '23 at 15:22
  • Updated. There is no results. But the shell does exist on that path. – karvai Feb 13 '23 at 15:49
  • What's the reason, that you want `/usr/bin/python2.7` ? – Maurice Meyer Feb 13 '23 at 15:54
  • @MauriceMeyer It is interfering with an installation. Updated reason above. – karvai Feb 13 '23 at 16:12
  • If `/usr/bin` is not already on path something is very broken – wim Feb 13 '23 at 16:13
  • @wim usr/bin is in path seen above. – karvai Feb 13 '23 at 16:26
  • Have a look [there](https://stackoverflow.com/a/70245446/7216865), how to configure `node/gyp` and Python path. Btw. there are multiple similar questions on SO... – Maurice Meyer Feb 13 '23 at 17:05
  • 1
    BTW, "... go to usr/bin. Then typing python2.7 ..." is not a valid test; you'll just run the `python2.7` executable found in the `PATH`. Try `cd /usr/bin` and then `./python2.7`, it'll probably show it isn't there. – Gairfowl Feb 13 '23 at 20:20

1 Answers1

0
  1. You can't make which python return executable called python2.7 no matter the $PATH
  2. which simply looks up executable python in all the directories in $PATH and prints the first one found
  3. $PYTHON has no special meaning (just in case you thought it does)
  4. It looks like you don't have /usr/bin/python2.7 at all (which is expected from the latest macOS versions). If you don't believe ls output suggested by @MauriceMeyer, try this:
    /usr/bin/python2.7 --version
    

If you had really had /usr/bin/python2.7, then the simplest way to set up python command would be a zsh alias. Example for my setup:

 % alias python666="/usr/local/opt/python@3/bin/python3"
 % python666 --version
Python 3.10.10
 % which python666
python666: aliased to /usr/local/opt/python@3/bin/python3

That, however, wouldn't work for script shebang and similar cases. To hack around that, you need strategically placed symlinks, but it's a messy business and I never recommend that.

Nikolaj Š.
  • 1,457
  • 1
  • 10
  • 17