1

I want run this simple python code int terminal:

#! /usr/bin/env python3

print('Hello world')

I saved this script as hello.py

I go to terminal to write down:

$ chmod +x hello.py

then I click enter. This is to allow permission to be granted. Terminal then showed me this:

-bash: $: command not found

Ok...I then write down the path to hello.py:

$ /Users/myname/Documents/MyPythonScripts/hello.py

I press enter. I was expecting terminal to print out hello world but to my horror, terminal show this:

/Users/myname/Documents/MyPythonScript/hello.py: line 3: syntax error near unexpected token `'Hello world''
/Users/hadi/Documents/MyPythonScript/hello.py: line 3: `print('Hello world')'

What's wrong here?

Btw, Running on macOS 10.13.3 and Python 3.6.3

4
  • 3
    Did you actually type the $ sign at the start of the line? If you saw it in examples, it is only the prompt, it's not part of the command. So simply enter chmod +x hello.py Commented Feb 22, 2018 at 11:20
  • It seems that your first line (the shebang line, starting with #!) isn't recognized. It could happen if your line endings are not Unix '\n', but Windows line endings. What editor did you create your script with? Commented Feb 22, 2018 at 11:41
  • Nope, I only enter chmod +x hello.py. Commented Feb 22, 2018 at 23:16
  • 1
    Oh, I got it now. I have a blank space in my python script before #!/usr/bin/env python. Sorry to waste your time. Commented Feb 22, 2018 at 23:23

1 Answer 1

1

First of all, this line:

-bash: $: command not found

tells that you've executed the "$" which is not right.

Your command to set permissions should look like this:

chmod +x hello.py

Another comment is that the shebang line should be without space:

#!/usr/bin/env python3
Sign up to request clarification or add additional context in comments.

3 Comments

The space in the shebang doesn't matter. The real problem was a space before it, as OP said in a comment. (The problem could also be caused by a blank line before it, if that's what they meant.)
The real problem was that chmod has not been executed as shown in the OP's error text
No, if the script didn't have execute permissions, it wouldn't execute at all, and you'd get a different error: "Permission denied"

Your Answer

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.