python: how to call a constructor in a module in a package in another package directly -
this question has answer here:
i porting matlab code python. need work packages , modules in case. relevant package directory structure looks this:
toppackage __init__.py subpackage __init__.py module.py ... in script use package, can work this:
from toppackage.subpackage.module import someclass s = someclass() but prefer working this:
import toppackage %somewhere @ beginning of file s = toppackage.subpackage.module.someclass() i see done in numpy. not find in documentation. how can that?
thanks in advance.
you need import contained packages in __init__.py files.
you can import packages inside toppackage/__init__.py example:
import toppackage.subpackage.module or can import each directly contained package, in toppackage/__init__.py:
from . import subpackage and in toppackage/subpackage/__init__.py:
from . import module just importing top-level package not automatically make contained packages available. need explicitly import full path once, somewhere, before works.
the numpy package imports nested packages in top-level __init__.py.
Comments
Post a Comment