在OSX 10.11.1上使用pip安装virtualenv和virtualenvwrapper

4

目标

安装virtualenv和virtualenvwrapper

背景

我正在尝试按照这篇教程https://realpython.com/blog/python/flask-by-example-part-1-project-setup/创建一个示例flask应用程序。

但是,由于我正在新的Mac上设置Python和相关技术,因此我经常遇到问题。

我卡在输入以下命令的地方:

$ mkvirtualenv --python=/usr/local/bin/python3 wordcounts

后来我意识到我没有安装python 3。但我仍然有错误:

-bash: mkvirtualenv: command not found

我找到了一篇教程,来帮助安装virtualenv 在Mac OS X上安装virtualenv和virtualenvwrapper,它说要运行以下命令:

pip install virtualenv virtualenvwrapper

但是我遇到了pip的问题

当前状态

我认为我在Can't install virtualenvwrapper on OSX 10.11 El Capitan上找到了一个现有的答案

答案说要执行以下操作:

sudo pip install pbr
sudo pip install --no-deps stevedore
sudo pip install --no-deps virtualenvwrapper

但是在评论中,另一个用户提到这个答案在10.11.1上不起作用。

日志

Captains-MacBook-Pro:flask-by-example mac$ sudo pip install pbr
Password:
The directory '/Users/mac/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/mac/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.
Requirement already satisfied (use --upgrade to upgrade): pbr in /Library/Python/2.7/site-packages
Captains-MacBook-Pro:flask-by-example mac$ sudo pip install --no-deps stevedore
The directory '/Users/mac/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/mac/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 stevedore
  Downloading stevedore-1.9.0-py2.py3-none-any.whl
Installing collected packages: stevedore
Successfully installed stevedore-1.9.0
Captains-MacBook-Pro:flask-by-example mac$ sudo pip install --no-deps virtualenvwrapper
The directory '/Users/mac/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/mac/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 virtualenvwrapper
  Downloading virtualenvwrapper-4.7.1-py2.py3-none-any.whl
Installing collected packages: virtualenvwrapper
Successfully installed virtualenvwrapper-4.7.1
Captains-MacBook-Pro:flask-by-example mac$ sudo pip install virtualenv virtualenvwrapper
The directory '/Users/mac/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/mac/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.
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): virtualenvwrapper in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): virtualenv-clone in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): stevedore in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): pbr>=1.6 in /Library/Python/2.7/site-packages (from stevedore->virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): argparse in /Library/Python/2.7/site-packages (from stevedore->virtualenvwrapper)
Collecting six>=1.9.0 (from stevedore->virtualenvwrapper)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six
  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/basecommand.py", line 211, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/commands/install.py", line 311, in run
    root=options.root_path,
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_set.py", line 640, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_install.py", line 716, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/req/req_uninstall.py", line 125, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/utils/__init__.py", line 315, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
    copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
    os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-tK4FXO-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
Captains-MacBook-Pro:flask-by-example mac$
1个回答

7

您不应该使用系统自带的Python版本。

根据Python文档

随OS X提供的Python版本适合学习,但不适合开发。

根据苹果文档

鼓励使用Perl、Python、Ruby或任何其他随OS X提供的脚本语言的开发人员管理其自己的语言和依赖项安装...

因此,我建议按照Python OS X安装文档中的建议和说明使用homebrew安装Python 2和/或Python 3。

请注意,这些说明提供了安装Python 2的命令。要安装Python 3,您需要运行:

brew install python3    # instead of 'brew install python'

这些Python安装文档还链接到详细的安装virtualenvvirtualenvwrapper的说明。 更新: 看起来你已经安装了virtualenvwrapper,但没有正确配置。当我安装它时,我根据virtualenvwrapper的安装文档运行了以下代码进行配置:
# Create a directory to hold the virtual environments.
mkdir $HOME/.virtualenvs

echo "
# Python's virtualenvwrapper-RELATED
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh 
" >> ~/.bash_profile
source ~/.bash_profile

好的,那帮了大忙。我现在也在用Python 3了。我猜新的问题是,当我运行命令mkvirtualenv --python=/usr/local/bin/python3 wordcounts时,出现了一个错误-bash: mkvirtualenv: command not found,然后我在GitHub上找到了这个链接https://github.com/robbyrussell/oh-my-zsh/issues/3047,但那是给使用zsh的人的。我确定我肯定漏掉了一些明显的东西,但不确定是什么。 - JGallardo
1
你有一个名为/usr/local/bin/virtualenvwrapper.sh的文件吗? - Mike Covington
迈克,是的,我有。我刚刚检查过了。 - JGallardo
看起来你已经安装了 virtualenvwrapper,但是没有正确配置。我更新了我的答案,并为您提供了更多的尝试方法。 - Mike Covington

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接