0

In VSCode, I can run Python code from a .py file by selecting the code in the editor then typing shift+enter. It runs without error and opens a Python terminal (prompt turns to >>>). However, when my prompt turns to >>>, the below approaches to running code produce both SyntaxError: invalid syntax and KeyboardInterrupt errors:

  • Left-clicking the arrow button near the top right of the screen and selecting "Run Python File"
  • Right-clicking the editor and selecting "Run Python File in Terminal"

Other approaches involving the arrow button, right-clicking the editor, or use of shift+enter do not produce these errors. Additionally, if I use any approach that doesn't produce the >>> prompt, none of these produce an error.

Can anyone explain what is going on? I can't find any documentation explaining what is happening with the terminal and how it relates to these errors. FWIW, I'm working with a virtual environment containing only numpy.

Here's an example .py file:

import numpy as np

msg = "Roll a dice!"  # in-line comment

print(msg)
print(np.random.randint(1,9))
print(5+5)  # in-line comment
0

1 Answer 1

1

>>> means python.exe is running.

SyntaxError: invalid syntax:

>>> xx/Programs/Python/Python313/python.exe test.py
  File "<python-input-0>", line 1
   xx/Programs/Python/Python313/python.exe test.py
      ^
SyntaxError: invalid syntax

It means executing cmd in a python program. Of course this is wrong syntax.

Input exit() to exit python first.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.