Python error “ImportError: No module named”

后端 未结 29 2964
野的像风
野的像风 2020-11-22 07:46

Python is installed in a local directory.

My directory tree looks like this:

(local directory)/site-packages/toolkit/interface.py

29条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 07:53

    I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:

    Booklet
    -> __init__.py
    -> Booklet.py
    -> Question.py
    default
    -> __init_.py
    -> main.py
    

    In 'main.py' I had tried unsuccessfully all the combinations bellow:

    from Booklet import Question
    from Question import Question
    from Booklet.Question import Question
    from Booklet.Question import *
    import Booklet.Question
    # and many othet various combinations ...
    

    The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:

    from booklet.Booklet import Booklet
    from booklet.Question import Question
    from booklet.Question import AnotherClass
    

    From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.

    Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.

提交回复
热议问题