9

I just downloaded Python 3.2 to Mac OS 10.6 environment. I'm new to programming and am trying to run my first stand-alone .py file, but I keep getting an error message saying "no such directory or file." The name of the file is "script1.py" and I saved it to /Users/billp/Documents. When I open the Terminal to run the file I type:

python script1.py

I've also tried adding this line to the beginning of the script:

#!/usr/local/bin/python

As well as this one:

#!/usr/bin/env python

Yet, I keep getting the same error message. Any help would be greatly appreciated.

asked Oct 21, 2011 at 22:33
7
  • can you show us the error as a whole? Commented Oct 21, 2011 at 22:33
  • What happens when you type just python at the prompt? Do you get the interpreter? Commented Oct 21, 2011 at 22:38
  • 1
    Keep in mind that you have to cd to the proper directory, i.e. cd /Users/billp/Documents. Or you can do python /Users/billp/Documents/script1.py. Commented Oct 21, 2011 at 22:53
  • 1
    Here is the error as a whole: python: can't open file 'script1.py': [Errno 2] No such file or directory william-peirces-mac-pro:~ billp$ Commented Oct 22, 2011 at 1:55
  • When I type python at the prompt I get this: Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> which I believe is the default version of Python that comes loaded with OS 10.6 Commented Oct 22, 2011 at 1:57

4 Answers 4

14

Make sure you are in the right working directory after opening terminal. Type

cd /Users/billp/Documents/

(use tab to autocomplete)

then

python ./script1.py

This way you are launching python executable and passing it path to your file as the first argument. The shebang #! line you mentioned allows you to launch your script directly, like this: ./script1.py, but you need to mark the file as executable chmod +x script1.py and provide path to interpreter (python) after the shebang. #!/usr/bin/env python references your default python installation.

The ./ stands for current directory. By default when you type script1.py your shell (which is the thing that you type commands into, through the terminal) would look for executable file in special folders listed in PATH env variable. script1.py is not usually there, so you would see -bash: script1.py: command not found. python, on the other hand is on the PATH so it should work.

answered Oct 21, 2011 at 22:42
Sign up to request clarification or add additional context in comments.

5 Comments

SWEET!! It ran after I specified the directory. Thanks lormus. As for using #!/usr/local/bin/python and marking the file as executable with chmod +x, I'm not sure I understand your instructions. Like this:
(Oops, hit return accidentally) ...As for using #!/usr/local/bin/python and marking the file as executable with chmod +x I'm not sure I understand your instructions. 1) I include the #! line listed above as the first line in my script, 2) I type './script.py' at the prompt after I open the terminal 3) I mark the file as executable with chmod +x script1.py -- how do I do that? When/where do I type that command? btw, thanks for being so basic and descriptive in your explanations -- that's extremely helpful.
Switch the 2) and 3) :). You type chmod +x script1.py at the terminal and it marks the file as eXecutable, as opposed to normal data files. (In Windows world extension '.exe' says the same thing, while here it is this 'execute bit') Then you are able to launch the file with ./script1.py.
I still can't get the file to work as an executable. Again, the first line in my script is as follows: #!/usr/local/bin/python Here is what my terminal entries look like william-peirces-mac-pro:~ billp$ cd /Users/billp/Documents/ william-peirces-mac-pro:Documents billp$ chmod +x brian.py william-peirces-mac-pro:Documents billp$ ./brian.py -bash: ./brian.py: /usr/local/bin/python: bad interpreter: No such file or directory Could this be a problem with the path to Python in the script? I installed 3.2 myself, but I think the OS considers 2.6 the default version (if I'm making sense).
I figured it out. The path in my script needs to be this: #!/usr/local/bin/python3 Thanks again for your help. Off and running now...I hope.
4

Maybe you forgot to make the file executable? Try this at the command prompt:

$ chmod +x script1.py

I prefer to start my Python scripts in a Mac with these lines (assuming of course that you're saving the file in UTF-8 encoding:

#!/usr/bin/env python
#coding=utf-8

Also, make sure that the pythoncommand is available in the path. If everything is set up correctly, it won't be necessary to type python first, and you can run the script directly by typing ./script1.py in the directory where it is located.

One final thing, for running a piece of code when executing the script from the command line (as opposed to simply loading the definitions in the file), write this at the end:

if __name__ == '__main__':
 # the code you want to call
answered Oct 21, 2011 at 22:36

2 Comments

As far as I know you don't need to make the file executable when it's an argument of python itself. It's only when you run it with the shebang.
@Griffin: Correct, you do not. Making it executable and providing a shebang is how you make it self-reliant.
0

Are your python binaries here?

/Library/Frameworks/Python.framework/Versions/3.2/bin/python
answered Oct 21, 2011 at 22:38

2 Comments

What are my "python binaries?" The path you typed exists and in the bin folder are several files such as: 2to3-3.2, idle3.2, pydoc3.2, python3, python 3.2, etc...
Your python binaries are the executable code that came with your python distribution. python is the most important one, but as you noticed others such as pydoc also exist in that bin directory.
0

It's worth pointing out, as long as the file is in your current directory it's automatically available. Otherwise, anything files will have to be referenced absolutely using the full path information.

So the following examples are calling the same file:

Explicit (absolute path)

python /Users/billp/Documents/script1.py
python /Users/billp/Documents/script2.py
python /Users/billp/Documents/script3.py

Implicit (relative path)

cd /Users/billp/Documents
python script1.py
python script2.py
python script3.py

As long as you're working with files in the same directory (commonly known as your working directory), you may always safely use relative paths. If the files are anywhere else, you must always specify an absolute path.

answered Oct 21, 2011 at 23:00

2 Comments

Obviously, my problem was rooted in a misunderstanding of paths and how to give direction to the python interpreter via the terminal. Both of your options work perfectly. Thank you. However, I have a question concerning the phrase "as long as you're working with files in the same directory" ...same as what? As the directory where the Python launcher resides?
That would be my second example. If you change to /Users/billp/Documents, you don't have to specify the absolute path. So that's what I meant, files in the same directory as each other. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.