问题描述
我正在创建一个使用Python OpenCV的项目。我的图像处理有点慢,所以我想我可以通过创建一个 .pyd
文件(我在某处读到)更快的代码。
I'm creating a project that uses Python OpenCV. My image processing is a bit slow, so I thought I can made the code faster by creating a .pyd
file (I read that somewhere).
我可以使用Cython创建一个 .c
文件,但是如何使 .pyd
?虽然他们是一种 .dll
,我应该先做一个 .dll
并转换吗?
I am able to create a .c
file using Cython, but how to make a .pyd
? While they are a kind of .dll
, should I make a .dll
first and convert it? And I think they're not platform-independent, what are equivalents on Unix?
感谢任何帮助!
推荐答案
您必须在终端中运行 setup.py
文件。这个例子使用 numpy
You have to run a setup.py
file in a terminal. This one is an example that uses numpy
try:
from setuptools import setup
from setuptools import Extension
except ImportError:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np
ext_modules = [Extension("my_code_cython",["my_code_cython.pyx"]),
Extension("another_code_cython",["another_code_cython.pyx"])]
setup(
name= 'Generic model class',
cmdclass = {'build_ext': build_ext},
include_dirs = [np.get_include()],
ext_modules = ext_modules)
Windows)你必须执行命令
In the terminal (cmd in Windows) you have to execute the command
python setup.py build_ext --inplace
重要的是,我想你已经安装了编译器(例如微软的Visual C ++编译器包为Python 2.7)。您可以在 https://github.com/cython/cython/wiki/CythonExtensionsOnWindows中找到更多信息a>
It is important that I suppose you have installed the compiler (Microsoft Visual C++ Compiler Package for Python 2.7 for example). You can find more information in https://github.com/cython/cython/wiki/CythonExtensionsOnWindows
这篇关于如何创建.pyd文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!