How do I get 'realpath' to find my symbolic link?
I believe you're running afoul of Apple's system for managing and switching among multiple versions of the same program. You can accomplish what you want, less elegantly but without problems, with the following script named python2
:
#!/bin/bash
exec /usr/bin/python "$@"
Make it executable (chmod +x python2
), and you're in business.
Explanation of the problem:
When you run /usr/bin/python
, it finds and executes python2.7
in the same directory. Your symbolic link fails because the system follows the symlink to /usr/bin
, then looks for and fails to find python2
there. You can get one step further by using a "hard link" instead of a symbolic link:
rm python2
ln /usr/bin/python python2
Now there's no symbolic link to follow, just two filenames for the same file (inode). But now I fail with the following message:
python2: posix_spawn: /Users/alexis/.../python22.7: No such file or directory
Notice the python22.7
: The framework is adding 2.7
to the name you've created! Instead of trying to unravel this and set up a forest of links that matches its expectations, I recommend you stay out of the way of the version management framework, and use the solution suggested above.
PS. There might be a better solution: If you would explain what you needed to do to begin with (why do you need to provide python2
as an alias for python
), someone can probably help you do it in a different way. This is known as an "XY problem" in stackexchange lingo...
Try:
ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/local/bin/python2
If you need to resolve (or investigate) a symlink you can use the platform independent bash library 'realpath-lib'. By default it emulates readlink and will work on Mac or Unix. It can be found on Github or Bitbucket and it's free.
But it sounds like you want to just do python2 (rather than ./python2) from your local (working) directory. It might be possible to do this with an alias in your .bashrc or otherwise you will need to add the working directory (that contains your symlink) to your PATH environment variable. This can also be done for the current session only or within the .bashrc file for future sessions. This could be a solution for just a specific user.
Another option that would work for all users would be to create the python2 symlink to /usr/bin/python in another directory on the path, say in /usr/local/bin. Perhaps something like:
sudo ln -s /usr/bin/python /usr/local/bin/python2
Then any user or script should find the python or python2 commands. Of course this option requires admin (root) privileges to install.