0

I'm running into an error when attempting to import a file in the parent directory.

Code from test/log_line.py:

#! /usr/bin/env python3 from ..lib_common import log_line 

Error:

$ py -3 test/log_line.py Traceback (most recent call last): File "test/log_line.py", line 2, in <module> from .. import lib_common ImportError: attempted relative import with no known parent package 

Directory Structure:

./ | test/ | | log_line.py | lib_common.py | __init__.py 

lib_common.py contains the log_line function. Added __init__.py at the suggestion of @Pavel.

Any idea why Python is giving me the error? Googling the traceback returned one somewhat-related result. No luck.

11
  • Create an empty file named __init__.py in ./ Commented Dec 10, 2017 at 23:36
  • @Pavel Didn't solve it. Commented Dec 10, 2017 at 23:38
  • 1
    Even with __init__.py in the root it won't work because you're making test as your entry point. It's a bad design anyway, why would you want to do such a thing? Commented Dec 10, 2017 at 23:38
  • 1
    @zwer Because I didn't know of another way. Enlighten me, what is the better way? Commented Dec 10, 2017 at 23:39
  • Start importing from your parent dictionary and work down. Commented Dec 10, 2017 at 23:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.