How can I import private methods in Python?

python

Solution

$ cat foo.py
def __bar():
    pass
$ cat bar.py
from foo import __bar

print repr(__bar)
$ python bar.py
<function __bar at 0x14cf6e0>

Perhaps you made a typo?

However, normally double-underscore methods aren't really necessary - typically "public" APIs are zero-underscores, and "private" APIs are single-underscores.

Problem

``` def __hello_world(*args, **kwargs): ..... ``` and I tried ``` from myfile import __helloworld ``` I can import the non private one. How do I import private methods? Thanks. I am now using a single underscore. ``` Traceback (most recent call last): File "test.py", line 10, in <module> from myfile.ext import _hello_world ImportError: cannot import name _hello_world ``` in my test.py ``` sys.path.insert(0, os.path.abspath( os.path.join( os.path.dirname(__file__), os.path.pardir))) from myfile.ext import _hello_world ```

Original source