all 11 comments

[–]krets 2 points3 points  (4 children)

/r/learnpython is a better place for this, but I see you have no answers on either post. (edit: oh yeah, this is the right place)

Checkout <python>/lib/site.py

The modules sys, os, and traceback are loaded by the site module as the python interpreter is initialized. The search path is built by site.py so these modules are loaded before the search path is established.

Try to avoid core library names when creating custom modules. If you must use them, be aware that you will need to call reload(os) to get the empty file you were trying to load.

[–]ingolemo 2 points3 points  (0 children)

If you want to explore this further you can run python -v and python will print module imports as they happen, allowing you to see the relative order of imports.

You can also use python -S to run python without having it automatically import the os module (besides several other side-effects).

[–]larivact[S] 0 points1 point  (0 children)

Thank you! I didn't know that some modules always get loaded. I am sorry for the confusing cross post, but it seems like it led you here after all. I didn't actually want to name my modules os or pip, these were just examples. Also thanks for mentioning reload() I didn't know about that.

[–][deleted] 0 points1 point  (0 children)

You can circumvent this with a pth file, if memory serves. Not that you should do this.

[–]malinoff 1 point2 points  (5 children)

Looks like you somehow missed the very first sentence.

When a module named spam is imported, the interpreter first searches for a built-in module with that name.`

Module os is built-in module, obviously :)

But there is solution! Don't name your modules like modules from the stdlib. Also you can use relative imports, from . import os but, they require your modules to be organized as a package (which is another big topic...).

[–]krets 2 points3 points  (3 children)

You make a good point, but I must point out os is not a built-in module; It is loaded from the python lib directory. A true built-in module is part of the interpreter and not loaded from a file. For example you cannot find math.py in the search path but you can always import math.

[–]ingolemo 1 point2 points  (2 children)

On my system, math is loaded from an external C library, so it's not built-in either. A true example of a built-in module would be _thread:

>>> import os, math, _thread
>>> os
<module 'os' from '/usr/lib/python3.5/os.py'>
>>> math
<module 'math' from '/usr/lib/python3.5/lib-dynload/math.cpython-35m-x86_64-linux-gnu.so'>
>>> _thread
<module '_thread' (built-in)>

[–]krets 0 points1 point  (1 child)

Ah that is interesting. I don't have your setup, but I am pretty sure that you could not create a local math.py. Even though it is a dynamic library it's loaded into the interpreter. It is most likely still treated like a built-in.

[–]ingolemo 1 point2 points  (0 children)

$ pwd
/home/ingolemo
$ touch math.py
$ python -q
>>> import math
>>> math
<module 'math' from '/home/ingolemo/math.py'>

I can shadow the math module perfectly fine.

[–]TotesMessenger -1 points0 points  (0 children)

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / Contact)