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.
Dustin Ingram
21.8k7 gold badges69 silver badges86 bronze badges
1 Answer 1
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)
answered Jun 15, 2020 at 18:53
Dustin Ingram
21.8k7 gold badges69 silver badges86 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
from foo import somefunction? In your examplebaris a module, not a function. You will not be able to call it like you do in your last example.