Skip to content

Failure to start Kernel due to built in modules being overridden by user code

Don Jayamanne edited this page Mar 10, 2022 · 5 revisions

Python kernels can fail to start if files such as os.py or random.py are created in the workspace folder.

Python too has built in modules such as os and random, thus when such files are placed in a directory that Python looks for modules, these get loaded instead of the built in modules. As a result the Python run time behaviour is unexpected and in most cases kernels would fail to start successfully.

Note, there are situations when users would want to overwrite such built in modules, however defining such a use case is beyond the scope of this WIKI. When creating such files, please be mindful of how the python runtime loads modules and how it can impact running of kernels.

To get a list of some of the built-in modules or modules in your Python environment use one of the following approachs:

print(dir(__builtins__))

# install [stdlib_list](https://pypi.org/project/stdlib-list/) from [here](https://pypi.org/project/stdlib-list/)
# Here 3.9 is the version of your Python environment.
print(stdlib_list('3.9')) 

Recommended solution

Please consider renaming the module so that it doesn't conflict with built in modules, e.g. rename random.py to random_.py or my_random.py or other.

For more information about Python modules, please see Modules and Importing Modules

Clone this wiki locally