Skip to content Skip to sidebar Skip to footer

Creating Python Package With Multiple Level Of Folders

I would like to create a package of my python code. My folder structure is like below: sing*(this is a folder)* --ml*(this is a folder)* ----regression*(this is a folder)* ------li

Solution 1:

sing
    __init__.py
    -ml
        __init__.py
        -regression
           __init__.py
           linear.py
        -classification
           __init__.py
           logistic.py

And if the working directory of application is not parent folder of sing then you need to register folder 'sing' into PYTHONPATH environment variable.

For importing linear from sing folder you can use relative path:

from ml.regressionimport linear

and for calling function of linear file you can use:

linear.<*functionname*>(...)

Solution 2:

You need to add a __init__.py files in every folder, so that python interpret the other *.py files (and folder) as packages. A empty file is enough.

Post a Comment for "Creating Python Package With Multiple Level Of Folders"