简体   繁体   中英

How do I create executable for Cython project using setuptools on Windows OS?

main.py:

print('test')

I can build using this command:

python setup.py build_ext --inplace --compiler=msvc

This will create *.pyd files and put them to needed folders in my package.

But my main.py is compiled as main.pyd.

Extension(
    'main',
    sources=['main.c','main.py'],
    include_dirs=[np.get_include()],
)

Tried this:

cl.exe  /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\P\Python27-32\include /Tcmain.c /link /OUT:"main.exe" /SUBSYSTEM:WINDOWS /MACHINE:X86 /LIBPATH:C:\P\Python27-32\libs

But it fails:

main.c Creating library main.lib and object main.exp MSVCRT.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup main.exe : fatal error LNK1120: 1 unresolved externals

Is there a way to build it as Windows executable main.exe using Cython?

I figured it.

setup.py has to expose entry_point:

entry_points={
          'console_scripts': [
            'mypackage-cli=mypackage.command_line:main',
            ],
      },

command_line.py:

from . import cli
def main():
    print cli()

__init__.py:

from markdown import markdown

def cli():
    return markdown(u'It''s a CLI!')

after python setup.py develop it creates executable file mypackage-cli.exe with 'shim' script mypackage-cli-script.py in %PYTHONPATH%\\Scripts

c:\tmp\mypackage>C:\Python27-32\Scripts\mypackage-cli.exe

<p>It's a CLI!</p>

It requires Python distribution to be at C:\\Python27-32

I posted demo here: github:mycyexepackage

Try use PyInstaller

pip install pyinstaller

From my experience this framework works allways

And download Cython bundling if it necessary : https://github.com/prologic/pyinstaller-cython-bundling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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