Tuesday 10 March 2015

PYTHONPATH and importing modules in python

What is PYTHONPATH

PYTHONPATH is an environment variable that defines additional locations of Python modules. This is the default search path for module files. This means that python interpreter will search for modules in the location of the PYTHONPATH.

How importing works in python

When a module named mymodule is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named mymodule.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
  • The directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

problem scenario and solve:

A
|--__init__.py
|
|--B
|--|--__init__.py
|--|--greetings.py
|
|--C
|--|--__init_.py
|--|--mymodule.py

Here A is a python package. Inside A, there are two packages B and C. 

greetings.py
def my_function():
    print "Congrats Bangladesh Cricket Team!! :D"

mymodule.py
from B.greetings import my_function
my_function()

Now, there is a problem. Package A  is not in python interpreter's search path. So it can not import greetings function from  within C. If mymodule.py is run, it will raise an ImportError as expected.

$ python mymodule.py
Traceback (most recent call last):
  File "mymodule.py", line 3, in <module>
    from B.greetings import my_function
ImportError: No module named A.B.greetings

To solve it, we need to add the absolute path of package A in sys.path inside mymodule.py before importing greetings from B. Path will be appended in sys.path. This will not affect other python program because the path will not be modified for any other Python processes. 

mymodule.py
import os
import sys

PATH = os.path.abspath(os.path.join(os.pardir))
sys.path.append(PATH)

from B.greetings import my_function
my_function()

Now as the path of A is added, Python interpreter can find the package B inside A. Let's run again this function

$ python mymodule.py
Congrats Bangladesh Cricket Team!! :D



No comments :

Post a Comment