0

Right, I have a structure like so:

/Project
    __init__.py
    script_a.py
    /tests
        __init__.py
        test_something.py

When test_something tries to import using from . import script_a it returns the error Attempted relative import in non-package.

I've put in the empty __init__.py files and added /Project to PYTHONPATH but it still throws up this error!

Any ideas?

EDIT: I have now used a tester.py situated in \Project and call:

import script_a
from tests.test_something import *

Now it works!!

Lucidnonsense
  • 1,072
  • 2
  • 13
  • 32

2 Answers2

1

from Project import script_a should work for you.

Rishi
  • 268
  • 1
  • 6
1

When test_something tries to import using from . import script_a it returns the error Attempted relative import in non-package.

  1. Using one dot . will lead you to the current directory. You should use two dots .. to get to the parent dir.

  2. You cannot run a module with a relative import itself. You can only import it. So test_something.py can only run as an import but it is not possible to run the script as __main__

bignose
  • 27,414
  • 13
  • 72
  • 104
embert
  • 6,902
  • 8
  • 47
  • 78