Python error “ImportError: No module named”

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

    On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:

     .:/usr/local/lib/python
    

    (Mind the .: at the beginning, so that it can search on the current directory, too.)

    It may also be in other locations, depending on the version:

     .:/usr/lib/python
     .:/usr/lib/python2.6
     .:/usr/lib/python2.7 and etc.
    
    0 讨论(0)
  • 2020-11-22 07:57

    If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path than your module's.

    To debug, say your from foo.bar import baz complaints ImportError: No module named bar. Changing to import foo; print foo, which will show the path of foo. Is it what you expect?

    If not, Either rename foo or use absolute imports.

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

    In my case, because I'm using PyCharm and PyCharm create a 'venv' for every project in project folder, but it is only a mini env of python. Although you have installed the libraries you need in Python, but in your custom project 'venv', it is not available. This is the real reason of 'ImportError: No module named xxxxxx' occurred in PyCharm. To resolve this issue, you must add libraries to your project custom env by these steps:

    • In PyCharm, from menu 'File'->Settings
    • In Settings dialog, Project: XXXProject->Project Interpreter
    • Click "Add" button, it will show you 'Available Packages' dialog
    • Search your library, click 'Install Package'
    • Then, all you needed package will be installed in you project custom 'venv' folder.

    Enjoy.

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

    This worked for me: Created __init__.py file inside parent folder (in your case, inside site-packages folder). And imported like this:

    from site-packages.toolkit.interface import interface
    

    Hope it will be useful for you as well !

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

    My problem was that I added the directory with the __init__.py file to PYTHONPATH, when actually I needed to add its parent directory.

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

    In my case I was including the path to package.egg folder rather than the actual package underneath. I copied the package to top level and it worked.

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