-2

When I click "run python file in terminal", it gives me the following error:

>>> & C:/Users/HP/python/anaconda/python.exe "d:/python/vscode/python course/3. pandas/dataframes 231021.py"
  File "<stdin>", line 1
    & C:/Users/HP/python/anaconda/python.exe "d:/python/vscode/python course/3. pandas/dataframes 231021.py"
    ^
SyntaxError: invalid syntax

However, when I manually select the lines of code and press shift enter to run selected lines of code, it runs perfectly in vs code.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 1
    Can You share your code, so that I can see if there's something wrong in the code ? – Kanishk Mewal Oct 24 '21 at 07:09
  • 2
    It looks like you are running a shell command in the Python interpreter, which indeed would be invalid syntax. – Mark Tolonen Oct 24 '21 at 07:14
  • Yes I initially thought there was something wrong with my code at first, however I tried it again with a simple code to print hello world and it gave the same error. – cheesecakesss Oct 24 '21 at 07:19
  • 4
    Does this answer your question? [Invalid Syntax error when running python from inside Visual Studio Code](https://stackoverflow.com/questions/51540391/invalid-syntax-error-when-running-python-from-inside-visual-studio-code) – Gino Mempin Oct 24 '21 at 07:22
  • @GinoMempin, I have already seen that post, unfortunately its my first time using VS code so I didn't really understand the solution. – cheesecakesss Oct 24 '21 at 07:29
  • @cheesecakesss You will have to be more _specific_. If you've already seen that post, then please [edit] your question to describe which of those answers you've already checked/tried, what happened, and what specifically you didn't understand about those answers. – Gino Mempin Oct 24 '21 at 07:39
  • The answer on the post said to use the exit() command in the console. I don't understand what console this is. – cheesecakesss Oct 24 '21 at 07:45
  • The one where your syntax error is appearing. The one with the `>>>`. It's called the python shell or python console. See https://docs.python.org/3/tutorial/interpreter.html#interactive-mode. The answer means to hit the enter key until you get a blank `>>>` prompt, then type in `exit()`. – Gino Mempin Oct 24 '21 at 08:39

3 Answers3

0

Go to the terminal and on the right side you will see a docked panel named powershell and when you hover over it it will show a dustbin ui(image like a dustbin). If you have multiple tabs close everything by clicking on the dustbin. Doing this, the terminal will close then simply run the code again and the problem should be solved!(It worked for me after fiddling for 3 days)

Note: Everything should be closed on the terminal right side panel. If the terminal doesn't closes, try closing vs code and open again and do the same thing. Mine had python opened too in the docked panel.

0

The problem is that you didn't exit python first. Python is giving you the syntax error, not the shell.

Just type in quit() into the terminal window, then re-run and it will work fine.

I had the same problem before I realized that shift-Return to run certain lines uses the open interpreter, but run file assumes you are starting at a shell prompt.

LW001
  • 2,452
  • 6
  • 27
  • 36
-1

I will suggest check the python version both on the command prompt and within the VS Code. I suspect they point to different versions. In this case you create virtual environment using venv and share the same in both cases so that you will get consistent output.

DeepakP
  • 30
  • 3