In python, we perform most of our tasks by importing different in-built libraries. In this article, we will discuss how we can In this article, we have discussed how to import a python file into a python program.
Import File From Current Working Directory
We can use any file as a module to import code. For this, we will use the import statement. For instance, suppose that we have the following file named sample_module.py
in the current working directory.
The code in sample_module.py is as follows.
def print_module_name():
print("Hi, I am in the sample module file.")
To import the functions defined sample_module.py, we can simply import the sample_module.py using the following statement.
import sample_module
Here, you can observe that we have only written the file name and not the file extension in the import statement. After importing the file, you can use the functions defined in the python file as shown below.
import sample_module
print("I am in example file. Calling functions from imported file.")
sample_module.print_module_name()
Output:
I am in example file. Calling functions from imported file.
Hi, I am in the sample module file.
Import File From a Different Directory Into a Python Program
Any python program can import the modules directly from certain directories. These directories are defined in the file path. To look at those directories, we can use the sys module. The path attribute in the sys module contains the name of all the directories from which the current python program can import other modules. You can print all such directories using sys.path
attribute as shown below.
import sys
print(sys.path)
Output:
['/home/aditya1117/import python example', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/aditya1117/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.10/dist-packages']
If the file path of any directory is present in the sys.path
attribute, we can import the module from that directory directly into our python file. You can observe that the current working directory "import python example"
is present in the sys.path
list. That’s why we were able to import the sample_module file directly into our program.
Hence, to import a file from another directory, we first have to add the file path of the directory to sys.path
attribute. As we know that the sys.path
attribute contains a list of file paths, we can append the file path of the directory to the python list using the append()
method. After adding the file path of the directory to the sys.path
attribute, we can import the python files defined in the directory into our current python program.
You can observe this in the following example.
import sys
print("Hi, I am in example file.")
print(sys.path)
sys.path.append("/home/aditya1117/codes")
print("New file paths:")
print(sys.path)
import sample
sample.module_name_printer()
Output:
Hi, I am in example file.
['/home/aditya1117/import python example', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/aditya1117/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.10/dist-packages']
New file paths:
['/home/aditya1117/import python example', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/aditya1117/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.10/dist-packages', '/home/aditya1117/codes']
Hi, I am in sample.py file in \codes directory.
Conclusion
In this article, we have discussed how to import a python file into a python program. To know more about python programming, you can read this article on file handling in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.