Giter Site home page Giter Site logo

Comments (3)

inducer avatar inducer commented on August 25, 2024

It seems as though you're wanting this to affect the shell in the debugger. Have you considered using pudb's custom shell feature?

from pudb.

maltezc avatar maltezc commented on August 25, 2024

I have not yet. If that is another way to go, then i will investigate it. Thank you!

from pudb.

maltezc avatar maltezc commented on August 25, 2024

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.

See below for 2 versions of my 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.

This version below works:

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

"""

Define this a function with this name and signature at the module level.

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.

other version trying to import and run an entire file.

"""
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

"""

Define this a function with this name and signature at the module level.

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?

from pudb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.