Where Does Python Look for Modules?
Last Updated :
20 Aug, 2021
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:-
- First, it searches in the current directory.
- If not found then it searches in the directories which are in shell variable PYTHONPATH
- 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']
Similar Reads
How to Import Local Modules with Python In Python, modules are self-contained files with reusable code units like functions, classes, and variables. Importing local modules allows for organizing the codebase effectively, enhance maintainability, and enhances code reuse. In this article, we will understand how to import local modules with
3 min read
Best Python Modules for Automation Automation is an addition of technology that performs tasks with reduced human assistance to processes that facilitate feedback loops between operations and development teams so that iterative updates can be deployed faster to applications in production. There are different types of automation libra
3 min read
OS Module in Python with Examples The OS module in Python provides functions for interacting with the operating system. OS comes under Python's standard utility modules. This module provides a portable way of using operating system-dependent functionality.The *os* and *os.path* modules include many functions to interact with the fil
10 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
Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
How to create modules in Python 3 ? Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t
4 min read
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
How to Install a Python Module? A module is simply a file containing Python code. Functions, groups, and variables can all be described in a module. Runnable code can also be used in a module. What is a Python Module?A module can be imported by multiple programs for their application, hence a single code can be used by multiple pr
4 min read