Python Import Module From Directory
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of python import module from directory, while working on big projects in python, we might require different modules and packages in order to complete it and make it working efficiently.
These imported modules opens a source of various preloaded features, data and other beneficial methods to make the program run efficiently and to full fill the required condition.
In this tutorial, we’re going to learn step by step that how we can import any module directly from the other directory within the python code.
Module – A module is basically, just a python coded program which have python extension, (.py).
It is a folder which contain module, and within the code this imported folder will work as a package which will open the door of preloaded features, data accessibility and other benefits.
Step By Step Guide On Python Import Module From Directory :-
Method 1 - SYS Path Method
In sys method, we add a new path of the different folder. The folder from where we want to import or extract the module within the code.
When we do that, the system path will help the python to look for that module within its directory.
Sys method comes in list type class functions eventually by using this we can go for the insert method to add the folder path to import the module in the code.
import sys sys.path.insert(0, '/folder/dellvostro/desktop/Folder2') from module1 import function_ops, function_4(4,5) print("calling function ops:", function4(argument1, argument2))
- Firstly, we imported the function or method ‘sys’
- Then, we used sys.path.insert() attribute to insert the directory of the folder we have created earlier for the module extraction within the code.
- Now, we imported module1 from the folder, and imported function ‘function_ops’.
- Then we called function_4 from the module1 and then passed the argument as values of which we need result.
- Just for a check, we printed the result of function 4 which we extracted from the module1 from the directory folder.
- At the end, we will get the desired output.
Method 2 - Pythonpath Environmental Variable Method -
Apart from the above sys method, we can also use this method to import the module within the python code from the directory.
By this method, we can import uninstalled libraries as well in the python code.
Such variable does not require python to run, so it works with the installation purpose of modules also.
In order to use pythonpath environmental variable method in the python code, first we have to set the path in terminal by using a command.
Path Setup in terminal -
- So first, run this command in terminal to set the path,
- SET PYTHONPATH= ”path/to/directory/folder”
- Now, we can proceed with the code after giving the path.
from module1 import function_1, ops ops(arguments) print("Final value of called function is", ops(5, 6))
- After setting the environmental path in terminal, we imported the module1 from the folder and then imported the function_1 from it.
- From function_1, we used ops function for the program.
- After that, we called ops function and then passed the arguments as per our need.
- At the end and for our confirmation, we printed the final value.
Conclusion :-
In this tutorial, we understood that how we can import a module from directory by using two methods.
This operation helps us to use various functions and modules within the code. I hope this tutorial on python import module from directory helps you.