0

I try to upgrade pip within a virtualenv following the instructions here. The upgrade fails because the system python cannot import the name Message from the email module.

(newsfeed)myhost:newsfeed admin$ pip install --upgrade pip
Traceback (most recent call last):
  File "/Users/admin/newsfeed/newsfeed/bin/pip", line 7, in <module>
    from pip import main
  File "/Library/Python/2.7/site-packages/pip/__init__.py", line 9, in <module>
    from pip.log import logger
  File "/Library/Python/2.7/site-packages/pip/log.py", line 8, in <module>
    from pip import backwardcompat
  File "/Library/Python/2.7/site-packages/pip/backwardcompat/__init__.py", line 69, in <module>
    from email import Message as emailmessage
ImportError: cannot import name Message

This problem is not just with upgrading pip but with installing anything within the virtualenv. When new package installations are attempted the same error occurs.

In a python terminal outside the virtualenv Message cannot be imported, but inside the virtualenv it can.

Also, in a different virtualenv on the same machine, there is no error installing or upgrading packages.

Why can't this virtualenv upgrade or install packages?

Community
  • 1
  • 1
ABM
  • 1,628
  • 2
  • 26
  • 42
  • Is there an email.py file somewhere in the path that could be shadowing the standard library's email module. If you do `python -c 'import email;print(email.__file__)'` while in the virtualenv, what is the output? – snakecharmerb Mar 29 '16 at 18:14
  • Thanks. The output of your command is: email/__init__.pyc – ABM Mar 29 '16 at 19:46
  • 1
    If it were the email package from the standard library I'd expect the path in the output would be in `/Library/Python/2.7/site-packages`. Looks like you have a folder named _email_ in the current directory or elsewhere on the path that contains an __ init __ .py file. Rename/move it and see if that fixes your problem. – snakecharmerb Mar 29 '16 at 20:23
  • @snakecharmerb you nailed it. I renamed the directory and it fixed my problem. Add it as an answer and I'll tick it off. – ABM Mar 29 '16 at 21:33

2 Answers2

1

The standard library's email package is being shadowed (hidden) by a package or module named email(.py) in your path.

Locate the package by calling

python -c 'import email;print(email.__file__)'

and rename it or remove it.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
0

Your pip is not inside the venv so you will probably not be able to upgrade it like this.

This is caused by the fact that you created a venv that uses system-packages. Remove the venv and recreate it by adding --no-site-packages as a parameter.

sorin
  • 161,544
  • 178
  • 535
  • 806