17

I convert my python code to c by cython and after that compile .c file and use .so in my project. My problem: I use __file__ in my python code and in while compile by gcc, it doesn't get error. but when I run program and import .so in other python files, appear an error from __file__ line.

How can solve this problem? Is there any method to replace with __file__?

4

2 回答 2

12

尝试在文件开头添加:

import inspect
import sys
if not hasattr(sys.modules[__name__], '__file__'):
    __file__ = inspect.getfile(inspect.currentframe())
于 2013-10-07T12:48:40.300 回答
0

不太确定如何使它与 python 兼容,但 gcc#define__FILE__代码所在文件的名称。

于 2013-10-07T13:04:38.800 回答