1

I have a custom Python library ("common") that is being imported and used from several Python projects.
That central library has the following structure:

/common
  /__init__.py
  /wrapper.py
  /util
    /__init__.py
    /misc.py

Our custom library resides in a central place /data/Development/Python, so in my Python projects I have an .env file in order to include our lib:

PYTHONPATH="/data/Development/Python"

That works fine, I can for example do something like:

from common.util import misc

However, now I want to make use of a class MyClass within common/wrapper.py from the code in common/util/misc.py. Thus I tried the following import in misc.py:

from ..wrapper import MyClass

But that leads to the following error:

Exception has occurred: ImportError
cannot import name 'MyClass'

Any ideas what I am doing wrong here?

PS: When I do an from .. import wrapper instead and then from code I use wrapper.MyClass, then it works fine. Does that make any sense?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • I encountered the same problem too before. I don't know why that happens, but I solve by "from common.wrapper import MyClass" – Mayan Oct 28 '19 at 10:28
  • Can you do me a favour? Can you please go into your root dir and run `tree`? you can `apt-get install tree` – DUDANF Oct 28 '19 at 10:59
  • I answered this [here](https://stackoverflow.com/a/58084390/11317776) – DUDANF Oct 28 '19 at 11:02

2 Answers2

1

It's finding wrapper, otherwise you'd get a different error:

>>> from wibble import myclass
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'wibble'

So it seems wrapper does not contain MyClass. Typo?

michjnich
  • 2,796
  • 3
  • 15
  • 31
  • Nope, I copy&paste'd that class name, just double-checked it again. Also I just tried to do it via `from .. import wrapper`and then in code I use `wrapper.MyClass`, and that approach works. I updated the OP accordingly. – Matthias Oct 28 '19 at 10:52
  • Personally I avoid relative imports anyway, but your situation is very odd. Your change backs up my assertion that it's not the `wrapper.py` that's not being found, but the `MyClass` class. If it works according to your edit then I think I'm out of helpful suggestions I'm afraid. BTW, no - to answer the question in your edit - that makes no sense to me :) – michjnich Oct 28 '19 at 10:56
0

"However, now I want to make use of a class MyClass within common/wrapper.py from the code in common/util/misc.py. Thus I tried the following import in misc.py:"

If you do export PYTHONPATH=~/common

In order to import MyClass you will do:

from wrapper import MyClass

Because you have added common to your root folder, utils is already in your path since it is in you root folder.

Similarly, if you wanted to import from wrapper you would do from utils.misc import ThisClass since utils.misc is already in the root folder common

DUDANF
  • 2,618
  • 1
  • 12
  • 42
  • I don't get that. As stated I already have set my `PYTHONPATH` via the `.env` file. So importing "common.*` does work fine. I tried it your way (`from wrapper import MyClass`) but still get the same error. However, doing an `from .. import wrapper` works fine. – Matthias Oct 28 '19 at 11:20
  • um. maybe using "wrapper" isn't the best since it seems like it could be a keyword? It also depends on where you're calling this from. If `..` works that means it's two levels deep. But from my perspective the path is `common/wrapper` so it shouldn't be two levels at all. – DUDANF Oct 28 '19 at 11:23