Here is the thing, I have a proxy holding the reference to a remote module, and I put some of these proxies to the sys.modules
such that I can use it just like local modules. But some other objects are put in the __builtin__
module at the remote environment (like a magic variable for convenience of debugging or referencing). I don't want to reference these vars like conn.__builtin__.var
, and I have to either replace the local __builtin__
(which seems not working for replace sys.modules['__builtin__']
or to hook the global name finding rules.
How? For a module you can just overload a getattr
to do this. But in a interactive interpreter like IPython
, who is the main module or how to do this? update: As pointed out by @Nizam Mohamed, yes I can get the __main__
module, but still I can't modify the name lookup role of it.
I'd like to turn the local environment completely to be the remote one (for a debugging console)
UPDATE
For now I just iterate all the __builtin__.__dict__
and if there is a name that isn't in the local __builtin__
. I add the name to local's __builtin__
. But it's not so dynamic compare to a name lookup rule say if I can't find the name in local __builtin__
try the remote one.
here is a similar discussion.
And this question gives a simulation of module by replace it with a object in sys.modules
. But this won't work for __builtin__
name lookup, I've also tried to replace the __builtin__.__getattribute__
with a custom one that will first use the original lookup followed by a custom one when failed. But global name lookup of __builtin__
never called into the __builtin__.__getattribute__
even __builtin__.__getattribute__('name')
returns the desired value, __builtin__.name
or name
never returns one.
foo
was add to the remote__builtin__.foo = 1
, I don't want to access this var by sayingconn.__builtin__.foo
, which can refer directly asfoo
in the remote side. – Vasilekget_ipython().user_ns
. I'm not sure how far that will work with a dictionary-like object with overridden methods, though - that's up to Python itself. – Girothis = __import__(__name__)
refers to current module. – Oxidimetry