0

I am converting to new style relative imports in Python 3.x. I am currently getting the error below when I try to perform an import from File1.py to utilsx.py

ValueError: Attempted relative import in non-package

I have the below folder structure. A project called MySuite contains all the tests in tests folder which is in the MySuite Project. These tests depend on certain utilities that exist in the utils project.

└── project
    ├── utils
    │   ├── utils1.py
    │   └── utils2.py
    └── MySuite
        ├── Runner.py
        └── Tests
            └── File1.py
            └── File2.py
            └── File3.py

The import statement that I am attempting to use from File1 is:

from .utils.utils1 import *

I also tried:

from ..utils.utils1 import *

While I only expect one of them to work neither do! Any ideas to change syntax or maybe I forgot something?

Keith
  • 4,129
  • 3
  • 13
  • 25
  • does this https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory helps? – P.hunter Oct 27 '19 at 04:54
  • A question from 10 years ago uses the old style relative imports. I'm trying to use the new path support in 3.x. Although I had mentioned Python 3 already I made my first sentence more clear. – Keith Oct 27 '19 at 05:10
  • @napuzba Interesting read but it doesn't resolve my up 2 directories and over issue. Only the projects are checked in (utils and MySuite). MySuite depends on the utils project but are not contained together as a project. The additional containing folder doesn't exist and cannot be in this case. :( I'm still looking for a solution. Just as another note these two projects are owned by different owners. – Keith Oct 28 '19 at 07:08

1 Answers1

0

Don't use any "." before utils.Like you can do

from utils.utils1 import *
ksouravdas
  • 124
  • 7
  • Removing the . returns: No module named fmsPythonUtilities.TestInfo – Keith Oct 27 '19 at 17:16
  • try doing something like this. export PYTHONPATH="${PYTHONPATH}:/path/to/your/module/" or add the "utils" package to your python's dist-packages/side-packages. – ksouravdas Oct 27 '19 at 20:28
  • Your last comment is the old pre Python 3 technique that I'm trying to get rid of. To move to a more standard Python 3 configuration. Little lost why the Python team didn't do other standard language support for paths with import. All I 'should' have to do is something like: from ../../utils/utils1 import * I understand I'm mixing Python and C++ style coding but it's just to get the idea across. I don't believe anybody would have gotten lost. I do see a bunch of confusion on this topic. – Keith Oct 28 '19 at 01:41