Mailing List Archive

Embedding: Defining packages?
Hi,

I'm embedding Python in a rather complex C program. To represent the
complexity (and to keep the thing modular), I want to define several
Python modules and group them in a package hierarchy:

MyProgram
+- Server
| +- Communications
| +- HardwareAccess
+- Client
+- Communications
+- AbstractObjects

...etc. How do I do that? Is there a way to pass hierarchical module names
to Py_InitModule, such as

theModule = Py_InitModule(methodDefs, "MyProgram.Server.Communications");

My attempts so far haven't worked.

Thanks in advance,
--Bjoern

PS I'm posting this via the newsgroup, as sending mail to python-list@cwi.nl
doesn't seem to work. Has anything changed there that I'm not aware of?
--
--------------------------------/\--One OS to rule them all---Windows NT-----
Bjoern Giesler / \ One OS to find them
<un4e@rz.uni-karlsruhe.de> / <> \ One OS to bring them all
-----------------------------/______\--And in the Darkness bind them---------
Thank you for your time, worship the Antichrist, and have a nice day.
Embedding: Defining packages? [ In reply to ]
Bjoern Giesler <un4e@rz.uni-karlsruhe.de> wrote:
: Hi,

: I'm embedding Python in a rather complex C program. To represent the
: complexity (and to keep the thing modular), I want to define several
: Python modules and group them in a package hierarchy:

: MyProgram
: +- Server
: | +- Communications
: | +- HardwareAccess
: +- Client
: +- Communications
: +- AbstractObjects

: ...etc. How do I do that? Is there a way to pass hierarchical module names
: to Py_InitModule, such as

: theModule = Py_InitModule(methodDefs, "MyProgram.Server.Communications");

: My attempts so far haven't worked.

: Thanks in advance,
: --Bjoern

: PS I'm posting this via the newsgroup, as sending mail to python-list@cwi.nl
: doesn't seem to work. Has anything changed there that I'm not aware of?

You probably need to reverse the order of the arguments
(http://www.python.org/doc/current/ext/methodTable.html)

theModule = Py_InitModule("MyProgram.Server.Communications", methodDefs);

(I've tried this format successfully.)

-Arcege
Embedding: Defining packages? [ In reply to ]
Hi,

Michael P. Reilly <arcege@shore.net> wrote:
: Bjoern Giesler <un4e@rz.uni-karlsruhe.de> wrote:
: : theModule = Py_InitModule(methodDefs, "MyProgram.Server.Communications");

: theModule = Py_InitModule("MyProgram.Server.Communications", methodDefs);

...was of course what I meant. Mine was just a typo. Still, this gives me a
module named "MyProgram.Server.Communications", but _no_ module "MyProgram".
What I want is to be able to say
from MyProgram import *
and then address functions via
Server.Communications.foo()

This doesn't work; it seems that Python can't define _packages_ from C, but
only _modules_. Is that correct?

--Bjoern
--
--------------------------------/\--One OS to rule them all---Windows NT-----
Bjoern Giesler / \ One OS to find them
<un4e@rz.uni-karlsruhe.de> / <> \ One OS to bring them all
-----------------------------/______\--And in the Darkness bind them---------
Thank you for your time, worship the Antichrist, and have a nice day.
Embedding: Defining packages? [ In reply to ]
Bjoern Giesler <un4e@rz.uni-karlsruhe.de> wrote:
: Hi,

: Michael P. Reilly <arcege@shore.net> wrote:
: : Bjoern Giesler <un4e@rz.uni-karlsruhe.de> wrote:
: : : theModule = Py_InitModule(methodDefs, "MyProgram.Server.Communications");

: : theModule = Py_InitModule("MyProgram.Server.Communications", methodDefs);

: ...was of course what I meant. Mine was just a typo. Still, this gives me a
: module named "MyProgram.Server.Communications", but _no_ module "MyProgram".
: What I want is to be able to say
: from MyProgram import *
: and then address functions via
: Server.Communications.foo()

: This doesn't work; it seems that Python can't define _packages_ from C, but
: only _modules_. Is that correct?

Yes, I see what you mean now; sorry. Correct, you cannot create
"package modules" that can be imported through "from", but you can
create the naming conventions. Most of the reason is how the importer
works, it is looking for a file with the submodule name, not a module
with that dotted name. You would have to install a replacement for the
__builtin__.__import__ function.

-Arcege