I am compiling a python program using cython as follows:
cython --embed myprg.py && gcc -Os -I /usr/include/python3.5m -o myprg myprg.c -lpython3.5m -lpthread -lm -lutil -ldl
If I started myprg, enter the following error message:
Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 890, in _find_spec AttributeError: 'ModuleImporterFromVariables' object has no attribute 'find_spec' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "myprg.py", line 9, in init myprg (myprg.c:2219) import json File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 892, in _find_spec File "<frozen importlib._bootstrap>", line 873, in _find_spec_legacy File "/usr/local/lib/python3.5/dist-packages/sh.py", line 3256, in find_module while in_importlib(parent_frame): File "/usr/local/lib/python3.5/dist-packages/sh.py", line 3204, in in_importlib return frame.f_code.co_filename == "<frozen importlib._bootstrap>" AttributeError: 'NoneType' object has no attribute 'f_code'
My python program:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sh import os import traceback import shutil import sys import json print(sys.argv[1:])
It seems that the compiled program is not able to load the json module, while other modules work well. Some tests have shown that this is similar to the argparse module.
My question is: why is this the case and how can I get around / correcting this?
-
Additional notes:
As I understand it, after compiling with cython, the binary will use some kind of "sh.py" method - part of the "sh" package. In this package, for some reason, the method checks the import using the following code:
""" helper for checking if a filename is in importlib guts """ return frame.f_code.co_filename == "<frozen importlib._bootstrap>"
Since there does not seem to be a stack frame that runs a program based on cython output, this method fails with an exception.
As far as I know, this error did not occur some time ago. This is new. It seems that something has changed in python in recent months, which breaks the Cython way of building binary code. Does anyone have more info on this?