Thursday 21 June 2012

How to import module from different folder in Python

Let say we have a folder structure like this:

Project
-- controllers
   -- controllerA.py
-- unit_testing
   -- test_controllerA.py

We want to import controllerA to be used in test_controllerA. So in test_controllerA.py we should add these code:

/**  test_controllerA .py **/
import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))

/**Now we can import the class **/
from controllers.controllerA import controllerA