Displaying of this message whenever I run a code slows down the system.
Why am I getting this message and how can I get rid of this message?
In the terminal window, it says
PS E:\python\codes>
e:; cd 'e:\python\codes'; & 'C:\Program Files\Python39\python.exe' 'c:\Users\yaho2\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy\launcher' '63060' '--' 'e:\python\codes\NadoCoding\test.py'
This path information is useful, it shows us the commands executed when VS Code runs python code. "cd" is to go to the parent folder of the executed python file. The called python executable program "python.exe", the debugger "debugpy" for debugging, and the executed python script.
You could use the green operation button provided by the "Python" extension, it will show less paths and therefore less running time.
In addition, you could also try to use VS Code related extensions, such as "Code Runner".
Related
https://medium.com/never-too-late-to-machine-learn/how-to-step-by-step-setup-notepad-to-run-python-scripts-e1ce3b3ac7fe
I am reading from this tutorial linked above.
What I am doing is trying to run python for the first time on Notepad++. I've downloaded python, etc.
What I am lost on is this:
"Just copy the location of the python exe file, and let’s go back to Notepad++. The execute screen is waiting for us.
In the execute screen, paste the location of Python followed with “$(FILE_NAME)” "
What is the execute screen, where am I pasting the location to?
I hope someone can help me. Thank you.
first, you need "$(FULL_CURRENT_PATH)" for full path of file, the $(FILE_NAME) is for file name only or relative path
then you can paste like
C:\Python39\python.exe "$(FULL_CURRENT_PATH)"
for execute screen, new name for the menu is Execute NppExec Script.. see image below
to run your python script in notepad++ is quite simple:
make sure your python is correctly installed, open your console and type python, you should see something similar to the following (it should say Python X.Y.Z accordingly to the version you have installed)
now that we know that python is correctly installed, go to notepad++, open on it your desire script and go to the Run menu and select Run...
enter the following command python -i "$(FULL_CURRENT_PATH)" and press Run
You should see the following
And that is all.
-
Now that the previous worked, lets make it more reusable, repeat step 1 and 2, but instead of Run press Save..., give it a name (like python for example) and if you so desire also a keyboard binding (a key combination to press to run this command)
Now in step 1, you can pick python (or whatever you name it) instead or "Run..." to run you script (or press you key combination if you give it one)
now some additional explanation about the previous command python -i "$(FULL_CURRENT_PATH)"
the -i option is to enter in interactive mode, that is run you script and then continue executing python so you can do more python stuff with everything for your script loaded in there too, that way the console doesn't just close intermediately after your script is done running.
$(FULL_CURRENT_PATH) is a command for notepad++ to said to substitute there for the full path of your given script
Alternative command: cmd /K CD "$(CURRENT_DIRECTORY)" & python -i "$(FILE_NAME)"
This command is the equivalent to opening your console (cmd /K), move with cd to the folder where your script is (CD "$(CURRENT_DIRECTORY)") and then run your script with python in interactive mode (python -i "$(FILE_NAME)") and & is to separate both instructions (the /K is like python's -i but for the console) (the "-i" is now optional, but I prefer it with it to test other stuff in the script without need to put it on the script because doing so make little sense...)
Why you might want to use this over the other? two reason:
the first is when dealing files so you don't have to type the full path of a given file that sit next to your script so you can use just the name of said file, aka set the working directory to that where the script is located, otherwise it will be that where notepad++ is located (usually it might be like "C:\Program Files\Notepad++").
In case of an error, the windows will remain open so you can know what was the error instead of closing abruptly.
For Details, There are no errors, I use code runner, and the programing language is Python.
You can find different ways of running Python code in VS Code, including running in terminal, here: How to execute Python code from within Visual Studio Code
Ctrl + Shift + P, search for terminal, click, run code with command python -m my_script.py and Enter
If you have installed the Code Runner extension, the play button in the upper right corner will have three options to run the code.
Select Run Code, and the code result will be output in the OUTPUT window. This option is to use the Code Runner extension to run code
Selecting Run Java will output the result in the original terminal, and Debug Java will start code debugging.
If you want to use the Code Runner extension to run the code and have the result output in the TERMINAL panel. Please add the following configuration in settings.json:
"code-runner.runInTerminal": true,
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:
terminal error message
>>> & 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),
selected lines
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).
debug run
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
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?
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).
When I run python code in VSC I get this nonsense that I dont need, Is there a way to remove it? The PS C:\Users.. thing
This information in the VS Code terminal is useful information. When we click the run button, it executes the run command in "Terminal", showing us the path of the python used and the path of the executed file, which avoids the confusion of multiple versions of python.
If you want to omit the displayed paths, you could click F5 to debug the file:
(Please use "console": "internalConsole", in launch.json.)
Or use the VS Code extension such as "Code Runner".
Have you tried adding python to system path?
Are you trying to run code like this?
python main.py
This link has the process for adding python to system variable.
https://datatofish.com/add-python-to-windows-path/
Currently, in Visual Studio Code (under Windows 10 64bits), at a Python file called path\myfile.py, if one clicks with mouse right-button for context menu and then chooses 'Run Python File in Terminal', an integrated CMD terminal is open and file is automatically run there with:
python.exe path\myfile.py
After the file stops running, one is naturally left at the integrated CMD cursor.
This behavior is quite different, for instance, from what one has with an IDE like Spyder. There, when you run code (e.g. with F5), at the end one is left still at the Python cursor and can access content of variables created when code was run.
Is there a way to achieve a similar behavior in Visual Studio Code?
You can configure VS Code Python extension to use the -i command line option
Described in https://docs.python.org/3/using/cmdline.html#cmdoption-i
You only have to add the setting bellow (inside settings.json file)
"python.terminal.launchArgs": ["-i"],
This will execute the command python.exe -i path\myfile.py.
I don't know if it is a new feature, but I've been using it for while.
If you would like to use the terminal IPython, like in Spyder, you can use a different set of options, as the following:
"python.terminal.launchArgs": ["-m","IPython","-i"],
With these, VS Code will execute the command python.exe -m IPython -i path\myfile.py.
Then, it will run IPython module as a "script" (with -m option), which will use the options -i path\myfile.py, i.e., IPython will run the file and remain opened.
BTW, another thing is: you can run "cells" in Spyder's integrarted terminal (regions of code with #%%). But in VS Code it seems you can't.
I've made a question with a "work around" to run cells of Python files in VS Code Integrated terminal, which is posted Here
Yes. Open a terminal window and it's like a terminal window on your computer. You can type python filepathandname and the python script will execute like it does from the command line.
The closest you can come is to run the code under the debugger and set a breakpoint at the end to pause the exiting of the execution. Otherwise feel free to file a feature request at https://github.com/microsoft/vscode-python.