Python error “ImportError: No module named”

后端 未结 29 2614
野的像风
野的像风 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 08:14

    I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.

    0 讨论(0)
  • 2020-11-22 08:15

    I solved my own problem, and I will write a summary of the things that were wrong and the solution:

    The file needs to be called exactly __init__.py. If the extension is different such as in my case .py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.

    Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.

    0 讨论(0)
  • 2020-11-22 08:15

    For me, it was something really stupid. I installed the library using pip3 install but was running my program as python program.py as opposed to python3 program.py.

    0 讨论(0)
  • 2020-11-22 08:17

    My two cents:

    enter image description here

    Spit:

    Traceback (most recent call last):
          File "bash\bash.py", line 454, in main
            import bosh
          File "Wrye Bash Launcher.pyw", line 63, in load_module
            mod = imp.load_source(fullname,filename+ext,fp)
          File "bash\bosh.py", line 69, in <module>
            from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
        ImportError: No module named RecordGroups
    

    This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my __init__.py were all there). Well turns out that game/oblivion.py and game/oblivion was confusing python which spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.

    EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.

    0 讨论(0)
  • 2020-11-22 08:17

    In my case, the problem was I was linking to debug python & boost::Python, which requires that the extension be FooLib_d.pyd, not just FooLib.pyd; renaming the file or updating CMakeLists.txt properties fixed the error.

    0 讨论(0)
提交回复
热议问题