Description
Assume, I have a package with the following structure:
package-folder
|_ mypackage
|_ __init__.py
|_ module1.py
setup.py
Where __init__.py
is empty and
module1.py
:
def do_stuff():
print('Did stuff.')
setup.py
is containing the usual context for making a package out of this, which can be installed with pip install -e pkgpath
on the local system.
In a script.py
, I would have to do:
import mypackage as abbr
abbr.module1.do_stuff()
# output: Did stuff.
My desired call would be:
abbr.do_stuff()
# output: Did stuff.
Questions
- How can I 'organize' my package so that I can call functions, out of module1.py directly from
abbr.function()
? What code changes are required? - Is this organization useful if the package grows? So that directories would act as the structure-element and python-files are just capsules to organize my functions within?
- What is the best way to organize functions in modules and sub-modules?
Further thoughts
- Is there a good reason not to have just one function in each python-file which can be used from the module? (and has the same name as the file)
- I assume, I will have to modify my
__init__.py
-files, but I couldn't figure out how.
Clarification on questions 2
package-folder
|_ mypackage
|_ __init__.py
|_ module1.py #with function do_stuff1()
|_ subpackage
|_module2.py #with function do_stuff2()
setup.py
Calls should be:
abbr.do_stuff1()
abbr.subpackage.do_stuff2()
And not:
abbr.module1.do_stuff1()
abbr.subpackage.module2.do_stuff2()