r xu r xu - 1 year ago 131
Python Question

Hide import in library (Python)

How do I hide imports in my library?


import numpy as np


import library

result (python3 test.py)

['__builtins__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__spec__',

is a module of the library.

What is a sensible way to hide the library that I used for writing a library?

Answer Source

If you want to mark the import as not a part of your public API, give the module a name with a leading underscore:

import numpy as _np

If you want to hide the name completely, you'll need to perform the import inside each function of your module that uses NumPy:

def f1(stuff):
    import numpy as np

def f2(stuff):
    import numpy as np

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download