python relative import error when referencing file
Question: Now I got relative imports to work by [following
this](http://stackoverflow.com/a/15458607/3834059). But I am getting some
errors where the files are referenced with the wrong relative paths.

My folder structure is something like this

    
    
    spamfilter_app
    ├── Makefile
    └── spamfilter
        ├── classifier
        │   ├── classifierNB.py
        │   ├── exceptions.py
        │   ├── __init__.py
        │   └── train.py
        ├── data
        │   ├── corpus1
        │   ├── corpus2
        │   └── corpus3
        ├── __init__.py
        ├── logfiles
        │   └── logfile.txt
        └──run
            ├── __init__.py
            └── test.py
    

It's absolute path is `/home/tasdik/Desktop/spamfilter_app`

* * *

My files look something like this

**test.py**

    
    
    from ..classifier.train import Trainer
    
    CUR_DIR = os.path.abspath('.')
    PARENT_DIR = os.path.abspath(os.path.join(CUR_DIR, os.path.pardir))
    LOGGING_FILE = os.path.join(PARENT_DIR, 'logfiles', 'logfile.txt')
    CORPUS_DIR = os.path.join(PARENT_DIR, 'data')
    
    logging.basicConfig(
        filename=LOGGING_FILE,
        level = logging.DEBUG,
        filemode = 'w',
        format = '%(asctime)s - %(levelname)s - %(message)s'
    )
    

* * *

**train.py**

    
    
    from .classifierNB import NaiveBayesClassifier
    
    CUR_DIR = os.path.abspath('.')
    PARENT_DIR = os.path.abspath(os.path.join(CUR_DIR, os.path.pardir))
    LOGGING_FILE = os.path.join(PARENT_DIR, 'logfiles', 'logfile.txt')
    
    logging.basicConfig(
        filename=LOGGING_FILE,
        level = logging.DEBUG,
        filemode = 'w',
        format = '%(asctime)s - %(levelname)s - %(message)s'
    )
    

* * *

**classifierNB.py**

    
    
    CUR_DIR = os.path.abspath('.')
    PARENT_DIR = os.path.abspath(os.path.join(CUR_DIR, os.path.pardir))
    LOGGING_FILE = os.path.join(PARENT_DIR, 'logfiles', 'logfile.txt')
    
    logging.basicConfig(
        filename=LOGGING_FILE,
        level=logging.DEBUG,
        filemode='w',
        format='%(asctime)s - %(levelname)s - %(message)s'
    )
    

**Error that I get**

When I do `$ python -m spamfilter.run.test` from the `spamfilter` parent
directory, I get this error. Here is the traceback.

    
    
    $ python -m spamfilter.run.test
    Traceback (most recent call last):
      File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
        "__main__", fname, loader, pkg_name)
      File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
        exec code in run_globals
      File "/home/tasdik/Desktop/spamfilter_app/spamfilter/run/test.py", line 18, in <module>
        from ..classifier.train import Trainer
      File "spamfilter/classifier/train.py", line 27, in <module>
        from .classifierNB import NaiveBayesClassifier
      File "spamfilter/classifier/classifierNB.py", line 43, in <module>
        format='%(asctime)s - %(levelname)s - %(message)s'
      File "/usr/lib/python2.7/logging/__init__.py", line 1540, in basicConfig
        hdlr = FileHandler(filename, mode)
      File "/usr/lib/python2.7/logging/__init__.py", line 911, in __init__
        StreamHandler.__init__(self, self._open())
      File "/usr/lib/python2.7/logging/__init__.py", line 936, in _open
        stream = open(self.baseFilename, self.mode)
    IOError: [Errno 2] No such file or directory: '/home/tasdik/Desktop/logfiles/logfile.txt'
    

The `logfile.txt` has been given a wrong absolute path. I tried hard coding
the path for `logile.txt`, but I faced the same problem when corpus
directories were to be accessed

Could anybody explain where am I going wrong?

**EDIT**

As suggested, I added the project path to `sys` path by adding

    
    
    sys.path.append("/home/tasdik/Desktop/spamfilter_bad")
    

in each of the mentioned files. And did the imports like

    
    
    from spamfilter.classifier.classifierNB.py import NaiveBayesClassifier
    

I think the imports happen just fine, but the file `logfile error still
persists`.

Here is an updated error traceback

    
    
    $ python -m spamfilter.run.test
    Traceback (most recent call last):
      File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
        "__main__", fname, loader, pkg_name)
      File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
        exec code in run_globals
      File "/home/tasdik/Desktop/spamfilter_app/spamfilter/run/test.py", line 22, in <module>
        from spamfilter.classifier.train import Trainer
      File "spamfilter/classifier/train.py", line 30, in <module>
        from spamfilter.classifier.classifierNB.py import NaiveBayesClassifier
      File "spamfilter/classifier/classifierNB.py", line 46, in <module>
        format='%(asctime)s - %(levelname)s - %(message)s'
      File "/usr/lib/python2.7/logging/__init__.py", line 1540, in basicConfig
        hdlr = FileHandler(filename, mode)
      File "/usr/lib/python2.7/logging/__init__.py", line 911, in __init__
        StreamHandler.__init__(self, self._open())
      File "/usr/lib/python2.7/logging/__init__.py", line 936, in _open
        stream = open(self.baseFilename, self.mode)
    IOError: [Errno 2] No such file or directory: '/home/tasdik/Desktop/logfiles/logfile.txt'
    


Answer: **Edit** : replace `os.path.abspath('.')` with `os.path.abspath(__file__)`

`os.path.abspath('.')` gives you different results when you execute your
script in different folders. `.` is the current path, it is based on which
path invokes the script

**Original Post**

You need to add your project into sys path, and make sure you have
`__init__.py` under folder `spamfilter`

`test.py`

    
    
    import sys
    sys.path.append("/path/to/spamfilter_app")
    from spamfilter.classifier.train import Trainer
    

