Execute Python code line by line, like VBA? - python

I'm new to python and I'm having fun. So far I've only been on the road in VBA and SQL, but one thing bothers me. Is there no feature that goes through the code line by line like in VBA? This has always helped me a lot with VBA (F8), I could check the value of the variable on the fly (hold mouseover), i could check the whole code better and see where exactly it is running on a bug. Is this function not really available in python? I use PyCharm as IDE
Thanks!

You may try the following command: python -m pdb <script.py>. It will run the script in the Python debugger where you can traverse your code step by step.

There is nothing like VBA/VB6 IDE for python, unfortunately. Microsoft's IDE is simply unmatched in terms of debugging convenience. As a language, Python is more powerful, but good debugging solution is yet to come, you can't chance lines execution order or alter code on the fly in the debugger.

Related

How to run a "section" of a python script, like Matlab

When using vim (or any other text editor) for python scripts, is there a way to run a section of your script alone?
Matlab has a feature where beginning a line with %% splits the lines of code that follow into a section of its own, that can be run independently. Is there a way of achieving this without having to use iPython? Even if the solution is a bit hacky, it would be an improvement over what I currently am doing, which is commenting out blocks that I don't want to run.
A generic solution would be vim-slime: https://github.com/jpalardy/vim-slime
SLIME is an Emacs plugin to turn Emacs into a Lisp IDE. You can type
text in a file, send it to a live REPL, and avoid having to reload all
your code every time you make a change.
Vim-slime is a humble attempt at getting some of these features into
Vim. It works with any REPL and isn't tied to Lisp.

Running Python Scripts Outside of IDLE

1- i want someone to help with this part
https://automatetheboringstuff.com/appendixb/
about Running Python Scripts Outside of IDLE and sending command line args .
2- i converted my code to .exe by pyinstaller ; what's the difference between this and running it
as a script .
3-how scripts are done . i see experienced people says :"i made a script to do something for me " how is that done >?
*i'm a beginner so try to make answers simple as possible.
If you're wondering about command line arguments, look into the "argparse" library https://docs.python.org/3/library/argparse.html.
The difference in the .exe and the normal script is that that the .exe versions (conventionally) are able to be redistributed to other sytems that don't have python installed whilst still being able to run the script. If you're only making the script for yourself the only real benefit is that you dont have to enter your IDE (code editor) every time you want to run the code, however if it's still in development then you'd have to compile the code every time you made a modification if you're wanting to run the code as an executable, which is very impractical.
Your third part is very, very vague. Python can be very versatile and i recommend you continue looking at the automatetheboringstuff.com website if you're interested in making scripts that can complete repetitive activities (although i must strongly advise you against using scripts maliciously).

iteratively and interactively writing and testing python code

I'd like to iteratively write and test my python code in the Python console. I can't find a way to easily load what i've got written in the editor (somefile.py) into the Python console in Pycharm. Is there a trick to doing so?
Pycharm will let me run the entire script but that's not useful because i want to build up my state in the shell by experimenting with the functions and data i've got in the environment (kind of like how a lisp programmer would use a REPL).
Pycharm have this alt-shift-E option.
This way you can write in the editor, select the lines you want to run and run them by alt-shift-E.
I use it all the time, although I find a lot of people not fameliar with this option.

visual studio code equivalent of python -i

I am looking for the equivalent of python -i in VScode.
That is, a way to open a terminal (or run in the included terminal) the program that is open, and use the program in the read-print-eval loop, being able to call functions and inspect variables freely.
I searched for this plugin without success.
I need it to be around as simple as pressing f5 and typing something. I want to use it to teach, and my students already have many difficulties with algorithms and coding. Also, easy instalation would be ideal. I am sorry for being that 'demanding', but classes are huge and the chance of getting everyone to do a procedure go down fast with the size of the procedure.
I would not mind creating such a utility myself, if someone will point me the way, and then uploading it to whatever 'store' VScode downloads its plugins from.

How do I trace my python program from start of execution to finish?

I have a function written in python, and I want to inspect how it runs step by step from start to finish. How do I go about doing this?
I am using PyCharm as an IDE, but I don't know if it has a tracing feature.
Any tips or resources that are newbie friendly on this issue?
Thanks very much in advance!
What you're looking for is a profiler. Luckily, PyCharm is really powerful and comes with a wealth of debugging/profiling tools.
If you're running your code within PyCharm, simply set a breakpoint on the first line within the function you wish to examine, then step through it using their interface.
If you're running your code via the commandline, I highly recommend familiarizing yourself with Python's debugging module, pdb. All you need to do to examine your function is temporarily add the line:
import pdb;pdb.set_trace()
.. as the first line of your function. When you run it and it hits this line, you can step through the execution on the commandline using simple directives like 'n' for next line.

Categories