未定义python''uuu file''

2024-10-01 17:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

情况是:

python嵌入ios应用程序(libpython2.7.a),所有逻辑都是python编写的,有些api支持通过swigwrap调用,如果python是py(c,o)一切都可以,但是太慢了,我想比我用cython编译到.c源代码要快一些。所以,它看起来很加载

can't find "__ file __" define

下面是调用堆栈:

[CGPLoad.py] load from file error [Error Message:

exceptions.NameError:name '__ file __' is not defined

Traceback:

init CGPLoad (build/CGPLoad.c:3054): CGPLoad.py(# 19)

init LogInOutShell (build/LogInOutShell.c:3089): LogInOutShell.py(# 20)

init CommonToolShell (build/CommonToolShell.c:6560): CommonToolShell.py(# 19)

init GameWorld (build/GameWorld.c:2516): GameWorld.py(# 19)

init IPY_GAMEWORLD (build/IPY_GAMEWORLD.c:27700): IPY_GAMEWORLD.py(# 28)

IPY_GAMEWORLD.swig_import_helper (build/IPY_GAMEWORLD.c:4304): IPY_GAMEWORLD.py(# 18)

]

python源代码是:

^{pr2}$

出了什么问题,怎么解决?


Tags: pybuild应用程序源代码init情况逻辑file
2条回答

这是预期的行为,因为__file__没有为已编译的可执行文件定义。你应该向

getattr(sys, 'frozen', False)

并相应地采取行动,例如:

^{2}$

如果您只需要在Cython模块中获取包根路径,但是由于Python错误http://bugs.python.org/issue13429而没有定义{},那么可以通过引用__init__.py来使用一个简单的技巧:

def get_package_root():
    from . import __file__ as initpy_file_path
    return os.path.dirname(init‌​py_file_path)

相关问题 更多 >

    热门问题