Receiving Import Error: No Module named ***, but has __init__.py

后端 未结 4 1888
無奈伤痛
無奈伤痛 2021-01-02 04:24

I understand that this question has been asked several times but after reading them, and making the suggested fixes, I\'m still stumped.

My project structure is as f

4条回答
  •  臣服心动
    2021-01-02 05:17

    Yet another way to solve this without the path goes like this: consider the following code where inside your folder name 'app' you have 3 files x.py, y.py and an empty init.py. So to run x.py you have an import from y such that:

    x.py

    from app.y import say_hi
    print ("ok x is here")
    say_hi()
    

    And

    y.py

    print ("Im Y")
    def say_hi():
        print ("Y says hi")
    

    so the folder structure would look like this:

    testpy
           app
               __init__.py
               x.py
               y.py
    

    Solution: in the folder BEFORE app do the following:

    $ python -m app.x
    

    Note: I did not use x.py (simply app.x)

    Result:

    Nespresso@adhg MINGW64 ~/Desktop/testpy
    $ python -m app.x
    Im Y
    ok x is here
    Y says hi
    

提交回复
热议问题