recently I switched from Spyder to VScode and I miss getting the output of python scripts in console. By that I mean:
Let's say I have a script as follow:
x = []
def append_func(x):
for i in range(10):
x.append(i)
return x
y = append_func(x)
and when I ran that script in Spyder, x would be generated, append_func would be declared and y would be generated as well. Then in console (outside of the script) I would write:
sum(y)
len(x)
len(y)
and get informations that may interest me after running the script.
So I would like play with my variables that were generated inside the script without editing the script and rerunning it.
Similar functionality is in Matlab - whenever we run the script, all of the variables and functions defined in a script are accessible in console, so we can transform/calculate/reuse them, check how some variables look like or what are their types, (...).
It's very handy when the scripts has kinda long execution time and we want to check some statistics of variables we didn't think about before.
Is such functionality in VS Code?
Screen from Spyder. As we can see x and y got saved into memory and can be used in the console:
Please use the python interactive. There are two ways in vscode, one is terminal and the other is window.
Right-click and select Run Current File in Interactive Window to open an interactive window and run the current script. You can continue to enter python code in the input box below and execute it with Shift+Enter.
Or use Shift+Enter to open a python interactive terminal, then select all the code in the file and press Shift+Enter to run code in the terminal, then you can continue to type python code and press Enter to execute.
If you want to look at the variables, clicking Variables in the interactive window will open a variables panel for you to look at.
There are more detailed instructions in the official documentation.
In addition, as you can see, there is a shortcut key conflict by default. If you want to use the shortcut key Shift+Enter to open an interactive window, you need to modify the corresponding shortcut key settings.
Related
I am used to debugging in RStudio. Using R I simply use the command: debug(my_function), then run the code and automatically the script debugs at my_function.
Now I am using Python in VS Code, I see debugging is possible by setting breakpoints visually using the red dots. However I need to debug a function without actually knowing where this function is stated. So my question is can I debug a specific function in VS Code using a command?
I hope to use something like this command line -> debug(my_function)
Thanks!
It looks impossible for now, I have submitted a feature request on GitHub.
Maybe you can try to copy the function into an isolated python file to debug it for now.
They are called Function Breakpoints
A function breakpoint is created by pressing the + button in the BREAKPOINTS section header and entering the function name. Function breakpoints are shown with a red triangle in the BREAKPOINTS section.
For example, in R you can set options(error = recover), and whenever an error occurs, it will let you inspect all the functions that are active at the time along with all the variables in their scope. It's basically like retroactively inserting a breakpoint wherever an error occurs.
Does pdb or Python have a tool like this?
If you are using VS Code as your editor, you should be able to do this.
Simply click Run > Start Debugging, and select a debug configuration
Screenshot of me using a variable that does not exist:
In JupyterLab you can run code in many different ways. And I often use the option to run the active line (where the mouse pointer is) using Run > Run Selected text or current line in console with a keyboard shortcut. After doing this, the pointer jumps to the next line, and you can keep going.
Can the same thing be done using VSCode?
And just to be clear, the following is not what I'm looking for:
Ctrl+Enter will run the whole cell.
Shift+Enter when highlighting a line or or other parts of the code will run that part.
Shift+Enter with no highlighted code runs the whole cell and inserts a new cell below the active cell.
So, how can I run only the active line without highlighting it?
A similar question has been asked here: How to run the select code in VScode?, but that sends code to the Terminal and does not provide the answer I'm looking for.
Go to file > preferences > keyboard shortcuts, search for Run Selection / Line in interactive window and assign your desired keyboard shortcut. And make sure that there are not conflicts. I found out that several tasks had Shift + Enter assigned to it, and that's what had me confused in the first place.
I have written code in a New file window in IDLE.
When I run the code there is no output.
Instead a dialog box appears showing a window accessing Python Folder 37-32.
When i closed the dialog box and the file I tried to create a new simple code below but I when I ran the code I got the same Dialog box.
What is wrong?
sum = 2+3 print(sum)
I have attached a screenshot showing the code and the dialog box that appears when the Module is run
Before you can execute your code, your first need to save the file. Thats the dialog box that popped up.
You should have seen a popup box like below. Did you? Is is unclear?
Save Before Run or Check
? Source Must Be Saved
OK to Save?
[OK] [Cancel]
One reason to require saving is that exception tracebacks refer to the file and line of lines that lead to the exception.
If you had saved, sum = 2+3 print(sum) would be a SyntaxError.
You can run single statements in Shell without saving.
The RESTART lines says that the shell re-initialized the environment for executing your code.
You should normally not save your code buried in the installation Scripts directory. Better to make a directory in your user directory, for instance, C:/Users/yourname/py/.
Yes, one should usually open a new question for unrelated questions. But without access to your machine, it is hard to know what happened with 'new'. It may be that IDLE could create a file under .../appdate/.../Scripts/, but your code cannot. If the open call did not raise and exception, it was likely created somewhere. Until you are more experienced, better to use absolute paths, such as C:/Users/yourname/py/new.txt.
I have a function hover() as below, dr is a firefox driver, image is a locator value.
If I run the function in an interpreter mode against a specific page, some more elements will display; but if I run the function in a script, I can't see the elements appear.
def hover():
elem=dr.find_element_by_css_selector(image)
hov=ActionChains(dr).move_to_element(elem)
hov.perform()
I even tried ActionChains(dr).move_by_offset(), or added some time.sleep(1) before and after the hov.perform() statement. Same results: interactive mode works, script mode fails.
I am using SPE to run the script and interpreter.
Any one has the same experience and solution for this?
Thanks.