python import modules inside folder

Viewed 8448

Want to learn the proper way for importing modules with folder

/garden
    __init__.py
    utilities.py
    someTools.py
    /plants
        __init__.py
        carrot.py
        corn.py

Inside /plants/__init__.py I have

__all__ = ['carrot', 'corn']
from . import *

inside carrot.py

def info():
    print "I'm in carrot.py"

When I do

import garden
garden.carrot.info()
# got correct result
I'm in carrot.py

My question is how do I correct namespace for utilities.py for example inside carrot.py and corn.py. I want to use function in utilities.py

Inside carrot.py when I

import utilities as util

# try use it and got global name error
util.someFunc()
# NameError: global name 'util' is not defined # 

Can I configure __init__.py in plants folder so that it import all the modules inside garden? Like utilities and sometools ? so I don't have to import utilities in both carrot.py and corn.py and be able to use utilities ?

1 Answers
Related