3

I am doing a project on a Macbook, its system is OS X 10.11.5. I used Python 3.5 and had a directory like this

rec-par/
    rec/
        __init__.py
        cle/
            __init__.py
            c.py
        par/
            __init__.py
            b.py
        util/
            __init__.py
            a.py

in a.py there is a import like

from rec.par.b import *

And it got a error like 'No module name rec.par , rec is not a package'. I have added the path 'rec-par' to the sys.path and in cle/c.py there is no error with the same import command.

I tried the methods given in Question How to fix "Attempted relative import in non-package" even with __init__.py, used python3 -m a.py . But it didn't work.

Is there anything I missing here?

Community
  • 1
  • 1
karajan1001
  • 250
  • 2
  • 6
  • Does your top-level folder (or any other place in your import path) contain a `rec.py` file? The exception message seems to imply that it does see something as `rec`, just not the right thing. – Blckknght Jul 15 '16 at 01:00

2 Answers2

5

OK, I had solved this problem. In the directory util/__pycache__ There was a file par.cpython-35.py which had the same name with the directory package. This caused a confusion. After it been deleted , The problem solved.

karajan1001
  • 250
  • 2
  • 6
0

The module in your code is called "rec-par", but when trying to import it you refer to "rec.par".

Domme
  • 141
  • 7
  • 2
    'rec-par/' is the root path of the whole project. I had added it the the PYTHONPATH (I can use from import in other directory for example in 'cle/').And 'rec/' is the sub directory and also a package of the 'rec-par/' , 'par/' is the package of the 'rec/'. 'rec.par' is only namely like the PYTHONPATH rec-par. Thy are two different things – karajan1001 Jul 15 '16 at 01:23