I am a python newcomer and have problem with executing my input code. My code is as follows:
userInput = input("Hi, What is your name? ")
print("Welcome {0}, Nice to meet you!".format(userInput))
Now the problem is that it runs perfectly fine in PyCharm 2020
, yet when I try to run it in my Ubuntu 20.04
's terminal, it returns the following error:
./userIO.py: line 7: syntax error near unexpected token `('
./userIO.py: line 7: `userInput = input("Hi, What is your name? ")'
I wonder what could the cause be. Can you please help me find what can cause the problem?
When running scripts (e.g. by doing ./script
at a command line), Linux uses a special line known as a shebang line to figure out what program is used to run the script. For example, #!/bin/bash
for Bash scripts, or #!/usr/bin/env python3
for Python scripts.
If there is no shebang line, Bash will default to running the script in Bash (i.e. as a shell script). Since your script is not a shell script, you get a bunch of errors from Bash attempting to interpret your Python code as a shell script.
To fix this, you need a shebang line, which must be the first line of your script. For a Python 3 script, you may use #!/usr/bin/env python3
, e.g. as follows:
#!/usr/bin/env python3
userInput = input("Hi, What is your name? ")
...
Alternatively, you can fix this by explicitly running your script with the Python interpreter, e.g. python3 script.py
.