Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issues with six under django?

Tags:

python

django

I'm trying to use a package called vcrpy to accelerate the execution of my django application test suite. I'm using django 1.7 on Mac, with Python 2.7.

I added the following couple of lines to one of my tests:

import vcr
with vcr.use_cassette('recording.yaml'):

The result is an import error:

    import vcr
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/__init__.py", line 2, in <module>
    from .config import VCR
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/config.py", line 6, in <module>
    from .cassette import Cassette
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/cassette.py", line 12, in <module>
    from .patch import CassettePatcherBuilder
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/patch.py", line 8, in <module>
    from .stubs import VCRHTTPConnection, VCRHTTPSConnection
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vcr/stubs/__init__.py", line 9, in <module>
    from six.moves.http_client import (
ImportError: No module named http_client

The problematic code in the VCR package itself is:

import six
from six.moves.http_client import (
    HTTPConnection,
    HTTPSConnection,
    HTTPMessage,
    HTTPResponse,
)

The funny thing: this code seems to run fine when I'm just running it from a plain python console, but it results in the above ImportError under Django or under the django manage.py shell.

Any idea what might be wrong?

( some additional details about the location of the six module:

When I'm running plain python console, I get the following:

Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29 2014, 21:07:35) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import six
>>> print six.__file__
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.pyc

Doing the same thing, with import django; django.setup(), from manage.py shell results in exactly the same directory and same six.pyc file.

)

like image 746
Roy2012 Avatar asked Dec 26 '14 11:12

Roy2012


Video Answer


1 Answers

Maybe a little bit too late for the original question, but I came here trough Google so for future reference, here's my solution:

Problem

The problem I found is that mac os comes with not only python but also some packages pre-installed. Six is one of those packages and therein lies the conflict. The pre-installed six takes priority over the pip installed six, but pip still gives information based on what it itself has installed (e.g. six 1.9.0 vs 1.4.1).

pre-installed (version 1.4.1):

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/

pip installed (whatever you have installed, for me it was 1.9.0):

/Library/Python/2.7/site-packages/

You can check if this is the case for you by running:

$ python
>>> import six
>>> print six.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py'

Fixing it

The solution is actually quite simple, just put

export PYTHONPATH="/Library/Python/2.7/site-packages:$PYTHONPATH"

in your ~/.bashrc (or whatever file your shell uses). If you've configured your pip to install somewhere else, put that in the pythonpath instead.

like image 176
Allard Stijnman Avatar answered Oct 13 '22 13:10

Allard Stijnman