在虚拟环境中使用Python 2.7.10

时间:2015-09-23 17:00:35

标签: python django macos python-2.7

我已经在这个问题上浪费了几个小时而且被困了。任何人都可以帮我吗?感谢。

我通过Homebrew在我的Macbook中安装了Python 2.7.10和Python 3.4。与此同时,macbook的默认值为2.7.6,因为它没有与pip捆绑在一起。

Python 3.4是符号

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

Python 2.7.10是

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

现在,我有运行python 2.7 web应用程序。当然,我需要通过python 2.7.10创建一个虚拟环境。

要么

1)创建一个python 2.7虚拟环境

我研究的步骤是:

a. pip install virtualenv

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

但是,在步骤a中,出现错误:

~ $/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

2)在Python 3.4创建的虚拟环境中使用python 2.7。有谁知道如何在Python 3.4创建的虚拟环境中选择python 2.7?

我使用此链接在Python 3.4中创建虚拟环境

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

更新 谢谢你的回答。我解决了问题1)。

基本上,您需要在安装setuptools和virtualenv时添加--user。例如,

~ $pip install virtualenv==1.5.2 --user

以下是我的测试结果:

     ~ $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 $

2 个答案:

答案 0 :(得分:0)

在步骤b中,请尝试使用以下行:

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

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

答案 1 :(得分:0)

pip install --upgrade setuptools
pip install --upgrade distribute

首先尝试这两个命令。错误可能来自您的setuptools。