扩展 Python:预加载我的 C 模块

发布于 2024-08-16 06:26:23 字数 613 浏览 5 评论 0原文

我正在尝试通过我编写的一些 C 函数来扩展 Python 解释器。通过阅读文档,为了公开这些功能,用户必须导入包含这些功能的模块。

是否可以通过 C API 加载预加载或预导入模块,以便用户不必键入 import?或者更好的是,来自导入<函数>

编辑:我可以在 Py_Initialize() 之后执行 PyRun_SimpleString("from mymodule import myfunction"); - 我只是想知道是否还有另一种方法可以做到这一点..?

编辑 2: 换句话说,我有一个用 C 编写的应用程序,其中嵌入了一个 Python 解释器。该应用程序提供了一些我想向用户公开的功能,以便他们可以为该应用程序编写简单的 Python 脚本。我想要的只是消除编写 from mymodule import myfunction1, myfunction2 的需要,因为,由于它是非常专业的应用程序,并且脚本在没有应用程序的情况下无法工作,因此要求始终导入...

I'm trying to extend Python interpreter by a few C functions I wrote. From reading docs, to expose those function the user has to import the module encompassing the functions.

Is it possible to load pre-load or pre-import via C API the module so that the user doesn't have to type import <mymodule>? Or even better, from <mymodule> import <function>?

Edit: I can do PyRun_SimpleString("from mymodule import myfunction") just after Py_Initialize(); - I was just wondering if there is another way of doing this..?

Edit 2: In other words, I have an application written in C which embeds a Python interpreter. That application provides some functionality which I want to expose to the users so they can write simple Python scripts for the app. All I want is to remove the need of writing from mymodule import myfunction1, myfunction2 because, since it is very specialized app and the script wont work without the app anyway, it doesn't make sense to require to import ... all the time.

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

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

发布评论

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

评论(5

悸初 2024-08-23 06:26:23

即使您用 Python 实现了一个模块,用户也必须导入它。这就是 Python 的工作方式,这实际上是一件好事 - 这是 Python 的一大优点 - 命名空间/模块系统强大、易于使用且易于理解。

仅对于学术练习,您当然可以通过创建自定义解释器将新功能添加到 Python 本身。您甚至可以通过这种方式创建新的关键字。但出于任何实际目的,不建议这样做。

Even if you implement a module in Python, the user would have to import it. This is the way Python works, and it's actually a good thing - it's one of the great pluses of Python - the namespace/module system is robust, easy to use and simple to understand.

For academic exercises only, you could of course add your new functionality to Python itself, by creating a custom interpreter. You could even create new keywords this way. But for any practical purpose, this isn't recommended.

遗失的美好 2024-08-23 06:26:23

一般来说,没有。但是,如果您的用户仅在交互式会话中使用您的模块,并且您可以设置他们的环境变量,则可以设置 PYTHONSTARTUP 环境变量到交互式会话启动时运行的脚本。

我能想到的唯一情况是,如果您和您的团队正在使用 python 进行数据分析,针对您的特定问题有自定义命令,并且大多数用户更多是科学家/统计学家而不是程序员。但即使在这种情况下,我建议使用 IPython,创建一个 IPython 配置文件,然后创建别名/脚本供用户运行。它更简洁,不同的名称警告他们没有使用默认的 Python 环境。

In general, no. But if your users are only going to be using your module in interactive sessions, and you can set their environmental variables, you can set the PYTHONSTARTUP environmental variable to a script to run when an interactive session is started.

The only case I can think of, is if you and your group are using python for data analysis, have custom commands for your particular problem, and most users are more scientists/statisticians than programmers. But even in that case, I'd suggest using IPython, creating an IPython profile, and then creating an alias/script for the users to run. It's cleaner, and the different name warns them that they aren't using the default Python environment.

情话已封尘 2024-08-23 06:26:23

如果您确实想这样做,请设置您的 PYTHONSTARTUP 环境变量指向一个文件。在 shkshbash 等中,您可以执行以下操作:

PYTHONSTARTUP=$HOME/.pystartup
export PYTHONSTARTUP

然后,您可以将语句和代码放入您的 $PYTHONSTARTUP< /code> 文件:

echo "import blah" >>$PYTHONSTARTUP

应该可以了。它仅适用于交互式会话。

If you really want to do this, set your PYTHONSTARTUP environment variable to point to a file. In sh, ksh, bash etc., you can do:

PYTHONSTARTUP=$HOME/.pystartup
export PYTHONSTARTUP

Then, you can put statements and code in your $PYTHONSTARTUP file:

echo "import blah" >>$PYTHONSTARTUP

That should do it. It will work only for interactive sessions.

罗罗贝儿 2024-08-23 06:26:23

没有。您可以将其添加到 Python 解释器本身,但这意味着创建一个自定义 Python 版本,我想这不是您想要的。

import 不仅用于加载模块,还用于使该模块在(main|current)命名空间中可见。能够做到这一点,而不需要破解实际的 Python 解释器,将非常强烈地反对“显式优于隐式”。

Nope. You could add it to the Python interpreter itself, but that would mean creating a custom Python version, which, I guess, is not what you want.

That import <mymodule> is not just for loading the module, it's also for making this module visible in the (main|current) namespace. Being able to do that, w/o hacking the actual Python interpreter, would run against "Explicit is better than implicit" very strongly.

临走之时 2024-08-23 06:26:23

查看这篇关于如何在嵌入式 python 中预导入模块的博客文章。

在 C 中嵌入 Python 并导入模块/C++

例如,在 Python 中模拟以下语句:

from hashlib import md5, sha1

我们在 C 中执行:

PyObject * subModules = PyList_New(0);
PyList_Append(subModules, PyString_FromString("md5"));
PyList_Append(subModules, PyString_FromString("sha1"));
PyObject * hashlibImports = PyImport_ImportModuleEx("hashlib", NULL, NULL, subModules);
PyObject * md5Module = PyObject_GetAttr(hashlibImports, PyString_FromString("md5"));
PyObject * sha1Module = PyObject_GetAttr(hashlibImports, PyString_FromString("sha1"));
PyObject * mainModule = PyImport_AddModule("__main__");
PyModule_AddObject(mainModule, "md5", md5Module);
PyModule_AddObject(mainModule, "sha1", sha1Module);

此外,这里还有 文章中提到的项目中的相关代码,其基本执行:

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

在Python初始化之后。

Check out this blog post on how to pre-import modules in embedded python.

Embed Python and Import Modules in C/C++

For example to simulate the following statement in Python:

from hashlib import md5, sha1

We do in C:

PyObject * subModules = PyList_New(0);
PyList_Append(subModules, PyString_FromString("md5"));
PyList_Append(subModules, PyString_FromString("sha1"));
PyObject * hashlibImports = PyImport_ImportModuleEx("hashlib", NULL, NULL, subModules);
PyObject * md5Module = PyObject_GetAttr(hashlibImports, PyString_FromString("md5"));
PyObject * sha1Module = PyObject_GetAttr(hashlibImports, PyString_FromString("sha1"));
PyObject * mainModule = PyImport_AddModule("__main__");
PyModule_AddObject(mainModule, "md5", md5Module);
PyModule_AddObject(mainModule, "sha1", sha1Module);

Also here is a link to relevant code from the project mentioned in the article which basically performs:

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

after Python initialization.

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