Python 什么是 pyximport,我应该如何使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15764232/
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
What is pyximport and how should I use it?
提问by guilhermecgs
I am using cython to generate faster code for a mathematical model. I am having a hard time compiling the code, but somehow I managed to do so using a .bat:
我正在使用 cython 为数学模型生成更快的代码。我很难编译代码,但不知何故我设法使用 .bat 来做到这一点:
setlocal EnableDelayedExpansion
CALL "C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.cmd" /x64 /release
set DISTUTILS_USE_SDK=1
C:\Python27\python.exe C:\gcsilve\trunk\myproject\myproject\cythonsetup.py build_ext --inplace
PAUSE
It runs ok...
运行正常...
My question is regarding pyximport. I have old code written by someone else that uses pyximport.install(). I couldn't figure out what it does and why I should use it, since I am already compiling the code by myself. So, can someone explain to me in a very simple (for dummies) way what pyximport does?
我的问题是关于 pyximport。我有使用 pyximport.install() 的其他人编写的旧代码。我无法弄清楚它的作用以及为什么要使用它,因为我已经自己编译了代码。那么,有人可以用一种非常简单的(对于傻瓜)方式向我解释 pyximport 的作用吗?
Additional information: I have a project1, using cython. I have a project2, that references project1.
附加信息:我有一个 project1,使用 cython。我有一个 project2,它引用了 project1。
采纳答案by Adam Barthelson
pyximport is part of Cython, and it's used in place of importin a way.
pyximport 是 Cython 的一部分,它import在某种程度上用于代替。
If your module doesn't require any extra C libraries or a special build setup, then you can use the pyximport module to load .pyx files directly on import, without having to write a setup.py file. It can be used like this:
如果您的模块不需要任何额外的 C 库或特殊的构建设置,那么您可以使用 pyximport 模块在导入时直接加载 .pyx 文件,而无需编写 setup.py 文件。它可以像这样使用:
>>> import pyximport; pyximport.install()
>>> import helloworld
Hello World
Straight from the Cython documentation
直接来自Cython 文档
回答by Roland Puntaier
You can use pyximportto automatically recompile and reload your .pyxmodule.
您可以使用pyximport自动重新编译和重新加载您的.pyx模块。
Remove what setup.pyhas generated, else you might load that extension module.
pyximportdoes not use setup.py.
删除setup.py已生成的内容,否则您可能会加载该扩展模块。
pyximport不使用setup.py.
Let's assume a hello.pyxwith a function mean2.
让我们假设 ahello.pyx带有一个函数mean2。
Start IPython.
启动 IPython。
In [1]: import pyximport
In [2]: pyximport.install(reload_support=True)
In [3]: from importlib import reload
In [4]: import hello
In [5]: hello.mean2(2,3)
In [1]: import pyximport
In [2]: pyximport.install(reload_support=True)
In [3]: from importlib import reload
In [4]: import hello
In [5]: hello.mean2(2,3)
The result:
结果:
Out [5]: 2.5
Out [5]: 2.5
Leave that window and go to your editor to change hello.pyx.
离开该窗口并转到您的编辑器进行更改hello.pyx。
Then go back to IPython and type
然后回到IPython并输入
In [6]: reload(hello);import hello;hello.mean2(2,3)
In [6]: reload(hello);import hello;hello.mean2(2,3)
You will see some text informing about the recompilation. Then the new result:
您将看到一些有关重新编译的文本。那么新的结果:
Out[6]: 'Mean of 2 and 3 is 2.5'
Out[6]: 'Mean of 2 and 3 is 2.5'

