tl;dr Caching
sys
is somewhat of a special case among other python modules because it gets loaded at program start, unconditionally (presumably because a lot of the constants, functions, and data within - such as the streams stdout
and stderr
- are used by the python interpreter). As @user2357112 noted in the other answer, this is partly because it's built-in to the python executable, but also because it's necessary for running a substantial amount of python's core functionality (see below how it needs to be loaded for imports to work). random
is part of the standard library, but it doesn't get loaded automatically when you execute, which is the primary relevant difference between it and sys
, for our purposes
Looking at python's documentation on the subject clarifies how python resolves imports:
The first place checked during import search is sys.modules
. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths.
...
During import, the module name is looked up in sys.modules
and if present, the associated value is the module satisfying the import, and the process completes. However, if the value is None
, then a ModuleNotFoundError
is raised. If the module name is missing, Python will continue searching for the module.
As for where it looks for the module, you can see in your observed behavior that it looks in the local directory first. That is, it searches the local directory first and then the "usual places" afterwards.
The reason for the discrepancy between how sys
is handled and how random
is handled is caching - sys
is cached (so python doesn't even check the path to import), whereas random
is not cached (so python does check the path to import it, and imports locally).
There are a few ways you can change this behavior.
First, if you must have a local module called sys
, you can use importlib
to import it in relative or absolute terms, without running into the ambiguity with the sys
that's already cached. I have no idea how this would affect other modules that independently try to import sys
, and you really shouldn't be naming your files the same as standard library modules anyway.
Alternatively, if you want the code to check python's built-in modules before checking the local directory, then you should be able to do that by modifying sys.path
, which shows the order in which paths are searched for input (the same as the $PATH
environment variable, or any other similar language-specific one). The first element of sys.path
is usually going to be an empty string ''
, that would result in searching the current working directory. So you can simply move that to the back of sys.path
, to have it searched last instead of first:
sys.path.append(sys.path.pop(0))
random
, which is in direct conflict with the Python standard libraryrandom
module. Also, neither your ownrandom
module nor the Python standard libraryrandom
module have any attributechoice
, which is the crux of the exception.random
but no problem forsys
.random.py
will make a conflict withrandom
Python module.