Have you ever used a Python file’s variables after importing (`from F import *`) it into another Python file? In this scenario, every variable from that file can be used in the second file where the import is made. In this article, we'll look at one Python mechanism, called __all__, which allows users to import only some variables rather than the entire set of variables from a Python file.
What is __all__ in Python?
In Python, `__all__` is a list of strings that defines what names should be imported from the current module (Python file), to another file. Only the variables that are declared in that list can be used in that other file after importing this file; the other variables, if referenced, will throw an error.
It also works in a package: in that case `__all__` is defined in the package's `__init__.py` file, and lists the public modules that will be imported from that package when `from package import *` is used.
Benefits of __all__ in Python
- Security Purpose: While importing the data from one Python file to another Python file, all the variables of the first file are usable in the second file. But due to security purposes, we want the second file to use only certain variables and not all the variables of the first file, then we can use the __all__ variable in the first Python file and define the usable variables in it.
- Usage of the same variable name: Let us suppose, we have declared one variable, titled XYZ in the first Python file and the first Python file is being called by another Python file, then all the variables of the first file will be imported into the second file. But in case, we have no use of the XYZ variable of the first file in the second file, then we can limit it by using __all__, and declare if needed the XYZ variable again in the second file and use it. Note that if you redefine a variable imported by `*`, your definition will have precedence over the imported one.
Syntax of __all__ in Python
__all__ = ['variable_1', 'variable_2']
Example:
First of all, in the file that will be imported via `from file import *`, declare some variables and assign them values. Then, create a list of the variables that you want to be imported into another file and declare them in the __all__ variable.
gfg1.py
Let us suppose we have two files gfg1.py and gfg2.py. We need to import certain variables of gfg1.py into gfg2.py. Thus, whatever we want to get imported from file1 to file2, we will define it in the __all__ variable, which is basically a list of strings.
Python3
# Declare some variables
app = 10
ball = True
cat = 'kitten'
dog = 100.0
# Define variables to be imported
# when module is imported
__all__ = ['app', 'ball']
Case 1: Calling the variable 'app' from the gfg1.py file
In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable app declared in the gfg1.py file.
Code for file gfg2.py:
Python
# Importing the gfg1.py file
from gfg1 import *
# Print the variable app
print(app)
Output:

Case 2: Calling the variable 'ball' from the gfg1.py file
In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable ball declared in the gfg1.py file.
Code for file gfg2.py:
Python3
# Importing the gfg1.py file
from gfg1 import *
# Print the variable ball
print(ball)
Output:

Case 3: Calling the variable 'cat' from the gfg1.py file
In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable cat declared in the gfg1.py file. As the variable is not declared under the __all__ list, it should pop up the error.
Code for file gfg2.py:
Python3
# Importing the gfg1.py file
from gfg1 import *
# Print the variable cat
print(cat)
Output:

Case 4: Calling the variable 'dog' from the gfg1.py file
In this example, we will import the gfg1.py file in the gfg2.py file and print the value of the variable dog declared in the gfg1.py file. As the variable is not declared under the __all__ list, it should pop up the error.
Code for file gfg2.py
Python3
# Importing the gfg1.py file
from gfg1 import *
# Print the variable dog
print(dog)
Output:

Note: As __all__ is a list, so we can only pass the string values in the list and not the function. Let us take an example of passing a function in __all__ and calling it to see what will happen in that case. We can see clearly that it popped out the error 'Item in __all__ must be str, not function.'
Python3
# Program to pass function
# in __all__ in Python
# Declare some variables
a = 10
b = 6
# Define names to be imported
# when module is imported
__all__ = ['a', 'b', lambda a,b:a+b]
Output:
Similar Reads
Python 101
Welcome to "Python 101," your comprehensive guide to understanding and mastering the fundamentals of Python programming. Python is a versatile and powerful high-level programming language that has gained immense popularity due to its simplicity and readability. Whether you're an aspiring programmer,
13 min read
Python for AI
Python has become the go-to programming language for artificial intelligence (AI) development due to its simplicity and the powerful suite of libraries it offers. Its syntax is straightforward and closely resembles human language, which reduces the learning curve for developers and enables them to f
7 min read
Python - all() function
The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True otherwise it returns False. It also returns True if the iterable object is empty. Sometimes while working on some code if we want to ensure that user has not entered a False v
3 min read
re.findall() in Python
re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule. We can do this using regular expressions (regex) to create the pattern and then use re.findall() to get a list of matches.Let's say
2 min read
Python 3 basics
Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the
10 min read
Python Features
Python is a dynamic, high-level, free open source, and interpreted programming language. It supports object-oriented programming as well as procedural-oriented programming. In Python, we don't need to declare the type of variable because it is a dynamically typed language. For example, x = 10 Here,
5 min read
Learn Python Basics
âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in
9 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Gotchas in Python
Python is a go-to language for most of the newcomers into the programming world. This is because it is fairly simple, highly in-demand, and ultimately powerful. But there are some cases which might confuse or rather trick a rookie coder. These are called "Gotchas"! Originating from the informal term
5 min read
as Keyword - Python
as keyword in Python plays a important role in simplifying code, making it more readable and avoiding potential naming conflicts. It is mainly used to create aliases for modules, exceptions and file operations. This powerful feature reduces verbosity, helps in naming clarity and can be essential whe
3 min read