# Python __all__ and the scope over files - __all__ should be used when importing * in outer files

Many Python modules __all__ to declare variables or functions that outer files can access. In short, the objects in __all__ are "public" and others are "private". This article supposes the Python application has util directory and it has example.py and helper.py.

/util
-example.py
-helper.py


helper.py

__all__ = ['a', 'b']

a = 1
b = 'JavaScript'

pi = 3.14


example.py

from util.helper import *

print(a)  # 1
print(b)  # moon
print(pi)  # NameError: name 'pi' is not defined


In helper.py, 3 variables are set but only a and b are in __all__. So outer files can access these but can't read pi. Actually, example.py can't read pi and raises NameError exception.

But __all__ works properly only in the case that an outer file import *. If it imports the file name, __all__ can't prohibit the access from outer files.

example.py

from util import helper

print(helper.a)  # 1
print(helper.b)  # moon
print(helper.pi)  # 3.14


helper.pi is printed. After all, __all__ is not a bar to protect the data from outer accesses.