How to import variables from another file in Python?
Last Updated :
03 Apr, 2025
To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name to access the required variables or functions in your current program. For example, math library. If we want to use the pi variable we use import math and then math.pi.
import Statement
We can use any Python source file as a module by executing an import statement in some other Python source file. When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches for importing a module.
import statement allows you to import a whole module and access its variables and functions using the syntax:
import filename
from import Statement
from statement allows you to import specific attributes (like variables or functions) from a module. You can directly access these attributes without using the module name as a prefix.
from filename import variable_name
Different approaches to import variables from other file
- import <file_name> and then use <file_name>.<variable_name> to access variable
- from <file_name> import <variable_names> and use variables
- from <file_name> import * and then use variables directly.
Note: For more information, refer Python Modules
Examples
Python
x = 23
y = 30
def swapVal(x, y):
x,y = y,x
return x, y
Suppose we have a file named "swaps.py". We have to import the x and y variable from this file in another file named "calval.py" then different ways to do so are:
Example 1: Using import <file_name> and accessing variables with <file_name>.<variable_name>
In this approach, we import the entire swaps module and access the variables and functions with the module name as a prefix.
Python
import swaps
# Import x and y variables using file_name.variable_name notation
nx = swaps.x
ny = swaps.y
print("x value: ", nx, "y value:", ny)
# Similarly, import swapVal method from swaps file
x , y = swaps.swapVal(nx,ny)
print("x value: ", x, "y value:", y)
Output:
x value: 23 y value: 30
x value: 30 y value: 23
Explanation:
- import swaps statement imports the entire swaps module.
- Variables x and y are accessed using swaps.x and swaps.y since we need to reference the module name to access its members.
- swapVal function is called using swaps.swapVal(nx, ny) to swap the values of x and y.
Example 2: Using from <file_name> import <variable_names> to import specific variables
In this approach, we directly import specific variables and functions from the swaps module.
Python
from swaps import x, y, swapVal
# Directly using x, y, and swapVal without the module prefix
print("x value: ", x, "y value:", y)
# Using the swapVal function
x, y = swapVal(x, y)
print("x value: ", x, "y value:", y)
Output:
x value: 23 y value: 30
x value: 30 y value: 23
Explanation:
- from swaps import x, y, swapVal imports only the specific variables and functions from the swaps module.
- This allows direct access to x, y, and swapVal without needing the swaps. prefix.
- swapVal function is invoked directly as swapVal(nx, ny) after the variables x and y are retrieved.
Example 3: Using from <file_name> import * to import all variables and functions
In this approach, we import everything from the swaps module. This means you don’t need to use the module name as a prefix.
Python
from swaps import *
# Using x, y, and swapVal directly
print("x value: ", x, "y value:", y)
# Using the swapVal function
x, y = swapVal(x, y)
print("x value: ", x, "y value:", y)
Output:
x value: 23 y value: 30
x value: 30 y value: 23
Explanation:
- from swaps import * imports all the variables and functions from the swaps module into the current namespace.
- This allows x, y, and swapVal to be used directly without any prefix.
- swapVal function is called directly as swapVal(x, y) after the variables x and y are imported.
Similar Reads
How to import a class from another file in Python ? In this article, we will see How to import a class from another file in Python.Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of in
4 min read
How to Use a Variable from Another Function in Python Using a variable from another function is important for maintaining data consistency and code reusability. In this article, we will explore three different approaches to using a variable from another function in Python. Use a Variable from Another Function in PythonBelow are the possible approaches
2 min read
Import Modules From Another Folder in Python In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two
2 min read
How To Print A Variable's Name In Python In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp
3 min read
How to Read from a File in Python Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently.Example File: geeks.txtHello World Hello GeeksforGe
5 min read
How to Import Another TypeScript Files? To use code from one TypeScript file in another, we use the module system introduced in ECMAScript 2015. This allows us to export functions, classes, or variables from one file and import them into another. By doing this, we can reuse code from previous projects without having to rewrite it. This ma
4 min read
How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
How to check if a Python variable exists? Checking if a Python variable exists means determining whether a variable has been defined or is available in the current scope. For example, if you try to access a variable that hasn't been assigned a value, Python will raise a NameError. Letâs explore different methods to efficiently check if a va
3 min read
How to fix 'Variable not defined' in custom modules in Python Encountering the 'Variable not defined' error in Python can be frustrating, especially when working with custom modules. This error indicates that Python cannot find a variable that you are trying to use. In this article, we will explore what this error means, its common causes, and the steps to res
5 min read
Exporting variable to CSV file in Python The CSV file or comma-separated values file is used to store and share data across platforms. The columns are separated by commas or other relevant delimiters, giving the data a tabular structure. Sometimes we come across ready-made CSV files while sometimes we need to create one according to the r
3 min read