1

I've read many of the other answers on here to try and get this working, but for some reason it won't. I've already changed permissions and made the .py executable with chmod +x <myscript.py>, and I've copied the file into my /usr/local/bin which is already in my PATH by default on OSX, however, I still can't execute the script unless I'm in the directory where it's located.

In addition, I'm a little confused on how the header works. I currently have #!/usr/bin/python because when I changed it to #!/usr/local/bin to match the directory, when I ran the script my terminal said I had a "bad interpreter."

I know this topic has been on here a lot, but the other fixes haven't worked. Thanks in advance.

1 Answers1

2

If you run your script like this:

./Untitled.py

Bash will try to find the script in the current directory -- because you've invoked the script with a file path (a relative one in this case).

However, if you run the script like this:

Untitled.py

Bash will search your PATH for an executable with that name. Assuming that you've made the script executable (with chmod) and that script resides in a directory in your PATH, the script should run.

Regarding the she-bang line, rather than hard-coding to a specific Python (/usr/bin/python), a more common approach would be this:

#! /usr/bin/env python

Your script will be executed by whichever Python is active at the moment (for example, you might be using virtualenv or pyenv to switch between Pythons, depending on your current project).

FMc
  • 40,916
  • 13
  • 74
  • 131