How to Fix Error: Bash: Syntax Error Near Unexpected Token '(' in Python
-
Error: bash: syntax error near unexpected token '('
in Python -
Fix
Error: bash: syntax error near unexpected token '('
in Python
Every time a Python code runs through a shell terminal such as Bash, it must point to an interpreter. If Bash cannot find a suitable way to run the file, then it will give out errors.
This guide will discuss Error: Bash: syntax error near unexpected token '('
.
Error: bash: syntax error near unexpected token '('
in Python
Python needs to be installed on your computer for the interpreter to find and run the Python files. The interpreter works differently in different operating systems.
In Windows, when we install Python, a program called IDLE
is installed on the computer, which comes with the interpreter. It runs Python codes.
In Linux, we can access Python using the shell terminal by typing the command python
. It opens the Python environment where the code can be written and run.
If the code has trouble finding the Python interpreter, it will run on whichever shell it runs. If the user runs the code from the Bash terminal, then the shell will give an error that will resemble this:
#Python 3.x
Error: bash: syntax error near unexpected token '('
Bash is a Unix command and is the default shell for most Linux distributions. It cannot understand Python code, so it gives out this error.
It might not give an error on the first line of the code and will give the error later because it might interpret some of the code as a shell command.
Fix Error: bash: syntax error near unexpected token '('
in Python
There are multiple ways to fix this error in Python. The fixes vary between Linux and Windows because these operating systems work differently.
Solutions for Linux
The path to the interpreter should be added to the code file so that the computer knows the interpreter has to run this file and not the shell terminal. We should add the following line at the top of the code file:
# Python 3.x
#!/usr/bin/env python
It runs the file from the Python interpreter, not the Bash shell. We have to note that this is not a Python comment.
Instead, this shell command starts the Python environment in the shell before running the code. The user can also run the code file on the shell by giving the python
command before the file name, like python filename.py
.
It also does the same and runs the file from the Python interpreter. If we have both Python 2 and 3 installed, we need to write python3
if we want to run the code using Python 3. And just python
if we want to run the code using Python 2.
Example Code:
# Python 3.x
#!/usr/bin/env python
print("Hello World")
Output:
#Python 3.x
Hello World
Solution for Windows
In Windows, the user can also use the python
keyword in the terminal to run the code file, but before doing so, the path to the Python interpreter needs to add to the PATH
variable of Windows. The steps to do that are:
- Search for
env
in the Windows search bar and open theEdit the system environment variables
option. - Now open the
Environment Variables
. - Now, choose the
PATH
variable and click onEdit
. - Paste the interpreter’s path in an empty field in this window.
- The path to the interpreter is now added to the user’s Windows, and we can use the
python
command to run the code files from the shell.
Now we need to write the following in a terminal to run the code:
#Python 3.x
python filename.py
I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.
LinkedInRelated Article - Python Error
- Can Only Concatenate List (Not Int) to List in Python
- How to Fix Value Error Need More Than One Value to Unpack in Python
- How to Fix ValueError Arrays Must All Be the Same Length in Python
- Invalid Syntax in Python
- How to Fix the TypeError: Object of Type 'Int64' Is Not JSON Serializable
- How to Fix the TypeError: 'float' Object Cannot Be Interpreted as an Integer in Python