-1

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?

5
  • 4
    How are you running it in the terminal? What command are you typing in to run it? I think you're trying to run it as a shell script. Commented Dec 8, 2020 at 6:49
  • well, you can use print(f"Welcome {userInput}, Nice to meet you!") as the f makes it a formatted string. Note that it is note supported before Python 3. I dont know if it will fix your problem. Commented Dec 8, 2020 at 6:49
  • @xpDev I don't think that will fix the issue, and their method of formatting the string is completely fine. Commented Dec 8, 2020 at 6:50
  • 2
    How do you try to run it on Ubuntu? python3 script.py or just script.py? Commented Dec 8, 2020 at 6:50
  • I feel like this may just be an issue with how they compile the code. In Windows, its like python3 userIO.py or something. Commented Dec 8, 2020 at 6:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to all friends especially nneonneo I can solve my problem in either of the two ways as below:

  1. If running it as a python script without using a shebang (so that the OS can understand how to run it), I can run the script correctly by typing:

      me@linuxbox $> python3 userIO.py
    
  2. If I like to be able to run the script by just typing ./userIO.py, then nneonneo's solution is the best.
    So to find the correct address to my python interpreter I typed: which python3 In my terminal and applied the terminal's return value (in my case it was: /usr/bin/python3) to the beginning of the file's contents as: #!/usr/bin/python3, so that I can now execute the file more easily.

Comments

0

You can change your second line of code with:

print(f"Welcome {userInput}, Nice to meet you!")

This reduces number of columns in your code.

1 Comment

OPs question wasn't about reducing the code, but about an error in Ubuntu terminal while it was working in PyCharm. Please add the supporting information on how your answer solves the error in Ubuntu terminal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.