Extending Python: pre-load my C module
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 requir开发者_运维百科e to import ...
all the time.
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.
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.
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.
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.
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.
精彩评论