惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Pavilion

windows下安装mingw-w64,c/c++编译器 word 文档,高亮关键字,因 DOM 结构复杂(跨段落、跨表格、跨样式)导致滚动定位偏差问题解决方案 python 命令行参数修改 安装Mysql macd指标 pyCharm 设置 签名,时间 Pycharm 设置 flask 监听端口 pycharm专业版的安装和破解教程 Python 虚拟环境安装flask框架 Read timed out. Python虚拟环境 解决pip无法更新问题的简单方法:WARNING: You are using pip version 20.2.1; however, version 24.2 is available. pyqt5 列表内添加按钮 RequestsDependencyWarning: urllib3 (2.0.0.dev0) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) - Pavilion PyCharm怎样添加Qt designer 十大排序算法 IntelliJ IDEA安装教程 JDK12的安装搭建 提交并发量的方法:Java GC tuning :Garbage collector python 绘制点线
VNPY加密教程(Python生成pyd文件)
Pavilion · 2019-04-29 · via 博客园 - Pavilion

在windows平台上安装python c extension的扩展包是件很痛苦的事情,一般通过安装vc/vs系列来编译C扩展,不过安装包都比较大。或者通过mingw编译,不过有时会在兼容性上出现点问题。

有个好消息就是微软为Python提供了专用的编译器Microsoft Visual C++ Compiler for Python 2.7(包含32位和64位) 下载地址: http://aka.ms/vcpython27

1.下载完成并安装。以本机为例,安装完成后的路径为: 

1

C:\Users\acer\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0

2.修改python安装目录下Lib\distutils\msvc9compiler.py文件(如有必要可能msvccompiler.py文件也需要做相应更改,视系统而定),找到get_build_version方法直接return 9.0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

def get_build_version():

    return 9.0

    prefix = "MSC v."

    = sys.version.find(prefix)

    if == -1:

        return 6

    = + len(prefix)

    s, rest = sys.version[i:].split(" "1)

    majorVersion = int(s[:-2]) - 6

    minorVersion = int(s[2:3]) / 10.0

    if majorVersion == 6:

        minorVersion = 0

    if majorVersion >= 6:

        return majorVersion + minorVersion

    return None

然后再找到find_vcvarsall方法直接返回vcvarsall.bat的路径(以自己机器安装后的路径为准)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

def find_vcvarsall(version):

    return r'C:\Users\acer\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat'

    vsbase = VS_BASE % version

    try:

        productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,

                                   "productdir")

    except KeyError:

        productdir = None

    if productdir is None:

        vsbase = VSEXPRESS_BASE % version

        try:

            productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,

                                       "productdir")

        except KeyError:

            productdir = None

            log.debug("Unable to find productdir in registry")

    if not productdir or not os.path.isdir(productdir):

        toolskey = "VS%0.f0COMNTOOLS" % version

        toolsdir = os.environ.get(toolskey, None)

        if toolsdir and os.path.isdir(toolsdir):

            productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")

            productdir = os.path.abspath(productdir)

            if not os.path.isdir(productdir):

                log.debug("%s is not a valid directory" % productdir)

                return None

        else:

            log.debug("Env var %s is not set or invalid" % toolskey)

    if not productdir:

        log.debug("No productdir found")

        return None

    vcvarsall = os.path.join(productdir, "vcvarsall.bat")

    if os.path.isfile(vcvarsall):

        return vcvarsall

    log.debug("Unable to find vcvarsall.bat")

    return None

方案2:修改注册表

错误描述: 
在从源代码安装Python模块时遇到此错误。可是我明明从官网下载并安装了Microsoft Visual C++ Compiler Package for Python 2.7,且配置了环境变量path。

错误原因: 
报这个错误的原因是Python的distutils模块中的msvc9compiler.py并不从环境变量指定的路径中寻找’vcvarsall.bat’,而是通过注册表来寻找…,然而,不知为什么编译器安装过程没有配置注册表。

解决办法: 
只要手工把注册表配置好,就可以了。 
// 1、打开注册表编辑器 
run regedit 
// 2、配置 
// 2.1、如果你安装的Python是32位的,则,创建如下项: 
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Setup\VC 
// 2.2、如果你安装的Python是64位的,则,创建如下项: 
HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC 
// 3、并在此项下新建字符串值: 
名称:productdir 
数据:vcvarsall.bat所在路径 
注意:路径中不包含最后的反斜杠。

3、创建工作目录并生成pyd文件

这里有一个坑。程序所在的目录路径不能包含中文文字。所以我在E盘下创建一个test文件夹,用于放置要处理的python文件。

简单写了一个测试文件(命名为test.py):

  1. #coding:utf-8
    def hello():
        print("Hello world")
        input("<press ENTER to quit>")
    

在该目录下,再新建一个py文件(命名为setup.py):

  1. from distutils.core import setup
    from Cython.Build import cythonize
     
    setup(
      name = 'Hello world app',
      ext_modules = cythonize("test.py"),
    )