pyx 编译成 pyd

1、编写好py文件,然后将后缀改为pyx
2、编写好setup.py文件
3、在setup.py 文件同目录下打开命令行执行:

python setup.py build_ext --inplace

之后就会生成pyd文件 和C文件

示例:
需要编译成pyd的文件 fast_fib.pyx

import functools

def fib1(n):
    if n in [1, 2]:
        return 1
    return fib1(n - 1) + fib1(n - 2)

def fib2():
    a=0
    b=1
    def calc():
        nonlocal a,b
        a,b = b,a+b
        return a
    return calc

@functools.lru_cache()# 在fibonacci函数上加上装饰器函数functools.lru_cache()
def fib3(n):
    if n in [1, 2]:
        return 1
    return fib3(n - 1) + fib3(n - 2)

编写setup.py

from setuptools import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize('fast_fib.pyx'))

打开命令行执行命令:

python setup.py build_ext --inplace
Microsoft Windows [版本 10.0.17763.2928]
(c) 2018 Microsoft Corporation。保留所有权利。

H:\Python27\pythonUpSpeedExc>python setup.py build_ext --inplace
running build_ext
building 'fast_fib' extension
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IH:\anaconda3\include -IH:\anaconda3\include "-ID:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-ID:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-ID:\Windows Kits\10\include\10.0.17763.0\ucrt" "-ID:\Windows Kits\10\include\10.0.17763.0\shared" "-ID:\Windows Kits\10\include\10.0.17763.0\um" "-ID:\Windows Kits\10\include\10.0.17763.0\winrt" "-ID:\Windows Kits\10\include\10.0.17763.0\cppwinrt" -Ic:\sybase\OCS-15_0\include /Tcfast_fib.c /Fobuild\temp.win-amd64-3.7\Release\fast_fib.obj
fast_fib.c
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:H:\anaconda3\libs /LIBPATH:H:\anaconda3\PCbuild\amd64 "/LIBPATH:D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\lib\x64" "/LIBPATH:D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x64" "/LIBPATH:D:\Windows Kits\10\lib\10.0.17763.0\ucrt\x64" "/LIBPATH:D:\Windows Kits\10\lib\10.0.17763.0\um\x64" /LIBPATH:c:\sybase\DataAccess\ADONET\dll /LIBPATH:C:\sybase\OCS-15_0\lib /EXPORT:PyInit_fast_fib build\temp.win-amd64-3.7\Release\fast_fib.obj /OUT:build\lib.win-amd64-3.7\fast_fib.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\fast_fib.cp37-win_amd64.lib
  正在创建库 build\temp.win-amd64-3.7\Release\fast_fib.cp37-win_amd64.lib 和对象 build\temp.win-amd64-3.7\Release\fast_fib.cp37-win_amd64.exp
正在生成代码
已完成代码的生成
copying build\lib.win-amd64-3.7\fast_fib.cp37-win_amd64.pyd ->

fast_fib.cp37-win_amd64.pyd就是fast_fib.pyx编译成的pyd文件
测试一下文件是否能用
打开命令行:

Microsoft Windows [版本 10.0.17763.2928]
(c) 2018 Microsoft Corporation。保留所有权利。

H:\Python27\pythonUpSpeedExc>python
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>> from fast_fib import fib3
>>> fib3(40)
102334155
>>>

编译后的pyd文件可用

下面是我在编译过程中遇到问题和解决方法:
1、error: Microsoft Visual C++ 14.0 or greater is required
解决办法:
需要Microsoft C++生成工具14.0以上
这个很好解决,打开这个链接,下载生成工具,然后把需要的装上即可
pyx 编译成 pyd_第1张图片
pyx 编译成 pyd_第2张图片
我这里有个问题,不知道是不是我已经装了visual studio2017的关系,我发现,安装好的工具不起作用,于是我就在visual studio2017点那里点修改,选了visual studio里的这两项来安装,然后就发现可以了

2、fatal error C1083: 无法打开包括文件: “io.h”: No such file or directory
解决办法:
再打开刚刚那个安装工具,选择对应版本的sdk安装就好了
我这里是win10 ,所以我选择win10 sdk
pyx 编译成 pyd_第3张图片

你可能感兴趣的:(python,python)