I just started a python bootcamp and am using Microsoft Visual Studio Code (latest version with Python 3.10.5) but have a couple of questions. (apologies for the long post)
I have the following code:
def weather_condition(temperature): if temperature > 7: return "Warm" else: return "Cold" input("What temperature: ")
To my knowledge there are three options to run the code
- Right mouse click and ‘run python file in terminal
- Select lines and press SHIFT + ENTER
- RUN (with or without debugging)
However even though the script is the same, each choice shows a complete different result in the terminal.
- If I choose to run the python file, it shows the following error in the terminal:
>>> & C:/Users/..../AppData/Local/Microsoft/WindowsApps/python3.10.exe d:/..../_SCRIPTING_/Python/Python001/user_input2.py File "<stdin>", line 1 & C:/Users/fine/AppData/Local/Microsoft/WindowsApps/python3.10.exe d:/..../_SCRIPTING_/Python/Python001/user_input2.py ^ SyntaxError: invalid syntax
- If I choose select lines (same lines used as #1),
it runs the script but it displays the entire script run process in the terminal (which doesnt happen on the teacher’s visual code:
mine:
>>> def weather_condition(temperature): ... if temperature > 7: ... return "Warm" ... else: ... return "Cold" ... >>> input("What temperature: ") What temperature:
teachers: teacher’s screen
- And last but not least is the Run script (with or without debug).
Which opens a completely new Python ‘debug’ terminal. Here the script runs normally (it seems) and looks more like the teacher’s version although his screen doesn’t show ‘debug’ or the small toolbar
anywhere in his visual code.
A. So what is the difference between each of the choices?
B. Which of the 3 should I be using?
C. Why does the first option give an error even though the script is written correctly?
Advertisement
Answer
The three options you mention are all specific to VSCode, but you’re right that they are intended to give you different ways to run a script.
To answer your questions:
A. So what is the difference between each of the choices?
The first option attempts to start Python in your terminal in VSCode, running a command like:
& "C:/Program Files/Python310/python.exe" c:/project/hello.py
The error message you are seeing is because in your terminal, Python was already running and VSCode just copies and pastes the above into the terminal, expecting it to land on a waiting terminal prompt, not on the Python interactive prompt. If you ran exit()
first, to close Python and return to the terminal prompt, and then tried again, it would work.
What the command means is to start that version of Python, running your script, in the current working directory of the terminal, in the current environment of the terminal. (in the background, returning control to you, hence the &
)
The second option does something similar, but instead of issuing the command above, just puts everything you selected on the clipboard and pastes it into the terminal. If that happens to be running Python on the interactive prompt, and the text selected is actually Python code, your script may work (depending on the code, and what was run before). If it was sitting on the terminal prompt, it won’t because Windows, Linux or Mac OS doesn’t understand Python without an interpreter.
The third option is very similar to the first, but instead of just dumping the simple run command in the terminal, it instead adds a few more commands, changing drive and directory and then trying to start the script. It still tries to use the active terminal for it though and it will fail (just as the first option) if Python happened to be running there already.
So, the 1st and 3rd are very similar, but completely different from the 2nd, which is trying to paste Python code instead of terminal commands.
B. Which of the 3 should I be using?
Depends on what you need. If you have a few lines of code you just want to see the effect of, you can use the 2nd method, assuming the lines of code will work in context of what you may have run before.
If you just want to run a script, it depends on where it needs to be run. VSCode gives you some more options to set up your environment for the script to run successfully, but it doesn’t give you as much control as something like PyCharm (then again, it’s also a lot smaller and quicker to start up than PyCharm and has fewer confusing complicated controls – it’s a matter of taste and need).
C. Why does the first option give an error even though the script is written correctly?
As indicated above, it only generates that error when the terminal has an interactive Python session running (you can tell from the >>>
prompt). The third one would give you a similar error if you tried it in that setting.
Similarly, the second option will cause problems if you don’t have an interactive session started (i.e. running some python.exe
).