0

I have already wasted several hours on this issue and am trapped. Could anyone help me out? Thanks.

I have both Python 2.7.10 and Python 3.4 installed in my Macbook through Homebrew. At the same time, the macbook has its default 2.7.6 which is to be avoided as it does not bundle with pip.

Python 3.4 is symbolick

/usr/local/bin/python3
../../../Library/Frameworks/Python.framework/Versions/3.4/bin/python3

Python 2.7.10 is

/usr/local/bin/python2
/usr/local/Cellar/python/2.7.10_2/bin/python

Now, I have python 2.7 web app to run. Naturally, I need to create a virtual environment by python 2.7.10.

Either

1) create a python 2.7 virtual environment

Steps I researched are:

a. pip install virtualenv

b. virtualenv -p /usr/local/bin/python2 <path/to/new/virtualenv/>

However, in step a, there is error:

~ $/usr/local/Cellar/python/2.7.10_2/bin/pip install virtualenv
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.10_2/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

Or

2) Use python 2.7 in virtual environment created by Python 3.4. Does anyone know how to choose python 2.7 inside the virtual environment created by Python 3.4?

I use this link to create virtual environment in Python 3.4

https://docs.python.org/3/library/venv.html

UPDATE: Thanks for both answers. I resolved the Question 1).

Essentially, you need to add --user when installing the setuptools and virtualenv. For example,

~ $pip install virtualenv==1.5.2 --user

Here is my test results:

     ~ $virtualenv -p /usr/local/bin/python2 test_python2/
    Traceback (most recent call last):
      File "/usr/local/bin/virtualenv", line 5, in <module>
        from pkg_resources import load_entry_point
      File "build/bdist.macosx-10.10-intel/egg/pkg_resources/__init__.py", line 3084, in <module>
      File "build/bdist.macosx-10.10-intel/egg/pkg_resources/__init__.py", line 3070, in _call_aside
      File "build/bdist.macosx-10.10-intel/egg/pkg_resources/__init__.py", line 3097, in _initialize_master_working_set
      File "build/bdist.macosx-10.10-intel/egg/pkg_resources/__init__.py", line 653, in _build_master
      File "build/bdist.macosx-10.10-intel/egg/pkg_resources/__init__.py", line 666, in _build_from_requirements
      File "build/bdist.macosx-10.10-intel/egg/pkg_resources/__init__.py", line 839, in resolve
    pkg_resources.DistributionNotFound: The 'virtualenv==1.5.2' distribution was not found and is required by the application
    ~ $
    ~ $

    ~ $virtualenv -p /usr/local/bin/python2 test_python2/
    Running virtualenv with interpreter /usr/local/bin/python2
    New python executable in test_python2/bin/python2.7
    Also creating executable in test_python2/bin/python
    Installing setuptools..................done.
    ~ $ls test_python2/
    bin include lib
    ~ $
    ~ $cd test_python2/
    ~/test_python2 $source bin/activate
    (test_python2)~/test_python2 $
    (test_python2)~/test_python2 $python
    Python 2.7.10 (default, Jul 13 2015, 12:05:58)
    [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    (test_python2)~/test_python2 $
    (test_python2)~/test_python2 $
day
  • 807
  • 1
  • 9
  • 18
  • I have exactly the same problems as you do. People have recommended conda to me instead of virtualenv. I just started playing with it yesterday, and so far it looks great. It's a combination of package management and virtual environments. http://conda.pydata.org/docs/ – saulspatz Sep 23 '15 at 17:09
  • Thank you! I wasted several hours on this issue. – day Sep 23 '15 at 17:12

2 Answers2

1

In the step b use try using this line:

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

via http://docs.python-guide.org/en/latest/dev/virtualenvs/

Hetdev
  • 1,097
  • 2
  • 15
  • 27
  • mine is essentially the same. I am using another python bin, python2.7.10 – day Sep 23 '15 at 17:18
  • well i guess the questiion is duplicated : http://stackoverflow.com/questions/7446187/no-module-named-pkg-resources – Hetdev Sep 23 '15 at 18:57
0
pip install --upgrade setuptools
pip install --upgrade distribute

Try these two commands first. The error might be coming form your setuptools.

  • Same error. /usr/local/Cellar/python/2.7.10_2/bin/pip install --upgrade setuptools Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.10_2/bin/pip", line 5, in from pkg_resources import load_entry_point ImportError: No module named pkg_resources – day Sep 23 '15 at 18:06
  • That's odd. Have you tried the solution on this thread: http://stackoverflow.com/questions/7446187/no-module-named-pkg-resources ? – Joseph Seung Jae Dollar Sep 23 '15 at 18:52