0
votes

I want to make a distributable python module, so I want to make users call functions inside of my module via just calling package name.

e.g.

/my_package/
/my_package/__init__.py
/my_package/src/
/my_package/src/__init__.py
/my_package/src/my_module.py

and, the contents of mymodule.py:

def test(): print('hello!')

in this case, I want to let users use my module like:

>>> import mypackage as mp
>>> mp.test()
hello!

just like tensorflow, numpy, etc. !!!

How should I configure __init__.py file and path info??

Thank you in advance :)

2

2 Answers

1
votes

In the file:

/my_package/__init__.py

add the line:

from src.my_module import test
0
votes

I assume that you don't just want to expose test function but a lot of other functions. So, you can separately maintain a __all__ list in your my_package/__init__.py that exposes any external methods to be used by others.

So, your my_package/__init__.py would look like

from src.my_module import test


__all__ = [test, ]

You can read about __all__ here: https://docs.python.org/2/tutorial/modules.html#importing-from-a-package