Jupyter autoload xmlrpc.client when import xmlrpc

When I test this script in jupyter, the import of xmlrpc expose the client.

import xmlrpc
<module 'xmlrpc.client' from 'D:\\program\\Miniconda3\\lib\\xmlrpc\\client.py'>

However, when I test it in repl of ipython, the import doesn’t expose the client.

In [1]: import xmlrpc

In [2]: xmlrpc.client
AttributeError                            Traceback (most recent call last)
<ipython-input-2-a172956b1e35> in <module>
----> 1 xmlrpc.client

AttributeError: module 'xmlrpc' has no attribute 'client'

My question is why these results are different between jupyter and ipython.

I didn’t find any code using this package in repo of jupyer or ipython, it is confused.

Some dependency somewhere must have imported xmlrpc.client before you did. If any code imported xmlrpc.client, every import of xmlrpc will have xmlrpc.client defined. ipykernel, which is used when you are in a notebook, has lots more dependencies than a terminal IPython session, so one of those must have imported it. You can check sys.modules['xmlrpc'] and sys.modules['xmlrpc.client'] to check what’s been imported already.

1 Like

Many thanks to @minrk, the sys.modules does show the xmlrpc and xmlrpc.client .

Furthermore, does the sys.modules influence the import of xmlrpc? For example, if a and a.b in sys.modules, then, will the import a have a.b defined?

After test, I think my guess is true.

And after searching, the submodules part of import system gives me the answer.

The invariant holding is that if you have sys.modules[‘spam’] and sys.modules[‘spam.foo’] (as you would after the above import), the latter must appear as the foo attribute of the former.

It is defined by the behaviour of submodule.