This question already has answers here:
How to quickly debug misbehaving script in Python without pdb?
(3 answers)
Closed 9 years ago.
I have a high level logic error deep within my Python script, and pdb doesn't help to debug it. Is there any other way to see what is being executed after I run my script?
NOTE: pdb is too slow and inconvenient for me. I wish I could grep over all cases when my function is executed, instead of inspecting manually each and every call, set/unset breakpoints. The state is lost when I exit pdb and its user interface is more confusing than helpful - requires docs at hand.
I found a way to do this using excellent trace module that comes with Python.
An example how to troubleshoot module installation problem:
python -m trace -t setup.py install > execution.log
This will dump all source line of setup.py install execution to execution.log. I found this to be more useful than pdb approach.
Related
This question already has answers here:
How to step through Python code to help debug issues?
(15 answers)
Closed 2 years ago.
I want to observe the data flow in a package that is executed in the shell. Is the freqtrade bot. I want to run it step by step so I can observe the dataflow. This have to be executed in the shell with some parameters e.g.:
"freqtrade trade -s strategy"
How could I do this from the shell or from vscode allowing me to go through each step, like if it was debugging mode (which I could not do for the package either)?
there is a debugger called pdb. Check here
This question already has answers here:
How to step through Python code to help debug issues?
(15 answers)
Can I put a breakpoint in a running Python program that drops to the interactive terminal?
(6 answers)
Closed 2 years ago.
To clarify my question, for example in some IDEs such as PyCharm one can put a breakpoint in the code, run the python script and when the code reaches that breakpoint, the program stops while the user can investigate some of the variables until that breakpoint.
Is such functionality possible through a terminal, that is executing a python code but while having it stopped at a certain line, a python console would be available for debugging.
Yes, you can use builtin Python Debugger import pdb; pdb.set_trace() as a one-liner to get full access to the current scope you are in, ability to call any functions, step through code, display values etc.
There's also https://pypi.org/project/pdbpp/ which needs to be installed separately. It has all the features that pdb has and then some.
i think that is not possible if u are running it in just the terminal.
if u want to investigate variables and stuff at a certain point, maybe u can simply just code lens
This question already has answers here:
Calling the "source" command from subprocess.Popen
(9 answers)
Closed 5 years ago.
Hello I am trying to source of file say check.setup from my pythons script.
Code
import os
os.system("source /fullpathhere/check.setup")
It says command not found. Surprisingly I can source the same file directly from the shell.
check.setup is csh file. It is sourcing other .csh files and setting few environment variable I saw few answers here but no one could possibly solve the problem.
PS: I tried to write bash file instead of python. I also tried using subprocess.Popen. Problem persists.
Aashish
use popen
subprocess.Popen('source ./fullpathhere/check.setup', shell=True)
Why didn't tag your posting as csh, even though you explicitly mention csh in your question....
There is no executable namend source (you can verify it by typing bin/which source. Well, you pointed out by yourself, that this is supposed to be done by csh, but how should Python know that it needs to invoke csh, if you don't tell it?
I don't have a csh on my system, but if I remember right from the time a couple of decades ago, where I indeed used csh for programming, you can do something like
os.system("csh -c fullpathhere/check.setup")
Actually, I would also specify the -f flag for skipping .cshrc, unless you really need that to be sourced as well.
There is a high level logic error deep within my Python script, and pdb doesn't help to debug it. Is there any other way to see what is being executed after I run my script?
NOTE: Using pdb is too slow and inconvenient - I wish I could grep over all cases when my function is executed, instead of inspecting manually each and every call, set/unset breakpoints. The state is lost when I exit pdb and its user interface is more confusing than helpful - requires docs at hand.
UPDATE: made it clear that pdb is not an option, so the most popular Python debugging tips can not be applied
I would recommend using pdb. You can use
import pdb
at the top of your script, and then add the line
pdb.set_trace()
somewhere in the code where you want to trace the problem. When the script gets to that line, you will have an interactive console where you can check variable values, run your own checks, and see what is going on. You can use n to execute the next line, or c to continue to the next occurrence of set_trace(). Full documentation is here: http://docs.python.org/2/library/pdb.html.
Let me know if you have any specific questions!
No, there's no magic formula.
My best suggestion is to get a good IDE with a debugger, like JetBrains' PyCharm, and step through your code to see where you went wrong.
Most of the time these situations happen because you make assumptions about behavior that aren't true. Get a debugger, step through, and check your assumptions.
I found a way to do this using excellent trace module that comes with Python.
An example how to troubleshoot module installation problem:
python -m trace -t setup.py install > execution.log
This will dump all source line of setup.py install execution to execution.log. I find this more useful than pdb, because usability of pdb command line interface is very poor.
This question already has answers here:
Passing options to Python executable in non-interactive mode
(4 answers)
Closed 7 years ago.
I would like to execute a script work.py in Python, after executing some initialization script init.py.
If I were looking for an interactive session, executing python -i init.py or setting PYTHONSTARTUP=/path/to/init.py would do the trick, but I am looking to execute another script.
Since this is a generic case which occurs often (init.py sets environment and so is the same all of the time), I would highly prefer not referencing init.py from work.py. How could this be done? Would anything change if I needed this from a script instead of from the prompt?
Thank you very much.
More generally than in the accepted answer of C0deH4cker, this is discussed in the Python manual in Section 2.2.5 - Cusomization Modules. The basic idea is, to get the location of the special start-up script, one needs to execute the following Python code, e.g. from the interactive session of the interpreter:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
The output should be exactly such a location, in the file sitecustomize.py.
Python has a special script that is run on startup. On my platform it is located at /usr/lib/python2.5/site-packages/sitecustomize.py IIRC. So, you could either put init.py in that directory alongside a sitecustomize.py script that imports it, or just paste the content of init.py in the sitecustomize.py.