Matuku
Matuku

Reputation: 949

IPython import local file

I've come across a weird disparity between IPython and the default python interpreter. I have a python file that shadows a built-in module's name: logging.py. Say it has a simple method foo().

If I start up the default python interpreter and call import logging it imports the local file and I can access logging.foo().

If I start up IPython and call import logging it imports the python built-in module. If I change the name to a non-shadow (e.g. import my_logging) then the import will work as expected.

Which is the expected behaviour? The current directory is at the start of sys.path for both interpreters but they differ in which imports have priority.

Upvotes: 4

Views: 1002

Answers (1)

BoppreH
BoppreH

Reputation: 10173

import sys
print(sys.modules)

IPython starts with most of the standard library imported, including logging. Those modules seem to be imported by full path.

Speculation: IPython already imported those libraries by full path for internal use, now when you import logging again it checks that the module is already imported, regardless of the path, and does nothing.

Upvotes: 2

Related Questions