flow
flow

Reputation: 611

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!

Upvotes: 1

Views: 914

Answers (4)

Ideogram
Ideogram

Reputation: 1375

There is also the 'sticky tape' module:

https://pypi.org/project/stickytape/

that…:

[…] can be used to convert a Python script and any Python modules it depends on into a single-file Python script.

(This module was released in 2021, hence it was not available at the time the question was posted.)

Upvotes: 0

flow
flow

Reputation: 611

in C++ add

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

Upvotes: 0

flow
flow

Reputation: 611

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/

Upvotes: 2

Taha
Taha

Reputation: 778

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 ;)

Upvotes: 1

Related Questions