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

后端 未结 4 1886
無奈伤痛
無奈伤痛 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:21

    Just a note for anyone who arrives at this issue, using what Gus E showed in the accept answer and some further experience I've found the following to be very useful to ensure that I can run my programs from the command-line on my machine or on another colleague's should the need arise.

    import sys
    import os
    sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
    

    When I execute the 'main' method, which is located in the 'nested' directory, it ensures that the 'src' directory is added to the PYTHONPATH at the time of execution meaning all following imports will not throw an error.

    Obviously, you need to adjust the number of ".." arguments to the os.path.join() method as determined by the location in your program of where your main method is executed from

提交回复
热议问题