Python Package in module call function

Viewed 41

First of all sorry if this is a very simplet question. I have a PyPi project with the following structure:

foo-->bar.py
foo-->__init__.py
setup.py

bar.py has the following contents

def somefunction(a,b):
     Do something with a,b

To call the function and use it I have to use something like the following:

import foo.bar as something
something.somefunction(x,y)

Typically in a module you don't have to use such long calls, I'd like to shorten it to something like this:

from foo import bar
bar(x,y)

I know it is something quite simple but not quite grasping it. Any help appreciated.

1 Answers

If you want bar to be a function, and not a module, you need to move somefunction into __init__.py and rename it:

__init__.py:

def bar(a,b):
    '''Do something with a,b'''
    pass

Then you can do:

from foo import bar
bar(x,y)
Related