Importing package modules from C-extension

Discussion in 'Python' started by Mark English, Mar 17, 2005.

  1. Mark English

    Mark English Guest

    Basic problem:
    If there is a C-extension module in a package and it tries to import
    another python module in the same package without using the fully
    qualified path, the import fails.

    Python 2.4 on Windows 2000

    For example:
    mypackage contains:

    "mypackage" is a directory with an empty "" file
    "cextension_example.pyd" is a c-extension module
    "" is a python module used by other python modules
    "" is a python module which imports

    In other words "" looks like this:
    ---Start code---
    import to_be_imported
    print to_be_imported
    ---End code---

    and "" looks like this:
    ---Start code---
    print 'Importing:', __file__
    ---End code---

    The c-extension "cextension_example.pyd" contains a line in its
    initialisation routine:
    ---Start code---
    PyObject *pToBeImported = PyImport_Import("to_be_imported");
    ---End code---
    Which returns NULL. I'd rather it imported the module from the local

    If at a python prompt I do this:
    >>> import mypackage.does_an_import

    it works fine as I would expect from reading the documentation.
    It starts by looking in the package where it is currently placed before
    searching the PYTHONPATH.

    If at a python prompt I do this:
    >>> import mypackage.cextension_example

    it fails to import "" which I do not expect.
    It does not look in the package where it is currently placed.

    In the past I have used the solution of a fully qualified path:
    ---Start code---
    PyObject *pToBeImported = PyImport_Import("mypackage.to_be_imported");
    ---End code---

    However this time my application is building library paths at runtime
    and doesn't know exactly what's going to be where. I can guarantee the
    directory structure will be like that above.

    So, how do I get the behaviour of "import", rather than the behaviour of
    what looks more like "__import__" ?

    Guesswork and speculation:
    >From looking at the python source, it seems that in my C extension I

    need access to the module's dictionary (passed as globals and locals
    parameters to import functions), so that the parent package's __name__
    entry can be used later on.

    Leading to more questions:
    If this is the case, how do I get at the current module and its
    dictionary from a c-extension function that takes no parameters ? Do I
    have to stash it away internally in the C-code on initialisation, or is
    there some thread local object I can get it from (either directly, or
    from the current frame) ? Is it good programming practice to use
    "PyThreadState_Get()->frame" ?

    Thanks for any help,

    The information contained in this e-mail is confidential and solely
    for the intended addressee(s). Unauthorised reproduction, disclosure,
    modification, and/or distribution of this email may be unlawful. If you
    have received this email in error, please notify the sender immediately
    and delete it from your system. The views expressed in this message
    do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary companies.
    Mark English, Mar 17, 2005
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobiah
    Sep 14, 2003
  2. Anand
    Tim Daneliuk
    Nov 8, 2003
  3. Dave
  4. plb
  5. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Peter Peyman Puk
    Mar 19, 2010

Share This Page