为什么转成 pyd 文件

性能提升、可以更好的保护你写的代码等

环境

  • win10
  • python 3.9
  • Microsoft Visual C++ 14.0

安装Cython

pip install cython

示例

hello.py

def hello():
    print("hello")

world.py

def world():
    print("world")

setup.py

# -*- coding:utf-8 -*-
from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize(["hello.py","world.py"]),
)

执行命令

python setup.py build_ext --inplace

使用

生成 pyd 文件后把其它文件删除,就可以像正常 py 文件一样直接使用

from hello import hello
hello()

遇到到问题:

error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/ Tools":

解决方法:
直接安装Microsoft C++ Build Tools
选:使用C++的桌面开发版本得对应我的是 14.0

分类: GUI开发 标签: 暂无标签

评论

暂无评论数据

暂无评论数据

目录