User007
User007

Reputation: 1559

How can I import private methods in Python?

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

Upvotes: 6

Views: 11383

Answers (4)

Jonathan L
Jonathan L

Reputation: 10668

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__()

Upvotes: 3

CppLearner
CppLearner

Reputation: 17040

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.

Upvotes: 2

g.d.d.c
g.d.d.c

Reputation: 47988

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.

Upvotes: 1

Amber
Amber

Reputation: 526633

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

Upvotes: 9

Related Questions