Python is installed in a local directory.
My directory tree looks like this:
(local directory)/site-packages/toolkit/interface.py
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.
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.
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:
Enjoy.
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 !
My problem was that I added the directory with the __init__.py
file to PYTHONPATH, when actually I needed to add its parent directory.
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.