Python error “ImportError: No module named”

后端 未结 29 2613
野的像风
野的像风 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:51

    Based on your comments to orip's post, I guess this is what happened:

    1. You edited __init__.py on windows.
    2. The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
    3. You used WinSCP to copy the file to your unix box.
    4. WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
    5. The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package.
    6. You create __init__.py in the appropriate directory and everything works... ?
    0 讨论(0)
  • 2020-11-22 07:51
    1. You must have the file __ init__.py in the same directory where it's the file that you are importing.
    2. You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.

    eg: /etc/environment

    PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2

    /opt/folder1/foo

    /opt/folder2/foo

    And, if you are trying to import foo file, python will not know which one you want.

    from foo import ... >>> importerror: no module named foo

    0 讨论(0)
  • 2020-11-22 07:52

    Fixed my issue by writing print (sys.path) and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.

    0 讨论(0)
  • 2020-11-22 07:53

    Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
    Right Click > Mark Directory as > Sources Root

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 07:55

    To all those who still have this issue. I believe Pycharm gets confused with imports. For me, when i write 'from namespace import something', the previous line gets underlined in red, signaling that there is an error, but works. However ''from .namespace import something' doesn't get underlined, but also doesn't work.

    Try

    try:
        from namespace import something 
    except NameError:
        from .namespace import something
    
    0 讨论(0)
提交回复
热议问题