在 Mac OS 上开始使用 cython
Getting started with cython on mac os
我用python写了一个简单的程序:
// main.py
import re
links = re.findall('(https?://S+)', 'http://google.pl http://youtube.com')
print(links)
然后我执行这个:
cython main.py
它生成了一个文件:main.c然后我试了这个:
gcc main.c
我有一个错误:
main.c:8:10: fatal error: 'pyconfig.h' file not found
#include "pyconfig.h"
^
1 error generated.
如何将python编译为c?如何在Mac上使用xcode开始使用cython?
您必须
使用 -I
标志告诉gcc
编译器系统上的pyconfig.h
文件在哪里。您可以使用find
程序找到它。
编译模块的一种更简单方法是使用 setup.py
模块。Cython 提供了一个cythonize
函数,用于启动.pyx
模块的此过程。
你缺少的另一点是,Cython文件通常定义从主Python模块使用的辅助函数。
假设您有以下目录和文件的设置:
cython-start/
├── main.py
├── setup.py
└── split_urls.pyx
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
)
split_urls.pyx
文件的内容是
import re
def do_split(links):
return re.findall('(https?://S+)', links)
它是使用定义的 Cython 函数的main.py
模块:
import split_urls
URLS = 'http://google.pl http://youtube.com'
print split_urls.do_split(URLS)
编译 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 ...
并检查您的主模块是否正在执行它应该做的事情:
$ python main.py
['http://google.pl', 'http://youtube.com']
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 如何从头开始创建 avi 文件?
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 自一周开始以来的秒数?
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- GStreamer在开始任何播放之前进行搜索
- 使 std::fstream 写入文件末尾,但从头开始读取
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 哪个C++规范开始支持 std::vector
- 将 PCL 链接到 Cython C++ 模块
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 如何在调试中通过传递引用开始
- 将数组/向量发送到 c++ 脚本的 cython 示例
- 如何使用 picojson.h 在 c++ 中从头开始创建 json 对象
- 从特定键开始遍历地图
- 列表的有效性在插入后开始迭代器
- Cython:无法分配给 2D 矢量
- 在 Mac OS 上开始使用 cython