Python: Submodules Not Found

China☆狼群 提交于 2019-12-25 05:25:09

问题


My Python couldn't figure out the submodules when I was trying to import reportlab.graphics.shapes like this:

>>> from reportlab.graphics.shapes import Drawing

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    from reportlab.graphics.shapes import Drawing
ImportError: No module named shapes

I have copied the reportlab package to /site-packages and I can import module reportlab.graphics successfully.

My Python version is 2.7.3.

Could anyone help me to fix this problem?


回答1:


As @dan-boa pointed out, you can add paths to the module search path, but since you can find the parent module, I doubt that this is your root problem.

Do you have some left-over installation of the module at another path? You can check the path where it is finding the parent package (reportlab) by executing:

import reportlab
print reportlab.__file__

If this is indeed the path you were expecting, then try this recursively with the the sub-modules, until you can see where the problem is. Perhaps, your package is corrupted? Try manually checking in the path returned if you can find the files/modules in question.

If this is not the path you were expecting, clean-up the installation from this 2nd path and try again.

Finally, in case you do find that it is a path problem, instead of adding the path each time using sys.path.append, you can add it to PYTHONPATH




回答2:


Please check your sys path and if the directory of the module is not present then add it.

import sys
sys.path.append('PATH_OF_THE_MODULE')

As site-packages is already their in the sys.path, may be therefore the package was imported successfully.



来源:https://stackoverflow.com/questions/10907460/python-submodules-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!