Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Python package with PyPy2 that only consist out of pyc files?

Using CPython2 I can compile my Python source code package with python.exe -c "import mypackage". After deleting all the *.py files recursively I am able to simply import it with import mypackage and use it as usual.

Using CPython3 I can compile my Python source code pyckage with python.exe -m compileall -b "full/path/to/mypackage". After deleting all the *.py files recursively I am able to simply import it with import mypackage. and use it as usual.

This can even be done using PyPy3 in exactly the same way.

Surprisingly when using PyPy2 this does not work!

After compiling and deleting the source files I get the following output:

Python 2.7.13 (9112c8071614, Feb 06 2019, 23:10:08)
[PyPy 7.0.0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import mypackage
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mypackage
>>>>

Is there a workaround for this problem?

How can I tell PyPy2 to just look at the *.pyc files like CPython2 does?

like image 583
Gahlen Feld Avatar asked Mar 05 '23 05:03

Gahlen Feld


1 Answers

As you have found, PyPy2 refuses to load lone .pyc files, i.e. .pyc files that are still there after you deleted the .py file. PyPy3 instead behaves like CPython.

The current status of PyPy2 reflects the annoyance of the PyPy developers with this detail of CPython. During the development of PyPy, it bit us too often to ignore. In our point of view, when you are normally developing anything, after you remove or rename a .py file, you want to see crashes if you forgot to fix or get rid of the import statements in other files. Instead of that, you see imports still working, and all tests for these unrelated files still passing because, well, they are still using the same old logic. So you think you're done and check in the files in the version control system---but of course it's wrong.

For this reason we decided early on that this behavior of CPython was more like a bug for us, and didn't reproduce it by default in PyPy2. If you really need this behavior, you need to retranslate PyPy2 by passing the --lonepycfile flag.

PyPy3 came later and it comes with its own importlib system, in pure Python, which we didn't touch.

like image 77
Armin Rigo Avatar answered Apr 27 '23 15:04

Armin Rigo