2

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
asked Jun 14, 2020 at 9:25
1
  • Do you mean that you want to do from foo import somefunction? In your example bar is a module, not a function. You will not be able to call it like you do in your last example. Commented Jun 15, 2020 at 18:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.