-1

I've been working on a python3 application and I ran into a strange problem that picked my curiosity after annoying me greatly.

My file structure is something like this:

root/
 |   __init__.py
 |   main.py
 |   fuzzy/
     |    __init__.py
     |    foo.py
 |   dreamy/
     |    __init__.py
     |    bar.py
     |    meh.py

So I need to use a method of the foo module in both bar and meh.

In bar I wrote:

from fuzzy.foo import foo_function

And that worked perfectly fine.

Now in meh I used the exact same syntax but ended up with an import error:

ModuleNotFoundError: No module named 'fuzzy'

I managed to solve the problem using this method, but I would like to understand why this happens.

According to the Python3 documentation my import syntax is correct:

An alternative way of importing the submodule is:

from sound.effects import echo

This seems to also be the syntax provided by answered question on this forum

Moreover, both bar and meh are in the same directory and their code starts in exactly the same way.

Does anyone know why this error occurs in one file but not in the other ?

Is there something I did wrong ?

If I did do something wrong, what would be the correct way (or good practices) to import local packages / methods ?

Thanks for your insight.

---EDIT---

I did not fiddle with my PYTHONPATH in any way, and I am running Python3 from a (vanilla) Conda venv. For good measures, I also created a new file structure and copied the code in new, blank files. This was to make sure that nothing "funny" would have happened to my directory.

Eric Jin
  • 3,836
  • 4
  • 19
  • 45
Nootaku
  • 217
  • 3
  • 14
  • `import foo` shouldn't work as old-style relative imports are unsupported in Python 3. You most likely did something funny with your current directory or your `PYTHONPATH`. No way for us to know. – jpmc26 May 10 '19 at 04:59
  • @jpmc26 Thanks for the input. However I did not fiddle with my `PYTHONPATH` in any way. I'm running my code on a vanilla Conda ve. Could you please give me a bit more info as to why `from foo import foo` shouldn't work ? According to the [Python3 official documentation](https://docs.python.org/3/tutorial/modules.html#packages) it should still work. – Nootaku May 10 '19 at 09:09
  • Read PEP 328. It talks about removing the functionality your import relies on and the ambiguity it creates. – jpmc26 May 10 '19 at 17:28

1 Answers1

0

This question seems to be terribly simple, however I did notice that other people struggled with it.

Though I found the answer to my question thanks to Baum mit Augen.

Baum mit Augen took the time to write this answer which is very instructive and well written.

So sum my problem up: In development, I tried to call my functions by launching my bar.py from the terminal. This means bar is the main, but located inside a package subdirectory. This causes an Import Error because "its name does not reveal that it is in a package".

I just want to highlight 2 things unrelated to my question:

  1. Considering the post on which I have found this answer this is NOT an easy problem. FYI: I had a quick chat with a Python developer who couldn't find the words to explain this to me.
  2. I find it incredible that those who seek help with valid questions are basically being answered to "RTFM", arch-linux style. I really find it a pity.
Nootaku
  • 217
  • 3
  • 14