A quickie: python, terminal "print command not found"
Asked Answered
S

3

6

Been using terminal to run python scripts written in textwrangler for about 18 months. Decided to look at possibility of moving to an IDE so downloaded a couple of trial versions. Just downloaded BBEedit and suddenly having problems executing script, either from BBedit or Textwrangler. The following code:

print "Please work"

for i in range(50):
    print i

yields the following error message:

/Users/paulpatterson/Documents/Python/Scripts/t.py: line 1: print: command not found
/Users/paulpatterson/Documents/Python/Scripts/t.py: line 3: syntax error near unexpected token `('
/Users/paulpatterson/Documents/Python/Scripts/t.py: line 3: `for i in range(50):'

Some files still work okay, but I'm struggling to figure out why others now aren't - not even sure if BBedit download has caused the problem. Can anyone help?

State answered 30/11, 2010 at 15:3 Comment(0)
D
25

Try putting

#!/usr/bin/env python

at the top of the script. The program is trying to execute it like a shell script instead of running it through python.

Deviled answered 30/11, 2010 at 15:5 Comment(1)
No worries, Paul. We all learn this by being bitten by it a few times. :)Deviled
I
1

It's running the script as a shell script, not a Python script.

Interpretative answered 30/11, 2010 at 15:5 Comment(0)
A
0

Also, be aware that this form of print (print-as-command) is changing to print-as-a-function when you convert to Python 3. So:

print "please work"

will have to be changed to:

print ("please work")
Aldridge answered 30/11, 2010 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.