如何创建 .pyd 文件?
How to create a .pyd file?
我正在创建一个使用 Python OpenCV 的项目。我的图像处理有点慢,所以我想我可以通过创建一个 .pyd
文件(我在某处读到)来加快代码速度。
我可以使用 Cython 创建 .c
文件,但是如何创建 .pyd
?虽然它们是一种 .dll
,但我应该先制作一个 .dll
并转换它吗?而且我认为它们不是平台独立的,Unix 上的等价物是什么?
感谢您的帮助!
您必须在终端中 运行 一个 setup.py
文件。这是一个使用 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)
在终端(cmd in Windows)你必须执行命令
python setup.py build_ext --inplace
重要的是我想您已经安装了编译器(例如 Python 2.7 的 Microsoft Visual C++ 编译器包)。您可以在 https://github.com/cython/cython/wiki/CythonExtensionsOnWindows
中找到更多信息
使用具有相应依赖项的 cythonize:
from setuptools.extension import Extension
extensions.append(Extension("write the file names.pyx"))
from Cython.Build import cythonize
ext_modules = cythonize(extensions)
那么,
setup(ext_modules=ext_modules, **setup_kwargs)
如果您尝试使用带有 Visual Studio 的 cython 将 python 代码转换为 pyd(Python 动态模块)来寻找答案,那么您将得到一个模糊的答案。因为,由于与更高版本的兼容性问题,您期望工作的可视化代码可能无法正常工作。比如msvc的1900、1929.
您需要在 disutils 中编辑 cygwin-compiler 才能完成任务。如果你想使用 MingW 那么你还需要包括在 disutils 中使用的编译器的配置。
一个很简单的方法是我们可以使用Nuitka,将python代码转换成C和Python动态库是非常简单和可靠的。无需配置,无需添加支持。
Let's grab basics
1).安装nuitka,pip install nuitka
2).从 Sourceforge 安装 MingW
3).将 mingW 添加到路径
现在一切都很好。
4).以管理员身份打开cmd,输入python -m nuitka --module file.py
Nuitka 将在当前目录中创建 file.c、file.pyi(用于导入)和 file.cp39_architecture.pyd。从 file.pyd 开始,您可以将模块直接导入 main.py,速度快如闪电。
但是,如果您想创建独立的应用程序,请尝试,
python -m nuitkafile.py
Prerequisites
Cython
MinGW-w64 added to your PATH environment variable
您需要在 .py 文件中添加一个名为 inityourfilename 的函数
然后必须通过
制作一个.c文件
cython --embed yourfile.py
然后您需要使用 gcc 将它编译成一个 .o 文件,将 -I 之后的文本替换为您的 Python 包含文件夹的路径
If your MinGW is 64 bit then this command
gcc -mdll -O -Wall -IC:\Users\yourusername\AppData\Local\Programs\Python\Python39\include -c -DMS_WIN64 yourfile.c
If your MinGW is 32 bit remove the -DMS_WIN64 flag
从 .o 文件制作 .pyd 文件
gcc -shared -LC:\Users\yourusername\AppData\Local\Programs\Python\Python39\libs yourfile.o -lpython39 -o yourfile.pyd
Replace text after -L with path to your libs folder
Replace -lpython39 with your python version ie -lpython38 if you have python 3.8
现在通过将 .pyd 文件导入另一个文件来检查它
我正在创建一个使用 Python OpenCV 的项目。我的图像处理有点慢,所以我想我可以通过创建一个 .pyd
文件(我在某处读到)来加快代码速度。
我可以使用 Cython 创建 .c
文件,但是如何创建 .pyd
?虽然它们是一种 .dll
,但我应该先制作一个 .dll
并转换它吗?而且我认为它们不是平台独立的,Unix 上的等价物是什么?
感谢您的帮助!
您必须在终端中 运行 一个 setup.py
文件。这是一个使用 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)
在终端(cmd in Windows)你必须执行命令
python setup.py build_ext --inplace
重要的是我想您已经安装了编译器(例如 Python 2.7 的 Microsoft Visual C++ 编译器包)。您可以在 https://github.com/cython/cython/wiki/CythonExtensionsOnWindows
中找到更多信息使用具有相应依赖项的 cythonize:
from setuptools.extension import Extension
extensions.append(Extension("write the file names.pyx"))
from Cython.Build import cythonize
ext_modules = cythonize(extensions)
那么,
setup(ext_modules=ext_modules, **setup_kwargs)
如果您尝试使用带有 Visual Studio 的 cython 将 python 代码转换为 pyd(Python 动态模块)来寻找答案,那么您将得到一个模糊的答案。因为,由于与更高版本的兼容性问题,您期望工作的可视化代码可能无法正常工作。比如msvc的1900、1929.
您需要在 disutils 中编辑 cygwin-compiler 才能完成任务。如果你想使用 MingW 那么你还需要包括在 disutils 中使用的编译器的配置。
一个很简单的方法是我们可以使用Nuitka,将python代码转换成C和Python动态库是非常简单和可靠的。无需配置,无需添加支持。
Let's grab basics
1).安装nuitka,pip install nuitka
2).从 Sourceforge 安装 MingW
3).将 mingW 添加到路径
现在一切都很好。 4).以管理员身份打开cmd,输入python -m nuitka --module file.py
Nuitka 将在当前目录中创建 file.c、file.pyi(用于导入)和 file.cp39_architecture.pyd。从 file.pyd 开始,您可以将模块直接导入 main.py,速度快如闪电。
但是,如果您想创建独立的应用程序,请尝试, python -m nuitkafile.py
Prerequisites
Cython
MinGW-w64 added to your PATH environment variable
您需要在 .py 文件中添加一个名为 inityourfilename 的函数 然后必须通过
制作一个.c文件cython --embed yourfile.py
然后您需要使用 gcc 将它编译成一个 .o 文件,将 -I 之后的文本替换为您的 Python 包含文件夹的路径
If your MinGW is 64 bit then this command
gcc -mdll -O -Wall -IC:\Users\yourusername\AppData\Local\Programs\Python\Python39\include -c -DMS_WIN64 yourfile.c
If your MinGW is 32 bit remove the -DMS_WIN64 flag
从 .o 文件制作 .pyd 文件
gcc -shared -LC:\Users\yourusername\AppData\Local\Programs\Python\Python39\libs yourfile.o -lpython39 -o yourfile.pyd
Replace text after -L with path to your libs folder
Replace -lpython39 with your python version ie -lpython38 if you have python 3.8
现在通过将 .pyd 文件导入另一个文件来检查它