How to set repl globals from code files? #15103
Replies: 3 comments 2 replies
-
How do you run your program?
Or perhaps yet another way? |
Beta Was this translation helpful? Give feedback.
-
I hope I understand your question correctly. Whenever I need to access a module variable, I just prepend the module name to the variable, for example: # hi.py
hi = 'hello'
name = 'Ali'
def hello():
print(f'{hi} {name}') Then I will be able to do this: >>> import hi
>>> hi.hello()
hello Ali
>>> hi.hi='Hello'
>>> hi.name='Baba'
>>> hi.hello()
Hello Baba
>>> hi.what = 123.45
>>> dir(hi)
['__class__', '__name__', '__dict__', '__file__', 'name', 'hi', 'hello', 'what']
>>> print(hi.what*2)
246.9 I hope this helps to answer what you asked. |
Beta Was this translation helpful? Give feedback.
-
This can very much be done with aiorepl, with that package you can pass a custom global object to the repl when you start it. I often preload my aiorepl global with other imports and variables so they're ready to use without manually importing every time. Aiorepl is then always available, even while your application is running. You don't need to break / interrupt the application to get to it. I'm not sure if this is possible at all from the base / startup repl. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to, when the running program breaks into a repl (either through an exception or Ctrl-C), to set some variables to be accessible in the REPL. I've tried catching the KeyboardInterrupt, setting
globals()['foo'] = foo
then re-raising, but while that changesglobals()
with the exception handler, it doesn't pass it through to the repl. Any suggestions?Beta Was this translation helpful? Give feedback.
All reactions