Home > database >  Run line in Python terminal not working after Run file in terminal. Visual Studio Code
Run line in Python terminal not working after Run file in terminal. Visual Studio Code

Time:11-04

I just started using VSC. I have created a .py file with the line print('Hola mundo'). When I run the line (Mayus Intro in Linux at least), it works fine.

>>> print('Hola mundo') Hola mundo

But then I try to Run the file (with the play button), and this errors raises:

>>> /home/tidop/anaconda3/envs/snappy/bin/python /home/tidop/Documentos/vs_folder/Hello_World.py
  File "<stdin>", line 1
    /home/tidop/anaconda3/envs/snappy/bin/python /home/tidop/Documentos/vs_folder/Hello_World.py
    ^
SyntaxError: invalid syntax

I have enter exit() in that terminal, so a normal linux terminal is used now. Here, I use the Run file button without problems.

(snappy) tidop@tidop-System-Product-Name:~/Documentos/vs_folder$ /home/tidop/anaconda3/envs/snappy/bin/python /home/tidop/Documentos/vs_folder/Hello_World.py Hola mundo

But now I can't run just a line in here, cause it causes a bash error.

Question: How can I run indifferently lines and files in VSC?

I use Spyder and this problem does not exist...

CodePudding user response:

Did you add the python extensions in VS Code? I hope this link is useful for you. enter image description here

In the python interactive terminal, you can directly type python code and run it.

enter image description here

But if you type a system command, such as C:/Users/Admin/AppData/Local/Programs/Python/Python310/python.exe c:/WorkSpace/pytest11/demo4.py, which is the command for vscode to run the python script (that is, the command for you to click the play button to run the code), then the python interactive terminal will report an error. Since this is not python code it tells you syntax error.

enter image description here

If you want to use both at the same time, you just need to open both integrated terminals at the same time.

enter image description here

  • Related