Closed maltezc closed 5 months ago
It seems as though you're wanting this to affect the shell in the debugger. Have you considered using pudb's custom shell feature?
I have not yet. If that is another way to go, then i will investigate it. Thank you!
hello again!
so i've hooked up the custom_shell and set up my file pudb_custom_shell.py
and started messing with it.
i've found that i need to use cons.push()
to "push" code to my custom shell. for example, if i have
cons.push("from pprint from pprint")
To use pprint in my console, i can just do `pprint("hello") and it comes out correctly.
is there a way i can run an entire .py file instead of just doing line by line in my pudb_custom_shell.py
file?
i've tried the following below and it will output a print statement but it won't import pprint.
pudb_custom_shell.py
file. one with cons.push('from pprint import pprint')
and then the one trying import a ipython_startup_script.py file and running everything in that.here is what it looks like:
""" This file shows how you can define a custom shell for PuDB. This is the shell used when pressing the ! key in the debugger (it does not affect the Ctrl-x shell that is built into PuDB).
To create a custom shell, create a file like this one with a function called pudb_shell(_globals, _locals) defined at the module level. Note that the file will be execfile'd.
Then, go to the PuDB preferences window (type Ctrl-p inside of PuDB) and add the path to the file in the "Custom" field under the "Shell" heading.
The example in this file
"""
def pudb_shell(_globals, _locals): """ This example shell runs a classic Python shell. It is based on run_classic_shell in pudb.shell.
"""
# Many shells only let you pass in a single locals dictionary, rather than
# separate globals and locals dictionaries. In this case, you can use
# pudb.shell.SetPropagatingDict to automatically merge the two into a
# single dictionary. It does this in such a way that assignments propogate
# to _locals, so that when the debugger is at the module level, variables
# can be reassigned in the shell.
from pudb.shell import SetPropagatingDict
ns = SetPropagatingDict([_locals, _globals], _locals)
try:
import readline
import rlcompleter
HAVE_READLINE = True
except ImportError:
HAVE_READLINE = False
if HAVE_READLINE:
readline.set_completer(
rlcompleter.Completer(ns).complete)
readline.parse_and_bind("tab: complete")
readline.clear_history()
from code import InteractiveConsole
cons = InteractiveConsole(ns)
cons.push("from pprint import pprint") # this works.
""" This file shows how you can define a custom shell for PuDB. This is the shell used when pressing the ! key in the debugger (it does not affect the Ctrl-x shell that is built into PuDB).
To create a custom shell, create a file like this one with a function called pudb_shell(_globals, _locals) defined at the module level. Note that the file will be execfile'd.
Then, go to the PuDB preferences window (type Ctrl-p inside of PuDB) and add the path to the file in the "Custom" field under the "Shell" heading.
The example in this file
"""
def pudb_shell(_globals, _locals): """ This example shell runs a classic Python shell. It is based on run_classic_shell in pudb.shell.
"""
# Many shells only let you pass in a single locals dictionary, rather than
# separate globals and locals dictionaries. In this case, you can use
# pudb.shell.SetPropagatingDict to automatically merge the two into a
# single dictionary. It does this in such a way that assignments propogate
# to _locals, so that when the debugger is at the module level, variables
# can be reassigned in the shell.
from pudb.shell import SetPropagatingDict
ns = SetPropagatingDict([_locals, _globals], _locals)
try:
import readline
import rlcompleter
HAVE_READLINE = True
except ImportError:
HAVE_READLINE = False
if HAVE_READLINE:
readline.set_completer(
rlcompleter.Completer(ns).complete)
readline.parse_and_bind("tab: complete")
readline.clear_history()
from code import InteractiveConsole
cons = InteractiveConsole(ns)
# import code
import os
# Get the path to the .py file in your home directory
home_dir = os.path.expanduser("~")
module_path = os.path.join(home_dir, "ipython_startup_script.py")
# Import and execute the script
with open(module_path, 'r') as file:
script_code = file.read()
exec(script_code) # commenting out this or
cons.push(script_code) # commenting out this one.
cons.interact("Press Ctrl-D to return to the debugger")
# When the function returns, control will be returned to the debugger.
my ipython_startup_script file looks like this:
print("running ipython_startup_script.py") # <-- this will output to the console from pprint import pprint # <-- this does not get imported.
i get this error if i dont put them in a function SyntaxError: multiple statements found while compiling a single statement
I've also tried button both of those lines in a function called Load()
and then called Load()
at the end.
Any ideas on where to go from here or is cons.push()
line by line in the shell file the way to go?
Is your feature request related to a problem? Please describe. Is there a way to add a hook(not sure if this is the correct usage of the term 'hook') for a PYTHONSTARTUP variable to point to a
startup.py
file that can be ran whenever pudb starts up?import pprint
orimport os
with every time i startup pudb for the python console, it would already run automatically and i would only need to typepprint.pprint(variable_to_pretty_print)
I'm able to get it to work with my ipython shell from terminal however I can't get it to work in pudb's ipython. I figure if i can get it to work for my terminal's ipython, its possible for pudb?
Describe the solution you'd like I would like for pudb to search my .zshrc file to look for a PYTHONSTARTUP or similar variable which would then point to a /my/pudb/startup.py script
Describe alternatives you've considered not sure of any alternatives yet.
Additional context Add any other context or screenshots about the feature request here.
What I tried I gave it an attempt and modified the
run.py
. I tried in the iterm2 consolepython3 debug_me.py
but no dice. if there is somewhere else I should be modifying, please let me know.