Search code examples
pythondjangopython-importpython-module

Import modules outside django root


I have a folder structure like this

Project/


        Common-resuable-code-i-wrote/
                                     folder1
                                     folder2
        WebApp/
               templates/
               views
               urls
               etc

I am trying to import folder1 from a module within the webapp folder. The webserver starts but when i request a page which uses this code it throws an exception about not being able to find the module. Is there a way to get this to work without refactoring everything?


Solution

  • Update your PYTHONPATH to be Project or a parent folder of Project

    http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

    After doing this, or if you already have done this, insert a file called __init__.py in your Common-reusable-code-i-wrote folder, and all subfolders inside there that you want to import code from.