Search code examples
pythonpython-importpython-module

Directory setup for python modules


I'm looking to create a python module with the following imports. However, I'm not sure how to go about structuring the files / directories to get the desired effect?

import one
from one import two
from one.two import three
from one.two.three import four

Obviously these imports wont exist in the same file like they are shown above, but I'm looking to have the flexibility to import in this way.


Solution

  • main/
        one/
            __init__.py
            two/
                __init__.py
                three.py
    

    inside three.py create a function four, something like:

    def four():
        print ('this is four')
    

    Now add path of main directory to the PYTHON_PATH

    Note: you need empty __init__.py files in every folder, so that python track those folders.