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:
    print(path)

Output:

C:\Users\This PC\AppData\Local\Programs\Python\Python310\Lib\idlelib
C:\Users\This PC\AppData\Local\Programs\Python\Python310\python310.zip
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__)
print(path)

This method will print the absolute path of the module.

Output:

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

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

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

Output:

'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

print(help(os))

Output:

Help on module os:

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

........

........

........

FILE

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

Method 3: Using importlib module

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

import importlib.util
print(importlib.util.find_spec("os"))

Output1:

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

Output2:

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

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

import importlib.util
print(importlib.util.find_spec("requests").origin)

Output3:

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

Method 4: Using inspect module

import inspect
import os
print(inspect.getfile(os))

Output:

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

Method 5: Directly printing module

import os, random
print(os)
print(random)

Output:

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

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

Thank you for visiting our website.


Also Read:

Share:

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 admin@copyassignment.com Thank you