0

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!

nobody
  • 19,814
  • 17
  • 56
  • 77
flow
  • 571
  • 1
  • 4
  • 16
  • You can insert entries for `Packz.a` and `Packz.b` into `sys.modules` directly. `import sys; import a; sys.modules['Packz.a'] = a`. It's how the `os` module sets up `os.path`. I'd consider this a last resort, though. – user2357112 May 26 '14 at 21:52
  • I've tried that too!!! this is definitively the last resort :D er second last. the last would be embedding them in C. even if the paths are correct it wont detect a package. it will however detect individual modules. the filesystem is embedded in an ARM binary – flow May 27 '14 at 16:08

3 Answers3

1

Indeed, you can modify your package behaviour by editing __init__.py.

Inside __init__.py, the variable all contains all the modules you call for with import *. In your case, it is something like:

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

See the following subject ..., it might help ;)

Community
  • 1
  • 1
Taha
  • 709
  • 5
  • 10
  • 1
    This isn't what the OP is trying to do. He wants to flatten the directory hierarchy while still importing `afunc` and `bfunc` as `from Packz.a import afunc` and `from Packz.b import bfunc`. Your answer wouldn't flatten the directory hierarchy, but would change the import notation. Also, `__all__` is irrelevant if you're not using `import *`, and it wouldn't be needed here even if the OP did care about `import *`. – user2357112 May 27 '14 at 03:10
  • the issue is that python wont detect a package even if i have an `__init__.py` in the folder – flow May 27 '14 at 16:06
1

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/

flow
  • 571
  • 1
  • 4
  • 16
0

in C++ add

setenv("PYTHONPATH", ".", 0);

flow
  • 571
  • 1
  • 4
  • 16