Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does the *built-in* python module 'sys' need to be explicitly imported with an 'import' statement?

The python documentation says:

"One particular module deserves some attention: sys, which is built into every Python interpreter."

My understanding is that if a module is built into the Python interpreter itself, then there is no need for an explicit import statement. If the sys module is built-in the Python interpreter, then why is an explicit import statement required for the sys module?

like image 417
nnkparikh Avatar asked Oct 25 '25 08:10

nnkparikh


1 Answers

sys is imported at Python startup. So when you import sys, it does not actually do anything except bind a variable name to the already-existing module.

When creating a module instance, there is no reason to have the sys name bound in the module scope when many (probably most) modules don't need to use sys. So, that name is not in scope by default.

like image 68
wim Avatar answered Oct 28 '25 01:10

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!