xcode 在 mac os 上开始使用 cython

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21752928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:39:17  来源:igfitidea点击:

Getting started with cython on mac os

pythonc++xcodemacoscython

提问by mitch

I wrote a simple program in python:

我用python写了一个简单的程序:

// main.py
import re
links = re.findall('(https?://\S+)', 'http://google.pl http://youtube.com')
print(links)

Then I execute this:

然后我执行这个:

cython main.py

It was generated a file: main.c And then I tried this:

它生成了一个文件:main.c 然后我尝试了这个:

gcc main.c

And I have an error:

我有一个错误:

main.c:8:10: fatal error: 'pyconfig.h' file not found
#include "pyconfig.h"
         ^
1 error generated.

How to compile python to c ? How to get started with cython with xcode on mac ?

如何将python编译为c?如何在 mac 上开始使用带有 xcode 的 cython?

回答by logc

You have to tell the gcccompiler where is the pyconfig.hfile on your system using the -Iflag. You can find it using the findprogram.

您必须使用标志告诉gcc编译器pyconfig.h系统上的文件在哪里-I。您可以使用该find程序找到它。

A simpler way to compilethe module is using a setup.pymodule. Cython provides a cythonizefunction that starts this process for a .pyxmodule.

编译模块的一种更简单的方法是使用setup.py模块。Cython 提供了一个cythonize函数来启动一个.pyx模块的这个过程。

Another point you are missing is that Cython files usually define helper functions to be used from a main Python module.

您缺少的另一点是Cython 文件通常定义要从主 Python 模块使用的辅助函数

Suppose you have the following setup as for dirs and files:

假设您对目录和文件进行了以下设置:

cython-start/
├── main.py
├── setup.py
└── split_urls.pyx

The contents of the setup.pyare

的内容setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(name="My first Cython app",
      ext_modules=cythonize('split_urls.pyx'),  # accepts a glob pattern
      )

The contents of the split_urls.pyxfile are

split_urls.pyx文件的内容是

import re

def do_split(links):
    return re.findall('(https?://\S+)', links)

And it is the main.pymodule which uses the defined Cython function:

它是使用定义的 Cython 函数main.py模块:

import split_urls

URLS = 'http://google.pl http://youtube.com'
print split_urls.do_split(URLS)

Compile the Cython module by issuing:

通过发出以下命令编译 Cython 模块:

$ python setup.py build_ext --inplace
Cythonizing split_urls.pyx
running build_ext
building 'split_urls' extension
creating build
creating build/temp.macosx-10.9-x86_64-2.7
... compiler output ...

And check that your main module is doing what it is supposed to do:

并检查您的主模块是否正在做它应该做的事情:

$ python main.py
['http://google.pl', 'http://youtube.com']