Monday, November 18, 2024
Google search engine
HomeLanguagesWhere Does Python Look for Modules?

Where Does Python Look for Modules?

Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module in that file and then we can use them. Modules can exist in various directories.

In this article, we will discuss where does python looks for modules.

Python looks for modules in 3 steps:-

  1. First, it searches in the current directory.
  2. If not found then it searches in the directories which are in shell variable PYTHONPATH
  3. If that also fails python checks the installation-dependent list of directories configured at the time Python is installed

Now we will discuss each of these steps:

Step 1: Firstly the python searches in the current directory. From the current directory we mean the directory in which the file calling the module exists. We can check the working directory from os module of python by os.getcwd() method. The directory returned from this method is referred to as the current directory. The code for getting the current directory is:

Python




# importing os module
import os       
 
# printing the current working directory
print(os.getcwd())


The output of the above code will be the current working directory which will be first searched for a module to be imported.

Step 2: If the module that needs to be imported is not found in the current directory. Then python will search it in the PYTHONPATH which is a list of directory names, with the same syntax as the shell variable PATH. To know the directories in PYTHONPATH we can simply get them by the sys module. The sys.path gives us the list of all the paths where the module will be searched when it is needed to import. To see these directories we have to write the following code:

Python




# importing the sys module
import sys        
 
# printing sys.path variable of sys module
print(sys.path)


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’]
 

Step 3: If the module is not found in the above 2 steps the python interpreter then tries to find it in installation dependent list of directories that are configured at the time of installation of python. These directories are also included in sys.path variable of sys module and can be known in the same way as the above step. The code will be:

Python




# importing the sys module
import sys        
 
# printing sys.path variable of sys module
print(sys.path)


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’]
 

RELATED ARTICLES

Most Popular

Recent Comments