Python: Importing a file from a parent folder

Posted by Sascha on Stack Overflow See other posts from Stack Overflow or by Sascha
Published on 2011-01-09T00:24:49Z Indexed on 2011/01/09 0:54 UTC
Read the original article Hit count: 263

Filed under:

...Now I know this question has been asked many times & I have looked at these other threads. Nothing so far has worked, from using sys.path.append('.') to just import foo

I have a python file that wishes to import a file (that is in its parent directory). Can you help me figure out how my child file can successfully import its a file in its parent directory. I am using python 2.7

The structure is like so (each directory also has the __init__.py file in it):

StockTracker/
__Comp/
____a.py
____SubComp/
______b.py

Inside b.py, I would like to import a.py: So I have tried each of the following but I still get an error inside b.py saying "There is no such module a"

import a

import .a

import Comp.a

import StockTracker.Comp.a

import os
import sys
sys.path.append('.')
import a    
sys.path.remove('.')

© Stack Overflow or respective owner

Related posts about python