Python doesn't work: ImportError: No module named 'logging'

家住魔仙堡 提交于 2019-12-02 00:01:14

问题


I'm experiencing a very strange issue on my Digitalocean Ubuntu droplet, where Python 2.7 gives me the following error:

  from instagram import InstagramAuthentication, InstagramAPI
  File "/root/instagram_insights/socialgraphs-final-assignment/instagram.py", line 1, in <module>
    import requests
  File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 55, in <module>
    import urllib3
  File "/usr/lib/python2.7/dist-packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 2, in <module>
    import logging
ImportError: No module named logging

In fact, I can't do anything with Python:

  1. If I'm trying to reinstall python through apt-get I get the same error
  2. If I'm trying to execute a any pip command, e.g. pip search logging, I get the error
  3. I can't even create a new virtualenv by doing virtualenv venv without getting the same error

My PYTHONPATH looks like this: ['', '/usr/local/lib/python2.7/dist-packages/gunicorn-19.3.0-py2.7.egg', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages']

The funny thing is that the error appeared out of the blue until I restarted gunicorn, which is the WSGI server I'm using to host my Flask site.

Before it happened I was fiddling with logs in nginx, but I can't see how that should have affected my Python installation.


回答1:


Make sure you use system-wide python and not virtualenv-from-somewhere:

which python

Try to find out where this module is located:

sudo updatedb
locate logging | grep python

Then try adding this path to PYTHONPATH



来源:https://stackoverflow.com/questions/33945725/python-doesnt-work-importerror-no-module-named-logging

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