Error presented in vscode when trying to run a Python code

Asked

Viewed 1,818 times

-1

Person, I’m trying to run some scripts on Python, however in terminal of vscode is being showing the following error:

bash syntax error near unexpected token ('

This error is only happening on the integrated terminal of vscode, I already reinstalled the program to check if this was the problem.

I’m using a Linux Mint 19.1 - "Tessa" with python 3.7.2.

I believe this one is unrelated to the code, since a simple print("Hello World") shows the same error.

Could someone help me?

  • This has something to do with Mr. Path, fix it, it’ll work

1 answer

1

This type of error happens when the operating system is trying to directly run a Python file, but does not have an indicator that it is a Python file. Under Unix and Linux, unlike Windows, the file extension (.py for Python, but also any other) - is just perfume. The operating system looks at the first bytes of the file to understand what type of file it is, and in the case of script files, call the correct interpreter.

In short, put the line down as first line from your Python script:

#! /usr/bin/env python3

Okay, this indicates to the operating system that it should use the "env" program to find out which is the correct program, in the current environment, to run a script in python3 .

This will fix your specific error, but there may still be another configuration error in your vscode - it should know, regardless of the operating system, that your script is in Python , and call the appropriate interpreter (I can’t tell if vscode looks at the file extension or what it does). The error you put is most common when running a Python program straight from the terminal.

  • When the file has no extension and also the shebang (#!...) that identifies your type Bash assumes it is his file and this type of error usually happens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.