0

Execute the following commands:

touch sys.py
touch parser.py
python3 -c 'import sys; print(sys)'
pypy3 -c 'import sys; print(sys)'
python3 -c 'import parser; print(parser)'
pypy3 -c 'import parser; print(parser)'

When CPython or PyPy try to import sys, they cannot and it defaults to the builtin module sys.

But some modules are different. In CPython, if you execute in another folder python3 -c 'import parser; print(parser)' you'll see the path of the module instead of (built-in), and is there is a parser.py file in the $PYTHONPATH (sys.path), it will be imported.

In PyPy3, it seems that parser has the same status as sys.

Is there a reference somewhere about those imports? (I know they are bad practice but I wonder why I have not seen this anywhere yet)

Labo
  • 2,482
  • 2
  • 18
  • 38
  • As far as I know, no. In CPython half the modules have one status and the other half the opposite status, and it depends on configuration-time options. In PyPy we are hard-coding which status to emulate for which module, based on the default CPython configuration. It seems we need to update that list to PyPy3. – Armin Rigo Jan 02 '18 at 14:50
  • Thank you! Do you have a reference for this list ("In CPython half the modules have one status and the other half the opposite status")? – Labo Jan 02 '18 at 16:31
  • No, as I said, it depends on how you configure your CPython. You can check in a particular distribution if the ``parser`` module has the same status as the ``sys`` module or not by checking if it was compiled as a built-in or an extension module (which you can know at runtime by checking if ``parser.__file__`` exists or not). – Armin Rigo Jan 02 '18 at 20:40
  • ...also, my comments are based on Python 2.x, and the import system changed, but from experience I doubt this change came with a simplification. – Armin Rigo Jan 02 '18 at 20:43

0 Answers0