mac 安装python虚拟环境,多版本共存

本文介绍了在macOS上安装Python虚拟环境的方法,确保多版本Python共存。通过安装pip并创建指定版本的虚拟环境,使用workon、deactivate等命令进行管理,实现不同项目的独立Python环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mac 安装python虚拟环境,多版本共存


首先,安装pip,python的包管理工具

sudo easy_install pip

安装完pip之后,就要安装 virtualenv

sudo pip install virtualenv

如果是用的macOS 10.11可能会出现以下的提示

我用的是macOS 10.12,没有出现以下的提示

The directory '/Users/xxxx/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/xxxx/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.

可以忽略,活着执行下面的命令

其实他已经在提示的最后建议要加上 -H

sudo -H pip install virtualenv

然后要安装virtualenvwrapper

sudo pip install virtualenvwrapper

我在安装的时候,遇到了这样的错误

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-8.1.2-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_set.py", line 736, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_install.py", line 742, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/utils/__init__.py", line 267, 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-vyEme3-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

执行下面的命令,重新安装

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

安装python3

brew install python3


创建虚拟环境

source /usr/local/bin/virtualenvwrapper.sh

让文件生效,并且要将命令写到 ~.bash_profile 里。

创建虚拟环境,指定python的版本,并将虚拟环境命名为python3

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

创建好虚拟环境之后,会自动进入虚拟环境

退出虚拟环境的命令为 deactivate

虚拟环境的一些命令:

workon   会列出所有的虚拟环境

workon  [name]  会进入指定的虚拟环境

deactivate   退出当前的虚拟环境

mkvirtualenv  [name] 创建虚拟环境

rmvirtualenv  [name] 删除虚拟环境

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值