0

So here's the case, I want to import a module, say a.py. However I don't want other modules which is imported by a.py in it. my a.py looks like this:

__all__ = ["fuc"]

import os

def fuc():
    return 1

then, in the same dir i create a file called b.py, which is simply prints out the modules in a:

import a
print(dir(a))

Simply running b.py gives me the output: ['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fuc', 'os'], which 'os' is still in there.

Why is __all__ not working here?

SimZhou
  • 466
  • 1
  • 7
  • 14
  • 2
    I think `__all__` limits imports only when `from a import *` syntax is used. Though, star imports are often discouraged. – Ankur Jan 21 '21 at 04:51
  • 1
    Why do you want to prevent this? It doesn't *re*import the module. Module imports are cached, in any case, it just makes it available in the module's namespace. If it *really* bothers you, just don't add it to the global namespace of your module – juanpa.arrivillaga Jan 21 '21 at 04:55
  • Does this answer your question? [Python: 'Private' module in a package](https://stackoverflow.com/questions/3602110/python-private-module-in-a-package) – user202729 Jan 21 '21 at 05:41

0 Answers0