Retrieve a module path in Python: 5 Methods

import a_module

Now, you can use this ‘a_module’ inside your program but do you know where Python looks for modules or retrieve a module path? Let’s check.

There are 3 places where Python checks for Python modules:

  1. Current directory: When you run any program then initially Python looks for modules in the directory in which you are running the program.
  2. PYTHONPATH: It is an environment variable set by the user when the user installs Python in the system. Click here to know more about PYTHONPATH.
  3. Other Python directories: If Python fails to find the required modules in the above two, then it checks in other Python directories that were configured at the time of installation of Python.

Try this code:

Retrieve a module path in Python

Python looks for modules or retrieve a module path mainly from the given below paths:

‘C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib’
‘C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib\site-packages’

Code to get the path where Python looks for modules or retrieve a module path:

Method 1: Using sys module

import sys
paths = sys.path
for path in paths:


C:\Users\This PC\AppData\Local\Programs\Python\Python310\Lib\idlelib
C:\Users\This PC\AppData\Local\Programs\Python\Python310\
C:\Users\This PC\AppData\Local\Programs\Python\Python310\DLLs
C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib
C:\Users\This PC\AppData\Local\Programs\Python\Python310
C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib\site-packages

Method 2: Using os module

import random
path = os.path.abspath(random.__file__)

This method will print the absolute path of the module.


'C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib\'

You can use the method below to print the path to the directory of the module.

import random
path = os.path.dirname(random.__file__)


'C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib'

Another method is using the help() method of the os module, this method will give 1521 lines of output and the path of the os module will be given at the end of all lines.

import os



Help on module os:

    os - OS routines for NT or Posix depending on what system we're on.





      c:\users\this pc\appdata\local\programs\python\python310\lib\

Method 3: Using importlib module

This method is fast because it retrieves the module path without loading it.

import importlib.util


ModuleSpec(name='os', loader=<_frozen_importlib_external.SourceFileLoader object at 0x000001F3E8495690>, origin='C:\\Users\\This PC\\AppData\\Local\\Programs\\Python\\Python310\\lib\\')
import importlib.util


C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib\

If the module/ library is not installed in your system, this method will through AttributeError.

import importlib.util


Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'origin'

Method 4: Using inspect module

import inspect
import os


C:\Users\This PC\AppData\Local\Programs\Python\Python310\lib\

Method 5: Directly printing module

import os, random


<module 'os' from 'C:\\Users\\This PC\\AppData\\Local\\Programs\\Python\\Python310\\lib\\'>

<module 'random' from 'C:\\Users\\This PC\\AppData\\Local\\Programs\\Python\\Python310\\lib\\'>

Thank you for visiting our website.

Also Read:


Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at Thank you