5

I recently installed both Python and Visual Studio Code. After taking an intro class I wrote a basic script and ran it in Visual Studio Code. That's when I noticed a problem with the way Python is setup in my Visual Studio code. Problem: When I start Visual Studio Code and open a python file, the Terminal defaults to "C:\Users\my_name\Documents Python" (this is the folder my python files are stored in). From what I understand, when you're in Python, the prompt should be ">>>". I am able to run my script but i cannot run any other Python code (ie something as simple as z = 5) in the terminal. If I type in "Python", I am prompted with ">>>" but can no longer run my script.

I thought this was an installation issue so i uninstalled and reinstalled both Python and Visual but the problem persists.

I tried adding the Python file path to where the program is installed to the windows environment under system settings and also clicked "Add to Path" when reinstalling Python but none of these solutions seemed to work.

when basic python code (ie z=5) doesn't seem to work but the script runs fine I get the error message below:

PS C:\Users\my_name\Documents\Python 2> z=5
z=5 : The term 'z=5' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ z=5
+ ~~~
    + CategoryInfo          : ObjectNotFound: (z=5:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Below is the error message I get after I switch to python and try running my script

PS C:\Users\my_name\Documents\Python 2> python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> & C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
  File "<stdin>", line 1
    & C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
    ^
SyntaxError: invalid syntax
>>>

4 Answers 4

3

There is nothing wrong with your setup. It looks you miss basic understanding of different ways to execute python code.

VS Code has integrated terminal. From there You can run your python script i.e. file with py extension same as you are on terminal/cmd/powershell. That is common way to write and execute code.

When you type python and hit enter you start python interactive shell. That is when you get >>> prompt. Your python interpreter evaluate and execute each line as you type in and hit enter. Same will happen if you type in python in cmd/powershell outside VS Code. Interactive mode is used more or less to experiment, test simple ideas, simple code examples, etc. but the code you type in is lost once you exit the interactive mode by >>>exit().

for further reference you may check

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

3 Comments

Thanks Buran. I still don't understand why I can't enter something as simple as "z = 5" and test simple python code in the terminal after running the script. A friend of mine who is also using Python always has ">>>" as the prompt and can run scripts along with use the terminal for testing code.
from >>> (i.e. interactive prompt) you cannot run script. There you can eneter and execute python code line by line. And when you are in terminal (not python interactive mode) you cannot do z = 5 because it's unknown command. You will get the same error when try to run it in cmd. Did you check the links I shared?
The only possibility that comes to my mind is that your friend has started 2 (two) integrated terminals in VSCode and switch between them (from the dropdown in the top-right corner of the terminal).
1

After reading the documentation above, I also had a similar problem after running scripts. I think that VSC is exiting out of python after running a script and requires starting up the python terminal again. I wonder if VSC can default to python after running a script instead of exiting out to powershell.

Comments

1

I figured out what the issue was here. Visual code thought Python was installed in the directory that my .py files are saved in as opposed to the actual location under program files. The path had to be edited under settings.

1 Comment

Ok, but how did you go about it? (show code please)
0

When you are in debug mode and stopped, you can type interactive Python in the 'Debug Window'. Useful for inspecting objects, variables, etc.

Otherwise as suggested, I just type "Python" in the terminal to get the interactive Python prompt ('>>>').

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.