我的Mac上有太多的pythons,并且pip install不是安装包进入正确的python包文件夹

时间:2017-06-06 22:05:03

标签: python macos python-2.7 pip virtualenv

我想在我的Mac上使用virtualenv,我用pip安装它:

pip install virtualenv

/usr/local/lib/python2.7/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv

显然,virtualenv没有安装到正确的Python包文件夹中。这是错误:

virtualenv -p python3 --system-site-packages py3

Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application

然后我想我的Mac上有太多的Pythons:

sudo find / -type d -name "*site-packages*"

find: /dev/fd/user: No such file or directory
find: /dev/fd/user: No such file or directory
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
/Library/Python/2.6/site-packages
/Library/Python/2.7/site-packages
/Library/Python/2.7/site-packages/flask/testsuite/test_apps/lib/python2.5/site-packages
/opt/X11/lib/python2.6/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-Nexe_V-uninstall/Library/Python/2.7/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-PpXcxM-uninstall/Library/Python/2.7/site-packages
/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-ROUjc9-uninstall/Library/Python/2.7/site-packages
/Users/user/Downloads/virtualenv-15.0.3/py3/lib/python3.6/site-packages
/Users/user/Library/Enthought/Canopy_64bit/factory-reset-backup/System/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/factory-reset-backup/User/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/System/lib/python2.7/site-packages
/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
/Users/user/Library/Python/2.7/lib/python/site-packages
/Users/user/Library/Python/3.6/lib/python/site-packages
/Users/user/py3/lib/python3.6/site-packages
/Users/user/venv/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.11.1/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.11.1/libexec/nose/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/lib/python3.6/site-packages
/usr/local/Cellar/numpy/1.12.1/libexec/nose/lib/python2.7/site-packages
/usr/local/Cellar/numpy/1.12.1/libexec/nose/lib/python3.6/site-packages
/usr/local/git/lib/python2.7/site-packages
/usr/local/lib/python2.7/site-packages
/usr/local/lib/python3.6/site-packages

我当前的python指向/usr/local/bin/python,我的pip指向/usr/local/bin/pip。这就是默认点内的内容:

cat /usr/local/bin/pip

#!/usr/local/opt/python/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
__requires__ = 'pip==9.0.1'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
    )

我的virtualenv可执行文件是这样的:

cat /usr/local/bin/virtualenv

#!/usr/local/opt/python/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'virtualenv==15.1.0','console_scripts','virtualenv'
__requires__ = 'virtualenv==15.1.0'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('virtualenv==15.1.0', 'console_scripts', 'virtualenv')()
    )

如何清理我的python安装并将pip和virtualenv安装指向当前的python包文件夹?

BTW,没有pass pip install-option参数,pip install不会将软件包安装到当前默认的python包文件夹。

pip install --install-option="--install-purelib=/python/packages" virtualenv

即使在使用pip3之后,我的python仍然有些奇怪:

$ pip3 install virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv



virtualenv -p python3 --system-site-packages py3
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application




pip3 install --install-option="--install-purelib=/python/packages" virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Collecting virtualenv
  Using cached virtualenv-15.1.0.tar.gz
Skipping bdist_wheel for virtualenv, due to binaries being disabled for it.
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... error
    Complete output from command /usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-y9p6_ro7-record/install-record.txt --single-version-externally-managed --compile --install-purelib=/Library/Python/2.7/site-packages --install-purelib=/python/packages:
    running install
    running build
    running build_py
    creating build
    creating build/lib
    copying virtualenv.py -> build/lib
    creating build/lib/virtualenv_support
    copying virtualenv_support/__init__.py -> build/lib/virtualenv_support
    copying virtualenv_support/argparse-1.4.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/pip-9.0.1-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/setuptools-28.8.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    copying virtualenv_support/wheel-0.29.0-py2.py3-none-any.whl -> build/lib/virtualenv_support
    running install_lib
    byte-compiling /python/packages/virtualenv.py to virtualenv.cpython-36.pyc
    error: [Errno 13] Permission denied: '/python/packages/__pycache__'

    ----------------------------------------
Command "/usr/local/opt/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-y9p6_ro7-record/install-record.txt --single-version-externally-managed --compile --install-purelib=/Library/Python/2.7/site-packages --install-purelib=/python/packages" failed with error code 1 in /private/var/folders/17/y6xv651n50j6gplk57ythm8m0000gn/T/pip-build-mvjkq8ur/virtualenv/



sudo pip3 install --install-option="--install-purelib=/python/packages" virtualenv
/usr/local/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
The directory '/Users/yong/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/yong/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
  Downloading virtualenv-15.1.0.tar.gz (1.9MB)
    100% |████████████████████████████████| 1.9MB 824kB/s
Installing collected packages: virtualenv
  Running setup.py install for virtualenv ... done
Successfully installed virtualenv



virtualenv -p python3 --system-site-packages py3
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 849, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'virtualenv==15.1.0' distribution was not found and is required by the application



ls /usr/local/lib/python3.6/site-packages/
__pycache__                      pip                              pkg_resources                    setuptools-32.2.0-py3.6.egg-info wheel
easy_install.py                  pip-9.0.1-py3.6.egg-info         setuptools                       sitecustomize.py                 wheel-0.29.0-py3.6.egg-info



ls /Library/Python/2.7/site-packages
Flask_MongoRest-0.1.1-py2.7.egg      colorclass-2.2.0-py2.7.egg-info      idna                                 pyparsing-2.2.0-py2.7.egg-info       terminaltables-3.1.0-py2.7.egg-info
Flask_Views-0.2.1-py2.7.egg          colorclass-2.2.0-py3.6.egg-info      idna-2.5-py2.7.egg-info              pyparsing-2.2.0-py3.6.egg-info       terminaltables-3.1.0-py3.6.egg-info
Flask_WTF-0.9.5-py2.7.egg            coverage-3.7.1-py2.7.egg             idna-2.5-py3.6.egg-info              pyparsing.py                         textblob
IPython                              dateutil                             jinja2                               pyparsing.pyc                        twisted
Pygments-1.6-py2.7.egg               django                               lxml                                 python_dateutil-2.6.0-py2.7.egg-info urllib3
WTForms-1.0.5-py2.7.egg              docopt-0.6.2-py2.7.egg-info          mongoengine                          queuelib                             urllib3-1.21.1-py2.7.egg-info
__pycache__                          docopt-0.6.2-py3.6.egg-info          mysql-0.0.1-py2.7.egg-info           requests                             urllib3-1.21.1-py3.6.egg-info
awscli-1.9.12-py2.7.egg              docopt.py                            nameparser                           requests-2.17.3-py2.7.egg-info       virtualenv-15.1.0-py2.7.egg-info
boto                                 docopt.pyc                           nltk                                 requests-2.17.3-py3.6.egg-info       virtualenv-15.1.0-py3.6.egg-info
bs4                                  docutils                             nose                                 scrapy                               virtualenv.py
cerberus                             eve                                  nose-1.3.1-py2.7.egg                 simplejson                           virtualenv.pyc
certifi                              events                               packaging                            six-1.10.0-py2.7.egg-info            virtualenv_support
certifi-2017.4.17-py2.7.egg-info     flask                                packaging-16.8-py2.7.egg-info        six-1.10.0-py3.6.egg-info            werkzeug
certifi-2017.4.17-py3.6.egg-info     flask_cuddlyrest                     packaging-16.8-py3.6.egg-info        six.py                               wtforms
chardet                              flask_mongoengine                    pandas                               six.pyc                              zmq
chardet-3.0.3-py2.7.egg-info         flask_mongoengine-0.7.0-py2.7.egg    pattern                              sklearn
chardet-3.0.3-py3.6.egg-info         flask_restful                        pip_upgrader                         sphinx
cleancat-0.3-py2.7.egg               flask_views                          pip_upgrader-1.4.0-py2.7.egg-info    tablib
colorclass                           flask_wtf                            pip_upgrader-1.4.0-py3.6.egg-info    terminaltables

我不得不将文件复制到python 3.6以使其正常工作。

cp -R /Library/Python/2.7/site-packages/virtualenv-15.1.0-py3.6.egg-info /usr/local/lib/python3.6/site-packages/
cp -R /Library/Python/2.7/site-packages/virtualenv.pyc /usr/local/lib/python3.6/site-packages/
cp -R /Library/Python/2.7/site-packages/virtualenv.py /usr/local/lib/python3.6/site-packages/

1 个答案:

答案 0 :(得分:3)

您正在python2.7上安装软件包,但随后在python3上检查其可用性!!

当您安装多个版本的python时,通常它也会附带它自己的pip版本。 例如python3也将安装pip3。 在下面的目录中查找它,所以: /Library/Frameworks/Python.framework/Versions/3.4/bin/pip3.4

在我的机器上,我还有一个pip3,它指向我机器上安装的最新版本的python3(在我的例子中是python3.4)。

然后你只需:

pip3 install <yourpackage>