realpython / book3-exercises

Book 3 Advanced Web Dev with Django 1.7 -- Exercises for the book
89 stars 58 forks source link

virtualenv is not compatible with this system or executable #10

Closed kurtgn closed 9 years ago

kurtgn commented 9 years ago

Hi,

I am now at the migrating to Python 3 stage. I am on Mavericks, and I installed Python 3.4 from https://www.python.org/downloads. When I do mkvirtualenv py3 I get this

Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.4'
New python executable in py3/bin/python3
Also creating executable in py3/bin/python
Failed to import the site module
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/site.py", line 73, in <module>
    __boot()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/site.py", line 2, in __boot
    import sys, imp, os, os.path   
  File "/Users/1111/.virtualenvs/py3/bin/../lib/python3.4/imp.py", line 22, in <module>
    from importlib import util
  File "/Users/1111/.virtualenvs/py3/bin/../lib/python3.4/importlib/util.py", line 12, in <module>
    from contextlib import contextmanager
ImportError: No module named 'contextlib'
ERROR: The executable py3/bin/python3 is not functioning
ERROR: It thinks sys.prefix is '/Users/1111/.virtualenvs' (should be '/Users/1111/.virtualenvs/py3')
ERROR: virtualenv is not compatible with this system or executable

I googled a bit and found python3 -m venv py3 command. This worked. But then, when installing stuff from requirements.txt, I got this

Collecting mock
  Using cached mock-1.0.1.tar.gz
  setuptools must be installed to install from a source distribution

however, I do have setuptools:

(py3) 1111:d3 1111$ ls py3/lib/python3.4/site-packages/
__pycache__         easy_install.py         pip-6.0.8.dist-info     setuptools
_markerlib          pip             pkg_resources           setuptools-12.0.5.dist-info

Please help. Stackoverflow keeps being silent, and I'm stuck. Thanks!

j1z0 commented 9 years ago

There are two separate issues here I think. For the first issue, with mkvirtualenv, it looks like you probably have incompatible version. Try the following and let me know how it works:

mkvirtualenv --python `which python3` env_name

This should cause virtualenv to use python 3.

If that doesn't work, I would say it maybe easier to use homebrew to get your python environment setup correctly. Here is a link to a pretty good guide to do that:

http://hackercodex.com/guide/python-development-environment-on-mac-osx/

Let me know how it goes.

kurtgn commented 9 years ago

Thanks, I tried using both ways but the result is the same: when I try to create virtualenv it still gives the same error:

  File "/Users/1111/.virtualenvs/py3/bin/../lib/python3.4/importlib/util.py", line 12, in <module>
from contextlib import contextmanager
ImportError: No module named 'contextlib'
ERROR: The executable py3/bin/python3 is not functioning

However, I came up with a workaround for this. I opened the Python package and found the utils.py file that was causing trouble, put contextlib.py in the same folder and added this folder to the system path directly from the utils.py:

...
from ._bootstrap import spec_from_file_location
from ._bootstrap import _resolve_name
from ._bootstrap import _find_spec

import os
import sys
sys.path.append(os.path.realpath(os.path.dirname(__file__)))

from contextlib import contextmanager
import functools
import sys
import warnings
...

and this helped create the virtualenv. Hope this won't bite me in the ass somewhere down the road.