Cannot start Python.py files from terminal on Mac

I just downloaded the Python 3.2 environment on Mac OS 10.6. I am new to programming and trying to run my first standalone .py file, but I keep getting the error message "There is no such directory or file." The file name is "script1.py" and I saved it in / Users / billp / Documents. When I open the terminal to run the file, I print:

python script1.py

I also tried adding this line to the top of the script:

#!/usr/local/bin/python

Like this one:

#!/usr/bin/env python

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

+5
source share
4 answers

Make sure you are in the correct working directory after opening the terminal. A type

cd /Users/billp/Documents/

(use the tab for autocomplete)

then

python ./script1.py

python . shebang #! script , : ./script1.py, chmod +x script1.py (python) shebang. #!/usr/bin/env python python .

./ . , script1.py ( , ), , PATH env. script1.py , -bash: script1.py: command not found. python, , PATH, .

+6

, ? :

$ chmod +x script1.py

Python Mac (, , UTF-8:

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

, , python . , python, script , ./script1.py , .

, script ( ) :

if __name__ == '__main__':
    # the code you want to call
+4

python ?

/Library/Frameworks/Python.framework/Versions/3.2/bin/python
0

, , . , .

, :

( )

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

( )

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

As long as you work with files in the same directory (usually called your working directory), you can always use relative paths safely. If the files are located elsewhere, you should always specify the absolute path.

0
source

All Articles