How to import a module from a directory on level above the current script

gchart, import, python

Solution

The `__init__.py` file of the GChartWrapper package expects the GChartWrapper package on PYTHONPATH. You can tell by the first line:

from GChartWrapper.GChart import *

Is it necessary to have the GChartWrapper included package in your package directory structure? If so, then one thing you could do is adding the path where the package resides to sys.path at run time. I take it `myview.py` is in the `myapp\view` directory? Then you could do this before importing `GChartWrapper`:

import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'utils')))

If it is not necessary to have it in your directory structure, it could be easier to have it installed at the conventional location. You can do that by running the setup.py script that's included in the GChartWrapper source distribution.

Problem

For my Python application, I have the following directories structure: ``` \myapp \myapp\utils\ \myapp\utils\GChartWrapper\ \myapp\model\ \myapp\view\ \myapp\controller\ ``` One of my class in \myapp\view\ must import a class called GChartWrapper. However, I am getting an import error... ``` myview.py from myapp.utils.GChartWrapper import * ``` Here is the error: ``` <type 'exceptions.ImportError'>: No module named GChartWrapper.GChart args = ('No module named GChartWrapper.GChart',) message = 'No module named GChartWrapper.GChart' ``` What am I doing wrong? I really have a hard time to import modules/classes in Python...

Original source