我可以使用什么方法代替python中的__file__? [英] What method can I use instead of __file__ in python?

查看:178
本文介绍了我可以使用什么方法代替python中的__file__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过cython将python代码转换为c,然后编译.c文件并在我的项目中使用.so。
我的问题:
我在python代码中使用了 __ file __ ,在通过gcc进行编译时,它没有出现错误。但是当我运行程序并在其他python文件中导入.so时,在 __ file __ 行中出现错误。

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.

如何能解决这个问题吗?是否有任何方法可以替换为 __ file __

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

推荐答案

尝试添加

import inspect
import sys
if not hasattr(sys.modules[__name__], '__file__'):
    __file__ = inspect.getfile(inspect.currentframe())

这篇关于我可以使用什么方法代替python中的__file__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆