Incorrect python import

I am confused by why simple absolute imports do not work. Following the instructions of the Python packages , I have a package with one subpacket:

sound/
    __init__.py
    top.py
    formats/
        __init__.py
        a.py
        b.py

a.py contains:

def foo():
    print("foo")

b.py contains:

from a import foo

def bar():
    foo()

if __name__ == "__main__":
    bar()

top.py contains:

from formats import b    

if __name__ == "__main__":
    b.bar()

Both files are __init__.pyempty. From sound / formats /, runs b prints fooas expected. But from the sound / when starting top, an error appears:

File ".../sound/top.py", line 1, in <module>
  from formats import b
File "...\sound\format\b.py", line 1, in <module>
  from a import foo
ImportError: No module named 'a'

(Note the strange appearance of the slashes in the first line and the backslashes in the second. Python 3.5, Windows 7 Pro.) It doesn't have to be that complicated - what syntax is needed to allow b to be sequentially imported

----- EDIT -----

unittest - , , . Python Project.

+4
1

, .

Python , , . C:\Python35\Lib\site-packages\http\ *, py -3 server.py. . .

Python , !

⚘ python --help | grep -e -m                                     
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
-m mod : run library module as a script (terminates option list)

, script. , !

> cd ../..
> dir
sound
> python -m sound.formats.b
foo
> python -m sound.top
foo

* , , Windows . , -!

+4

Source: https://habr.com/ru/post/1665661/


All Articles