2

I have this weird Python import and I can't get it to work with any of the suggestions in other discussions.

I'm currently adding a small script to someone else's module, so I cannot change the file structure or any of the existing code, which makes things difficult.

Here is an example of the python script which contains a bunch of classes:

/path/to/root/directory/aaa/bbb/ccc/utils.py

The current developer imports this as follows:

from aaa.bbb.ccc import utils
utils.SomeClass.someMethod()

All directories in the tree have a __init__.py file

Now I want to call the module externally as follows:

import sys
sys.path.append('/path/to/root/directory')
from aaa.bbb.ccc import utils
utils.SomeClass.someMethod()

This does NOT work, and gives the the following error:

from aaa.bbb.ccc import utils
ImportError: No module named ccc

However, changing the import a little bit does work:

import sys
sys.path.append('/path/to/root/directory')
from aaa.bbb.ccc.utils import *
SomeClass.someMethod()

I do not understand why the 2nd one works and not the 1st. Again, I cannot change the existing code, so the following 2 statements must work with my sys.path.append and imports:

from aaa.bbb.ccc import utils
utils.SomeClass.someMethod()

I cannot remove the utils from utils.SomeClass

Does anyone know how I can achieve this?

goocreations
  • 2,938
  • 8
  • 37
  • 59
  • Can you try something like `from aaa.bbb import ccc` followed by `from ccc import utils`? Just curious if that gets you anywhere. – Engineero May 23 '18 at 16:28
  • 1
    Or maybe `from aaa.bbb import ccc.utils as utils`? – Engineero May 23 '18 at 16:28
  • What does your `sys.path` look like? Maybe try pre-pending the `/path/to/root/directory` instead of appending -- is it possible that there is another module `aaa.bbb` in the `path` that is getting found before yours, and maybe that one is missing some `__init__.py` files or doesn't have `ccc`? – user May 23 '18 at 16:30
  • `from aaa.bbb import ccc.utils as utils` does not work, gives a Python syntax error – goocreations May 23 '18 at 16:40
  • `from aaa.bbb import ccc` followed by `from ccc import utils` also does not work: `from ccc import utils ImportError: No module named ccc` – goocreations May 23 '18 at 16:42
  • I also tried to `sys.path.insert(0, ...)` but that does give the same problems – goocreations May 23 '18 at 16:43

1 Answers1

0

This really isn't an answer, but there is no way I could fit this into a comment, let alone with any sane formatting. My apology, but it hopefully can still help us get somewhere.

I suspect there is still a bit of information missing somewhere in the question. I've setup the following tree under /tmp/so/xxx:

.
└── aaa
    ├── __init__.py
    └── bbb
        ├── __init__.py
        └── ccc
            ├── __init__.py
            └── utils.py

__init__.py files are blank and utils.py says:

class SomeClass:
    @staticmethod
    def someMethod():
        print("FOO")

Which if I read the description correctly should be enough to replicate the behavior. Now I try both snippets of yours:

import sys
sys.path.append("/tmp/so/xxx")
from aaa.bbb.ccc import utils
utils.SomeClass.someMethod()

And run:

$ python test1.py 
FOO

And second one:

import sys
sys.path.append("/tmp/so/xxx")
from aaa.bbb.ccc.utils import *
SomeClass.someMethod()

Runs as well:

$ python test2.py 
FOO

I've tried Python 3.6.5 and 2.7.15. Same result.

Based on your output, I presume you're running Python 2. The only way I could reproduce this problem (get the same error) was by removing __init__.py from ccc/, but the other syntax would not work either then.

Could you perhaps post your tree and content of __init__.py files? With the available data, I was unfortunately able to reproduce the problem.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39