1

Within a folder somedir I have several directories dir1, dir2 etc. and some files x.py, y.py, z.py etc. In dir1 there are modules a, b, c etc., module a is used in module b (like from a import somethings) with no problem as usual.

But when I try to use any module say, a within module x and use this module x within module z, error occurs.

somedir/
    |dir1/
    |   |__init__.py
    |   |a.py
    |   |b.py # from a import somethings / import a etc. -> ok
    |   |c.py
    |dir2/...
    |.
    |.
    |.
    |x.py # from dir1.a import * or somethings -> error (`ModuleNotFoundError: No module named a`)
    |y.py
    |z.py # from x import * or somethings -> error (`ModuleNotFoundError: No module named a`)

I haven't created any packages neither want to, as I want to use this in an application program. Should I really make a package for this to work. How can I manage such imports. Please help.

ApuCoder
  • 2,795
  • 2
  • 3
  • 19
  • Why don't you want to create packages? – Charles Dassonville Dec 03 '21 at 18:19
  • Does this answer your question? [How to avoid circular imports in Python?](https://stackoverflow.com/questions/7336802/how-to-avoid-circular-imports-in-python) – Mihai Chelaru Dec 03 '21 at 18:20
  • Thank you for your quick response. Ok, I should be more specific, I will update the post soon. – ApuCoder Dec 03 '21 at 18:21
  • @Mihai no, this is not the case here. – ApuCoder Dec 03 '21 at 18:30
  • Have you read through the answers on that question and determined they don't solve your problem? What have you tried? Looks like you are only using the `from x import y` syntax, which answers on that question suggest may cause the problem you're experiencing. There's at least 4 different solutions proposed there, so without mentioning which ones you've tried it's very difficult to see this very generic question as anything more than a duplicate. – Mihai Chelaru Dec 03 '21 at 18:32
  • @Mihai, let's consider this case, I have a central module in `dir1` which I will be using in other modules in the same directory `dir1` to avoid any circular import. But whenever I try to import any module from `dir1` within any one of `x`, `y` or `z` etc. in any way, the problem occurs (says, `ModuleNotFoundError: No module named, say, a). – ApuCoder Dec 03 '21 at 18:45
  • You may also want to look at [this question](https://stackoverflow.com/questions/43728431/relative-imports-modulenotfounderror-no-module-named-x) based on your clarifications. Your initial question was really unclear as to what error you were actually getting and what the chain of import statements is relative to the file structure. In future include an example that also has the relevant code you are running, how you are running the code, and the full traceback of the error you're getting. – Mihai Chelaru Dec 03 '21 at 19:24
  • Either put an empty `__init__.py` file in every directory including `somedir`, or make sure `dir1` is in your PYTHONPATH. – TheEagle Dec 03 '21 at 19:26
  • 1
    @Mihai exactly, that was the same problem as mine, that I failed to find out. I took the suggestion from *Vinod Rane*, that though worked, not quite satisfying. I've to figure out some nice one. Nonetheless, got at least one solution. Thanks for your cooperation. – ApuCoder Dec 03 '21 at 21:52

3 Answers3

0

I suppose dir1 is not on your python path. You can verify this by checking sys.path Try using relative imports in b.py Relative imports for the billionth time

Gábor Pálovics
  • 455
  • 4
  • 12
0

Your file might not running from the same directory as dir1. To check try print(__file__)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 04 '21 at 00:46
0

I got one working solution (not so satisfying though) as suggested from a link above by editing the __init__.py file as:

#__init__.py

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))

Thanks everyone for their support.

ApuCoder
  • 2,795
  • 2
  • 3
  • 19