Error presented in vscode when trying to execute a code in Python

-1

Person, I'm trying to run some scripts in Python , but in terminal of vscode is showing the following error:

bash syntax error near unexpected token ('

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

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

I believe this is not related to the code, since a simple print("Hello World") has the same error.

Could anyone help me?

    
asked by anonymous 31.12.2018 / 17:22

1 answer

2

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

In short, put the below line as the first line of your Python script:

#! /usr/bin/env python3

Ready, this tells the operating system that it should use the "env" program to find out which program is correct 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 do not know tell whether the vscode looks at the file extension or what it does). The error you put in is more common when you run a Python program from the terminal.

    
31.12.2018 / 19:43