6
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
3
  • Typo? Can you paste the error you received? Commented Apr 30, 2012 at 3:37
  • If you pasted the error exactly as received it looks like you didn't use two underscores when you imported it. Commented Apr 30, 2012 at 3:52
  • Yes. Exactly what I have. I did this in console (import it within console) as well, and said the same thing. Commented Apr 30, 2012 at 3:57

4 Answers 4

9
$ 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to test them, yet, make them "private" so developers know they are not public APIs. Let me try single underscore.
Generally that's what single-underscore methods are used for.
Thanks. I've edited my post. Let me try something in the meantime. Thanks.
3
from "module" import *

only import "public" fields and functions. For private fields and functions, you need to explicitly import them as mentioned in other answers. See below for a full example. Tested on python 3.7.4

$ cat file_w_privates.py

field1=101
_field2=202
__field3=303
__field4__=404

def my_print():                                                                                
  print("inside my_print")                                                                                                                                                                
def __private_print():
  print("inside __private_print")                                                                                             
def __private_print__():                                                                       
  print("inside __private_print__")

$ cat file_import.py

from file_w_privates import _field2, __field3, __field4__ # import private fields
from file_w_privates import  __private_print, __private_print__ # import private functions
from file_w_privates import *   # import public fields and functions

print(f"field1: {field1}")
print(f"_field2: {_field2}")
print(f"__field3: {__field3}")
print(f"__field4__: {__field4__}")

my_print()
__private_print()
__private_print__()

Comments

2

Are you sure you have the latest source imported? Double check that you are importing the latest source.

check it by doing this:

>> import myfile.ext
>> print dir(myfile.ext)

You should see all the methods (I think double underscore will be skipped, but single will still appear). If not, it means you have an old version.

If this shows okay, but you still can't import the actual thing. Make a virtualenv, and try again.

Comments

1

There's nothing that should prevent you from importing a module level function that's been declared using __. Python implements name mangling on "private" methods of a class to avoid accidents in derived classes, but it should not affect a module level definition. I my tests I have no problem with this:

File A


def __test(): pass

File B


from A import __test

print __test  # output is <function __test at 0x024221F0>

I say "private" because someone who's interested in doing so can still import those methods.

1 Comment

Thanks. Iv'e updated my post. I can import other methods using the same code above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.