KennyMcK
KennyMcK

Reputation: 35

Python Package in module call function

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.

Upvotes: 2

Views: 49

Answers (1)

Dustin Ingram
Dustin Ingram

Reputation: 21520

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)

Upvotes: 1

Related Questions