How to locate a particular module in Python? - GeeksforGeeks

06 Jun, 2021
In this article, we will see how to locate a particular module in Python? Locating a module means finding the directory from which the module is imported. When we import a module the Python interpreter searches for the module in the following manner:
First, it searches for the module in the current directory.
If the module isn’t found in the current directory, Python then searches each directory in the shell variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of directories.
If that also fails python checks the installation-dependent list of directories configured at the time Python is installed.
The sys.path contains the list of the current directory, PYTHONPATH, and the installation-dependent default. We will discuss how to use this and other methods to locate the module in this article. 
For a pure python module we can locate its source by module_name.__file__. This will return the location where the module’s .py file exists. To get the directory we can use os.path.dirname() method in os module. For example if we want to know the location of ‘random’ module using this method we will type the following in the python file
Python
# importing random moduleimport random  # importing the os moduleimport os  # storing the path of modules file # in variable file_pathfile_path = random.__file__  # storing the directory in dir variabledir = os.path.dirname(file_path)  # printing the directoryprint(dir)
Output:
C;\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib
For this method, we will be using the sys module. The sys.path variable of sys module contains all the directories which will be searched for modules at runtime. So by knowing these directories we can manually check where our particular module exists. To implement this we have to write the following in python shell:-
Python
# importing sys moduleimport sys  # importing sys.pathprint(sys.path)
This will return the list of all directories which will be searched for the module at runtime.
Output:
[‘/home’, ‘/usr/lib/python2.7’, ‘/usr/lib/python2.7/plat-x86_64-linux-gnu’, ‘/usr/lib/python2.7/lib-tk’, 
‘/usr/lib/python2.7/lib-old’, ‘/usr/lib/python2.7/lib-dynload’, ‘/usr/local/lib/python2.7/dist-packages’, 
‘/usr/lib/python2.7/dist-packages’]
In the python shell after we import some modules we can get various information about the module using help(module_name). For example, if we want to know the location of the module os using this method we will type the following in python shell
Python
# importing os moduleimport os  # printing help(os)print(help(os))
Under various information, we will find a heading with the name FILE below which the location of the module will be present.
Output:
....
/various other informartion/
....
FILE
      c:\Users\lenovo\appdata\local\programs\python\python39\lib\os.py
We can also use the inspect module in python to locate a module. We will use inspect.getfile() method of inspect module to get the path. This method will take the module’s name as an argument and will return its path. For example, if we want to find the directory of the os module using this method we will write the following code:
Python
# importing random moduleimport inspect  # importing the os moduleimport os  # printing the file path of os moduleprint(inspect.getfile(os))
Output:
C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\os.py
Picked
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()