简体   繁体   中英

Can't import modules in python without sudo - ubuntu 12.04

I feel like this is a new issue since I upgraded to ubuntu 12.04.

Now, if I enter the interactive shell and import a standard module like urllib2 , I get the following error:

$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
   import httplib
  File "/usr/lib/python2.7/httplib.py", line 79, in <module>
   import mimetools
  File "/usr/lib/python2.7/mimetools.py", line 6, in <module>
   import tempfile
  File "/usr/lib/python2.7/tempfile.py", line 34, in <module>
   from random import Random as _Random
  File "/usr/lib/python2.7/random.py", line 47, in <module>
   from os import urandom as _urandom
  ImportError: cannot import name urandom
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in     apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
    from apport.report import Report
  File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
    import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
  File "/usr/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
    ImportError: cannot import name urandom

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
    import httplib
  File "/usr/lib/python2.7/httplib.py", line 79, in <module>
    import mimetools
  File "/usr/lib/python2.7/mimetools.py", line 6, in <module>
    import tempfile
  File "/usr/lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
   File "/usr/lib/python2.7/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
>>>

If I run sudo before entering the interactive shell, everything is fine, it imports without throwing a fit.

I don't want to just chmod 755 my entire /usr/lib/python2.7 directory, so what can I do to make things run more smoothly in a more sensible way?

Use a Virtual Machine. That will give you a clean environment unhampered by past installs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM