Is it possible to change the code while debugging in VSCode and that the change will take effect immediately without rerun the code?
I'm using Microsoft Python extension.
It depends. There's no such thing as a hot reload in Python. The closest you can come is importlib.reload(), but realize that only reloads the module and not the objects which already exist in memory. IOW it typically doesn't do what you want in code (it's usually used in the REPL).
So, apparently that the closest way of doing it is to use Jupyter capabilities (that can be use via VSCode).
With Jupyter you can split your code to multiple cells and run every each one of them separately without run all from beginning.
Related
I want to know becouse as far as i know how the python code works it is slower becouse is an interpretated languaje need to do some extra work in running time thus it is slower, but if i compiled it, it wuld be more performant? or ot will be the same? and what will be the difference between them? other thean performance and when i should compile the code? when i need no other dependencis of python interpretator?, larger proyects or somthing else?
I wanted to ask because there are a lot of peopol new in programing using python and that will be very useful information to know
When you use py2exe, that basically creates a big zip file that contains your source code, the modules you need, and a copy of the Python interpreter. When you run it, it unzips into a miniature self-contained Python environment and runs the interpreter. There is no difference in performance.
I write code mainly in Python or R depending on the situation. However, one thing I REALLY like about R is that it saves your data into the IDE. So you can continue doing stuff on this data without the need to run all the code again each time. Is this even possible in Python, as elegant as R, or do I just have to save a file onto my HDD every time I compute some data that takes some time ?
I recommend changing your IDE from what ever you are using to something like Visual Studio Code and downloading the Jupyter extension or using Jupyter Notebooks. It does precisely what you are asking for, saving variables during the session so you can modify them on the fly.
Compared with other IDEs, Visual Studio Code has the best support for jupyter that I have seen without using the notebook entirely based on my experiences using various IDEs.
This is a general question so codes not included here.
Essentially, I wrote python functions that I need to use in sublime and make sure I have saved after every modification. A jupyter notebook is running along side while I am making changes to the python file.
The problem is, as I intend to do debugging within the jupyter notebook, even though I ran the import after every modification of the python file, the effect of the modification does not show up in the jupyter notebook.
Can anyone tell me why is this? Do I have to shut down the localhost everytime to import the most recent version of the python file? Is there a way to avoid this?
Thanks a lot!
If a module has already been imported, importing it again won't reread the file from disk. You would have to use reload(module). (See more information about similar issues here.) It's not totally clear from your statement if that's the source of your problem but it's a reasonable guess.
complete Python noob here (and rusty in C).
I am using a Mac with Lion OS. Trying to use NFCpy, which uses USBpy, which uses libUSB. libUSB is crashing due to a null pointer but I have no idea how to debug that since there are so many parts involved.
Right now I am using xcode to view the code highlighted but I run everything from bash. I can switch to Windows or Linux if this is going to be somehow easier with a different environment.
Any suggestions on how to debug this would be much appreciated ;-)
PS: It would be just fine if I could see the prints I put in C in the bash where I run the Python script
You should see your printf() you put in C in your terminal, something is already wrong here. Are you sure that you're using the latest compiled library? To be sure, instead of print, you can use use assert(0) (you need to include assert.h).
Anyway, you can debug your software using gdb:
gdb --args python yourfile.py
# type "run" to start the program
# if you put assert() in your code, gdb will stop at the assert, or you can put
# manual breakpoint by using "b filename:lineno" before "run"
Enable core dumps (ulimit -Sc unlimited) and crash the program to produce a core file. Examine the core file with gdb to learn more about the conditions leading up to the crash. Inspect the functions and local variables on the call stack for clues.
Or run the program under gdb to begin with and inspect the live process after it crashes and gdb intercepts the signal (SIGSEGV, SIGBUS, whatever).
Both of these approaches will be easier if you make sure all relevant native code (Python, libUSB, etc) have debugging symbols available.
Isolating the problem in a program which is as small as you can manage to make it, as Tio suggested, will also make this process easier.
PS: It would be just fine if I could see the prints I put in C in the bash where I run the Python script
You didn't mention anything about adding prints "in C" elsewhere in your question. Did you modify libUSB to add debugging prints? If so, did you rebuild it? What steps did you take to ensure that your new build would be used instead of the previously available libUSB? You may need to adjust your dylib-related environment variables to get the dynamic linker to prefer your version over the system version. If you did something else, explain what. :)
I am new to python and haven't been able to find out whether this is possible or not.
I am using the PyDev plugin under Eclipse, and basically all I want to find out is, is it possible to edit code whilst you're sitting at a breakpoint? I.e. Edit code whilst you're debugging.
It allows me to do this at present, but it seems to still be executing the line of code that previously existed before I made changes.
Also, are you able to drag program execution back like you can in VBA and C# for example?
If either of these are possible, how can I enable them?
PyDev supports this to some extend since version 1.4.8, see the change notes and the corresponding blog entry.
When you start a Python program, it will be compiled into bytecode (and possibly saved as .pyc file). That means you can change the source but since you don't "open" the source again, the change won't be picked up.
There are systems like TurboGears (a web framework) which detect these changes and restart themselves but that's probably going to confuse the debugger.
Going back in time also isn't possible currently since the bytecode interpreter would need support for this.
You can run arbitrary commands in the console during the breakpoint. For my needs, this typically achieves the same purpose as coding live, although I do wish it were as elegant as simply using the editor.