Python submodule imports using __init__.py -



Python submodule imports using __init__.py -

i'm learning python, , can't figure out how imports in __init__.py work.

i understand the python tutorial __init__.py file initializes package, , can import subpackages here.

i'm doing wrong, though. explain me (and future python-learners) i'm doing wrong?

here's simplified illustration of i'm trying do.

this file structure:

package __init__.py test.py subpackage __init__.py hello_world.py

the contents of hello_world.py:

def do_something(): print "hello, world!"

subpackage/__init__.py empty.

package/__init__.py contains:

import test.submodule.do_something

and finally, test.py contains:

do_something()

this how effort run hello_world.py using osx terminal , python 3:

python test.py

python throws next error:

nameerror: name 'do_something' not defined

you understand when import module, interpreter creates new namespace , executes code of module new namespace both local , global namespace. when code completes execution, module name (or name given in as clause) bound module object created within importing namespace.

the __init__.py in bundle serves much same function. package, having structure, written directory can contain modules (regular .py files) , subdirectories (which must contain __init__.py file) sub_packages. when bundle imported new namespace created , package's init.py executed namespace local , global namespaces. reply problem can strip filestore downwards omitting top-level package, never considered interpreter when test.py run program. this:

test.py subpackage/ __init__.py hello_world.py

now, subpackage no longer sub-package, have removed containing bundle irrelevant. focusing on why do_something name undefined might help. test.py not contain import, , it's unclear how expecting do_something acquire meaning. create work using empty subpackage/__init__.py , test.py read

from subpackage.hello_world import do_something do_something()

alternatively subpackage/__init__.py reads

from hello_world import do_something

which establishes do_something function within subpackage namespace when bundle imported. utilize test.py imports function package, this:

from subpackage import do_something do_something()

a final alternative same __init__.py utilize test.py imports (sub)package , utilize relative naming access required function:

import subpackage subpackage.do_something()

to gain access in local namespace

with empty __init__.py achieved test.py reading

import subpackage.hello_world subpackage.hello_world.do_something()

or even

from subpackage.hello_world import do_something do_something()

ultimately best tool maintain straight clear understanding of how import works , effect various forms have on importing namespace.

python

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -