我可以配置 pydev 自动编译 cython 文件吗?

发布于 2024-12-18 10:30:08 字数 295 浏览 3 评论 0原文

我刚刚拿起 Cython。我正在使用它在我的 Python 项目中构建核心库。目前,我已经使用 distutils 配置了一个 setup.py 文件,并且每当我想重新编译 Cython 文件时都会运行以下命令:

python ./setup.py build_ext --inplace

但是,我经常忘记。我喜欢 Eclipse 在每次编辑/保存时自动为 Java 构建类文件。是否可以为 PyDev、Eclipse 或其他一些巧妙的方式配置类似的行为?

I'm just picking up Cython. I'm using it to build a core library in my Python project. Currently I've configured a setup.py file with distutils and am running the following command whenever I want to recompile the Cython file:

python ./setup.py build_ext --inplace

However, I often forget. I like how Eclipse automatically builds class files for Java every time I edit/save. Is it possible to configure similar behavior for PyDev, Eclipse, or some other clever way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-12-25 10:30:09

目前,PyDev 中没有特殊支持在 cython 文件发生更改时自动编译它们...如果您愿意,可以创建一个外部构建器:

右键单击项目 >属性>建设者>新的>程序,然后将程序配置为 python,将要运行的模块作为参数,并接收 ${build_files} 变量作为参数。

您可能应该检查某些更改的文件是否是 .pyx 文件,如果是,则调用该文件的实际构建命令 - 也许还有依赖项。

For now there's no special support in PyDev to automatically compile cython files when they change... if you want you can create an external builder:

Right click project > properties > builders > new > program, then configure the program as python having as a parameter the module to run and receiving as arguments also the ${build_files} variable.

You should probably check if some changed file is a .pyx file and if it is, call the actual build command for that file -- and maybe dependencies.

夜访吸血鬼 2024-12-25 10:30:09

您可以使用“魔法”sitecustomize.py 自动获得 cython 编译在你的基础 PYTHONPATH 中调用 pyximport,即使需要一些安装详细信息(例如,在 Windows 下,您的 mingw 位置),这里是一个示例:

import pyximport
import os
import numpy
#import cython
import Cython.Compiler.Options as Options
Options.cimport_from_pyx = True

if os.name == 'nt':
    if os.environ.has_key('CPATH'):
        os.environ['CPATH'] = os.environ['CPATH'] + numpy.get_include()
    else:
        os.environ['CPATH'] = numpy.get_include()

    # XXX: we're assuming that MinGW is installed in C:\MinGW (default)
    if os.environ.has_key('PATH'):
        os.environ['PATH'] = os.environ['PATH'] + ';C:\MinGW\bin'
    else:
        os.environ['PATH'] = 'C:\MinGW\bin'

    mingw_setup_args = { 'options': { 'build_ext': { 'compiler': 'mingw32' } } }
    pyximport.install(setup_args=mingw_setup_args)
elif os.name == 'posix':
    if os.environ.has_key('CFLAGS'):
        os.environ['CFLAGS'] = os.environ['CFLAGS'] + ' -I' + numpy.get_include()
    else:
        os.environ['CFLAGS'] = ' -I' + numpy.get_include()

    pyximport.install()

pyximport.DEBUG_IMPORT = True

作为旁注,如果您在 Windows 下,请记住您的 cython 必须是 稍微修改以使用 mingw。

您还应该将您的文件命名为 *.pyx 才能使其正常工作。另一个建议:您应该使用 cython "pure python" syntax 以便Pydev 编辑器不会抱怨。

You can obtain automatically cython compiling using a "magic" sitecustomize.py in your base PYTHONPATH that calls pyximport, even if it requires some installation details (such as, under windows, your mingw location), here is an example :

import pyximport
import os
import numpy
#import cython
import Cython.Compiler.Options as Options
Options.cimport_from_pyx = True

if os.name == 'nt':
    if os.environ.has_key('CPATH'):
        os.environ['CPATH'] = os.environ['CPATH'] + numpy.get_include()
    else:
        os.environ['CPATH'] = numpy.get_include()

    # XXX: we're assuming that MinGW is installed in C:\MinGW (default)
    if os.environ.has_key('PATH'):
        os.environ['PATH'] = os.environ['PATH'] + ';C:\MinGW\bin'
    else:
        os.environ['PATH'] = 'C:\MinGW\bin'

    mingw_setup_args = { 'options': { 'build_ext': { 'compiler': 'mingw32' } } }
    pyximport.install(setup_args=mingw_setup_args)
elif os.name == 'posix':
    if os.environ.has_key('CFLAGS'):
        os.environ['CFLAGS'] = os.environ['CFLAGS'] + ' -I' + numpy.get_include()
    else:
        os.environ['CFLAGS'] = ' -I' + numpy.get_include()

    pyximport.install()

pyximport.DEBUG_IMPORT = True

As a side note, if you are under windows, please keep in mind that your cython must be slightly modified to use mingw.

You should also call your files *.pyx for this to work. Another advice : you should use the cython "pure python" syntax so that the Pydev editor will not complain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文