pythonc++python-2.6python-c-api

is it possible to completly flatten a python package, removing directory completly?


I'm wondering because i'm having some massive issues with importing package modules into my embedded python enterpreter, regardless of sys.path .

so for example.

my package.

program.py
    lib|
        Packz|
               - __init__.py
               - a.py
               - b.py

program.py importing functions like

from Packz.a import afunc
from Packz.b import bfunc

Is it possible to flatten this package to completely remove the directory the module resides in and place all the lib files in the same directory?? ( providing that the module names don't collide of course)

program.py
    lib|
        Packz.py
        a.py
        b.py

WHile still maintain the ability to import like this from my main program:

from Packz.a import afunc
from Packz.b import bfunc

could I do something like:

Packz.py>

import a
import b

any thoughts on the subject?

i've got a virtual filesystem that seems to have trouble loading in the module if it's referenced by it's directory name. the main program does "see" the files in all the directories though and i can import regular single file modules. such as io.py timeit.py

i've tried importing my module with the python c api to no avail. i'm on python 2.6 so i cannot use import to import a module with a path. ( only 2.5 and below, seems like it was bug)

Thanks!


Solution

  • I got my code working by doing a search through all the modules in the directory and removing all instances (use 'sed' or sublime text :D ) of :

    Packz.

    ex:

    from Packz.a import afunc
    
    becomes:
    
    from a import afunc
    
    and
    
    
    
    from Packz import a
    becomes:
    
    import a
    

    BUT
    anything that is 
    from Packz import __version__
    
    stays the same
    
    -And renaming my __init__.py file to Packz.py
    

    (this is only the case if your init file has some version info, if it's empty u can delete it)


    now in your code you must reference the module directly rather than the package.

    If you want you can add:

    __all__ = ["afunc", "bfunc"]
    from a import afunc
    from b import bfunc
    

    to your Packz.py file if you need it to import all your modules at once from a single standalone module.

    For those having issues with the import search function in virtual filesystems, this seems like one decent solution although not as dynamic. (appart from hacking the python import function)

    This is the other: (generate convert your module packages to c through cython and then embed the modules into your application binary, then you don't have to worry about pathing issues...+ it makes the code run faster)

    http://mdqinc.com/blog/2011/08/statically-linking-python-with-cython-generated-modules-and-packages/