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
Getting started with cython on mac os
提问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 gcc
compiler where is the pyconfig.h
file on your system using the -I
flag. You can find it using the find
program.
您必须使用标志告诉gcc
编译器pyconfig.h
系统上的文件在哪里-I
。您可以使用该find
程序找到它。
A simpler way to compilethe module is using a setup.py
module. Cython provides a cythonize
function that starts this process for a .pyx
module.
编译模块的一种更简单的方法是使用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.py
are
的内容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.pyx
file are
split_urls.pyx
文件的内容是
import re
def do_split(links):
return re.findall('(https?://\S+)', links)
And it is the main.py
module 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']