3

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??

2 Answers 2

2

In the file:

/my_package/__init__.py

add the line:

from src.my_module import test
Sign up to request clarification or add additional context in comments.

1 Comment

One more question... If I want to "hide" the internal packages, what should I do?? e.g. if the repository has "MyClass" in /my_package/src/my_module.py, it returns "<class my_package.src.my_module.MyClass .. blah blah>" when users type "type(MyClassInstance)" on console. I want to show it like: <class my_package.MyClass .. blah blah> Any suggestions?? I've already tried to change the string of self.__module__, but it did not work.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.