简体   繁体   English

argparse在Windows上不适用于Cython

[英]argparse does not work with Cython on Windows

I am writing a Windows app. 我正在编写Windows应用程序。 using Python/Cython and compiling using MingW GNU C compiler and an MSYS2 terminal. 使用Python / Cython并使用MingW GNU C编译器和MSYS2终端进行编译。

The small snippet code below shows the issue: 下面的小片段代码显示了此问题:

import argparse

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Argparse Cython Test')
    parser.add_argument(
        '--run',
        type=str,
        default='',
        dest="runFile",
        required=False,
        help='Execute this file.')

    args = parser.parse_args()
    print ("args -- ", args)

#endif main

I translate the code into C with: 我将代码转换为C:

> cython -3 --embed -o argTest.c argTest.py

(Side note: For some reason the emitted C code generates a main function int wmain () instead of int main() so I do that change using a sed script). (旁注:由于某种原因,发出的C代码生成一个主函数int wmain()而不是int main(),因此我使用sed脚本进行了更改)。 And then compile with MingW GCC: 然后使用MingW GCC进行编译:

> gcc -v -O3 -I/d/Python3/include/ -L /d/Python3/libs/ -o argTest argTest.c -lpython36 -lm -ldl -Wl,--subsystem,windows

I see this output when I run the executable with the argparse options -- seems to be some unicode characters: 当我使用argparse选项运行可执行文件时,我看到此输出-似乎是一些Unicode字符:

> ./argTest.exe -h                                         
usage: \u3a44\u4d5c\u2079\u7453\u6675\u5c66\u6556\u4378\u646d\u7950\u615c\u6772\u6554\u7473\u652e\u6578\u5200\u433d\u5c3a\u4957\ua4f3\u4bf8\u3ff4\u0d00\u682d\u0200▒\u02b1\ua4f3\u4bf8\u3ff0\u0e00.\u02b1▒\u02b1\ua4b4\u0cf8\u3ff0\u0800 [-h] [--run RUNFILE]
\u3a44\u4d5c\u2079\u7453\u6675\u5c66\u6556\u4378\u646d\u7950\u615c\u6772\u6554\u7473\u652e\u6578\u5200\u433d\u5c3a\u4957\ua4f3\u4bf8\u3ff4\u0d00\u682d\u0200▒\u02b1\ua4f3\u4bf8\u3ff0\u0e00.\u02b1▒\u02b1\ua4b4\u0cf8\u3ff0\u0800: error: unrecognized arguments: \u682d\u0200▒\u02b1\ua4f3\u4bf8\u3ff0\u0e00.\u02b1▒\u02b1\ua4b4\u0cf8\u3ff0\u0800

Without the argparse options, the executable runs fine. 没有argparse选项,可执行文件运行良好。 My application (using wxPython) runs fine on Windows without using argparse options. 我的应用程序(使用wxPython)可以在Windows上正常运行,而无需使用argparse选项。

Anyone knows what may be happening here? 有人知道这里可能会发生什么吗?

Thanks 谢谢

argparse is working fine, but your mixing Python 3 UTF-8 string and char* . argparse工作正常,但是您将Python 3 UTF-8 stringchar* Changing wmain for main means you will be handling you inputs as ASCII. 更改main wmain意味着您将以ASCII处理输入。 Check answers here about how to enable wmain in MINGW compilers. 在此处查看有关如何在MINGW编译器中启用wmain答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM