tags:

views:

72

answers:

4

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.

A: 

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.

jae
Current namespace is probably a dict, right? So it could be somehow possible to find that dict and AddItem() my function to it...?
EcirH
@EricH - even if it's possible in some hackish way, DON'T DO IT. It's not idiomatic in Python, not what users would expect and generally a bad practice.
Eli Bendersky
+2  A: 

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.

Eli Bendersky
A: 

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.

AFoglia
A: 

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.

Alok