问题
I can't import import modules from the standard library with c extensions. This happened after I upgraded to Ubuntu 14.04 from 12.04. I've tried reinstall python, python-dev, but its not helping. I've noticed other people with similar posts, but they all use virtualenv, whereas I am not using it all.
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle, email, json, readline, socket, turtle
>>> import ctypes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ctypes/__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
ImportError: No module named _ctypes
>>> import io
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: No module named _io
>>> import datetime
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named datetime
回答1:
from which -a python
we see there are two installs of python 2 in /usr/local/lib/python
and /usr/bin/python/
so removing /usr/local/lib/python
will sort the issue.
回答2:
If you are working in virtualenv, it may have broken during upgrade. You can repair it by simply running
virtualenv /PATH/TO/EXISTING/ENVIRONMENT
or
virtualenv --system-site-packages /PATH/TO/EXISTING/ENVIRONMENT
回答3:
For No module named _ctypes Error You can try this:
apt-get install libffi-dev
I hope this helps.
回答4:
Maybe your paths are not correctly setted.
Try looking at:
import sys
sys.path
maybe the python path is not there, and then is not importing the modules.
If there is not there, add the path like a new element in the list.
sys.path.append(new path)
I hope this helps
回答5:
Based on your comment, that which python
returns /usr/local/lib/python
, it looks like you have a local installation of Python which is different than your distribution's installation. Distribution packages are never installed into /usr/local
on Ubuntu. Mixing a custom-installed Python with your distribution supplied libraries (as the /usr/lib
paths in your errors indicate) can lead to a variety of problems.
I would recommend deleting the Python installed in /usr/local/lib
(which is an odd place to install a binary), or removing it from your PATH, so that you can access your distribution installed Python instead.
来源:https://stackoverflow.com/questions/24985807/python-not-importing-correctly-after-upgrade-to-14-04