51

I just fresh installed ubuntu 14.04LTS and i am trying to use pip but i am getting the following traceback:

(nlmanagement)psychok7@Ultrabook:~/code/work/nlmanagement$ pip freeze
Traceback (most recent call last):
  File "/home/psychok7/code/work/venv/nlmanagement/bin/pip", line 9, in <module>
    load_entry_point('pip==1.1', 'console_scripts', 'pip')()
  File "/home/psychok7/code/work/venv/nlmanagement/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 337, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/psychok7/code/work/venv/nlmanagement/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 2279, in load_entry_point
    return ep.load()
  File "/home/psychok7/code/work/venv/nlmanagement/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 1989, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/home/psychok7/code/work/venv/nlmanagement/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 10, in <module>
    from pip.backwardcompat import walk_packages, console_to_str
  File "/home/psychok7/code/work/venv/nlmanagement/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg/pip/backwardcompat.py", line 77, in <module>
    from urllib2 import URLError, HTTPError
  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 32, in <module>
    import io as _io
  File "/usr/lib/python2.7/io.py", line 51, in <module>
    import _io
ImportError: No module named _io

any ideas?? i have tried sudo apt-get install python3-pip and sudo apt-get install python-pip

psychok7
  • 5,373
  • 9
  • 63
  • 101
  • See also http://stackoverflow.com/questions/15608236/eclipse-and-google-app-engine-importerror-no-module-named-sysconfigdata-nd-u – Bryce Sep 18 '14 at 04:36

8 Answers8

56

I had the same problem. This happened in Ubuntu - 14.04 and Virtual environment's Python version - 2.7.3

After spending a day in debugging, posting my answer here, hope it helps for future visitors.

I found that io.py is invoking _io module. I think _io module is internal to the interpreter, so, just replacing the python binary in the environment alone with the newer version should fix.

(In my case, Cloudera Manager 5.x Agent's virtualenv on ubuntu 14.04 was using python interpreter 2.7.3, replaced it with 2.7.6.

echo "Using " && python2.7 --version
mv myenv/bin/python myenv/bin/python.bak;
cp `which python2.7` myenv/bin/python

When a new virtualenv is created, it uses updated python binary from the system. Hence, that explains the accepted answer in the thread.

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
  • 2
    I thought the whole point of virtualenv was to wall off the python and libraries from the OS. Why would the system python change affect anything? – Bryce Sep 18 '14 at 04:15
  • After trying this I get "ImportError: No module named _sysconfigdata_nd", due to https://bugs.launchpad.net/ubuntu/+source/python2.7/+bug/1115466 – Bryce Sep 18 '14 at 04:53
  • That is a great question. Though the virtualenv wall off the python stuff, the `_io` issue is with the native libraries of interpreter itself which are plausibly not isolated, shared from system. I think, in ubuntu 14.04 some of system libraries are restructured, so the older python(2.7.3 - which with ubuntu12.04) did not work. When I checked with the newer python that came along with ubuntu14.04(just be dropping into console and do `import io`) it worked. – Thamme Gowda Sep 24 '14 at 15:04
  • 1
    I had to remove `myenv/bin/python` before copy. ```rm myenv/bin/python;cp `which python` myenv/bin/python```. Thanks! – iurisilvio Sep 26 '14 at 14:52
  • 2
    This is the only answer on this page that actually worked for me (tried them all). This upgraded the python interpreter from python 2.7.3 in the virtual-env to 2.7.6. I'm not using Cloudera manager BTW, just a vanilla Ubuntu 14.04 where the virtual env originated in 12.04 (and git cloned on 14.04). Thanks! – arielf Jun 29 '15 at 21:44
  • Your solution is easy and worked for me (but not the others provided). Thanks a lot. – Eugène Adell Mar 07 '20 at 17:52
50

Your virtualenv became corrupt due to diffs in the Python system lib.

Best choice is to reinstall your virtualenv:

$ deactivate
$ rmvirtualenv nlmanagement
$ mkvirtualenv nlmanagement
$ pip install -r requirements.txt
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • why do i have these diffs? because of the upgrade? – psychok7 Apr 19 '14 at 23:31
  • @psychok7 yep, the system upgrade probably replaced some libraries. – Yuval Adam Apr 20 '14 at 11:32
  • Bingo! I didn't even think of the virtualenv getting stale from the upgrade. – Esteban Aug 20 '14 at 17:22
  • 5
    It tells me that `rmvirtualenv: command not found`. Do I need to be in a specific directory or something? Also, what/where is `requirements.txt`? – Roman Aug 22 '14 at 08:18
  • 5
    Sure, but this is a big hammer, when just copying the new 2.7 binary to the existing env is sufficient (see http://stackoverflow.com/a/24191303/143804). – ire_and_curses Aug 29 '14 at 02:06
  • @ire_and_curses I prefer to have a proper clean virtualenv from scratch. Why is this a big hammer? Nothing is lost. – Yuval Adam Aug 29 '14 at 14:52
  • 2
    That depends on how expensive your virtualenv is to recreate. In my specific case, the installation is only semi-automated through requirements.txt. There is a manual licensing step, and a manual configuration step. Also, I depend on scipy, among other things. Building scipy from scratch takes many minutes. – ire_and_curses Aug 29 '14 at 15:51
  • Just came across this answer, and thought I'd add that your suggestion depends on the mkvirtualenv scripts being installed, and that you're suggesting someone destroy their virtualenv before freezing their requirements file. – Luke Tully Sep 22 '18 at 07:42
21

You don't need to delete the virtual environment.

Just follow these steps.

  1. Let's say your virtual environment name is "mydev" and you are using virtualenvwrapper, do

    cd $WORKON_HOME
    virtualenv mydev
    

    The above command just upgrades the link to the python executable without losing any package. Without virtualenvwrapper, $WORKON_HOME might not be defined; then you need to change to the parent directory of the virtualenv.

  2. outside virtualenv, if your pip is not working, then just

    easy_install pip
    

    will resolve the issue.

user1248490
  • 963
  • 9
  • 16
Gomes
  • 3,330
  • 25
  • 17
  • 2
    Trying #1 I get 'OSError: Command python -c "#!python \"\"\"Bootstra...sys.argv[1:])' – Bryce Sep 18 '14 at 04:31
  • 1
    pip was not working. I eventually solved it with a symlink per https://bugs.launchpad.net/ubuntu/+source/python2.7/+bug/1115466 – Bryce Sep 18 '14 at 21:16
3

After upgrading my ubuntu server to 14.04, I also have this kind of problem while executing pip

ImportError: No module named _io

Then I found out simply reinstall virtualenv can solve this issue!

sudo pip install virtualenv --upgrade

kakashi
  • 317
  • 1
  • 8
2

One cause of this is a bad "pip". See: https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1373254

And/or it can be a problem with virtualenv: https://bugs.launchpad.net/ubuntu/+source/python2.7/+bug/1115466 Where the recommended workaround is:

  cd /usr/lib/python2.7
  ln -s plat-x86_64-linux-gnu/_sysconfigdata_nd.py .
Bryce
  • 8,313
  • 6
  • 55
  • 73
0

You have to replace the python version in your corrupted virtualenv.

Do this:

  1. Create a new Virtualenv

    mkvirtualenv MyNewTestEnv
    
  2. Locate "python" (in my case the virtualenvs are located in the home directory)

    cd ~/.virtualenvs/MyNewTestEnv/bin
    
  3. Copy "python" from the new virtualenv into your corrupted virtualenv

    cp ~/.virtualenvs/MyNewTestEnv/bin/python ~/.virtualenvs/<yourCorruptedEnv>/bin
    

That's it. Now your CorruptedEnv should be fixed.

dh1tw
  • 1,411
  • 2
  • 23
  • 29
0

If you have installed different python versions. You must reference your python

virtualenv -p /usr/bin/python2.7 myenv

or

virtualenv -p /usr/bin/python3.x myenv
mcolak
  • 609
  • 1
  • 7
  • 13
0

This workaround applies to SuSE 12.2 Downloaded python and installed it with ./configure and make install to defult /usr/local folder

Installed pip and virtualenv

When trying to create python virtual env

*linux-t69c:/nkadic # virtualenv -p python venv2*
**ImportError: No module named _struct**
or (if manually setting PYTHONPATH)
**ImportError: No module named _io**

Here is the workaround:

Check default sys.path python (do not change default PYTHONPATH)

check if there are libs dir /usr/local/lib/python2.7/lib-dynload exists

if not, create symlink to existing lib64 path:

linux-t69c:/nkadic # ln -s /usr/local/lib64/python2.7/lib-dynload/  
/usr/local/lib/python2.7/lib-dynload
linux-t69c:/nkadic # ls -ltr /usr/local/lib/python2.7/lib-dynload
lrwxrwxrwx 1 root root 39 Aug 12 21:06 /usr/local/lib/python2.7/lib-dynload -> 
/usr/local/lib64/python2.7/lib-dynload/

After this workaround "virtualenv -p python venv2" works

linux-t69c:/nkadic # virtualenv -p python venv2
Already using interpreter /usr/local/bin/python
PYTHONHOME is set.  You *must* activate the virtualenv before using it
New python executable in /nkadic/venv2/bin/python
Installing setuptools, pip, wheel...done.
linux-t69c:/nkadic # 
nkadic
  • 1