• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

How to make visible functions at package level in python?

I have package that contains several modules. I would like to expose some functions from a module to the package level.

How to do that?

package
__init__.py
module.py that contains function

So ...
# example
 
import package
 
package.function()
 
# here the function definition is in the module.py
# how to avoid this:
 
import package.module
 
package.module.function()

Open in new window

0
koleto115
Asked:
koleto115
  • 2
1 Solution
 
Roger BaklundCommented:
Use this syntax:

from package import function

... or:

from package.module import function
0
 
koleto115Author Commented:
I want to keep the package name as a root name and to expose the function defined in the module at package level. What I need is suppose the package name is effects and it contain module echo.py and the function delay() is defined inside the module echo.py so I need to do this:

import effects

effects.delay()

NOT

import effects.echo

I would like to keep the root name of the package.

0
 
Roger BaklundCommented:
Within effects/__init__.py, do:

from echo import delay
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now