前言:繼續安裝中,這節記錄 mac 安裝 python 虛擬環境,多版本共存...
1. 安裝 pip -- python的包管理工具:
sudo easy_install pip
安裝成功,出現下面:
2. 安裝完pip之后,就要安裝 virtualenv:
sudo pip install virtualenv # 卸載安裝:sudo pip uninstall virtualenv
據說如果是用的macOS 10.11可能會出現以下的提示(我用的是macOS 10.13,也出現以下的提示):
1 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. 2 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
3. 然后要安裝virtualenvwrapper:
Virtaulenvwrapper 是 virtualenv 的擴展包,可以更方便地新增,刪除,復制,切換虛擬環境。
sudo -H pip install virtualenvwrapper
安裝成功如下:
據說,有人在安裝的時候,遇到了這樣的錯誤(我未遇到,未驗證):
1 Installing collected packages: six 2 Found existing installation: six 1.4.1 3 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. 4 Uninstalling six-1.4.1: 5 Exception: 6 Traceback (most recent call last): 7 File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/basecommand.py", line 215, in main 8 status = self.run(options, args) 9 File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/commands/install.py", line 317, in run 10 prefix=options.prefix_path, 11 File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_set.py", line 736, in install 12 requirement.uninstall(auto_confirm=True) 13 File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_install.py", line 742, in uninstall 14 paths_to_remove.remove(auto_confirm) 15 File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove 16 renames(path, new_path) 17 File "/Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg/pip/utils/__init__.py", line 267, in renames 18 shutil.move(old, new) 19 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move 20 copy2(src, real_dst) 21 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2 22 copystat(src, dst) 23 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat 24 os.chflags(dst, st.st_flags) 25 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'
執行下面的命令,重新安裝:
1 sudo pip install pbr 2 sudo pip install --no-deps stevedore 3 sudo pip install --no-deps virtualenvwrapper
4. 創建虛擬環境:
source /usr/local/bin/virtualenvwrapper.sh
5. 讓文件生效,並且要將命令寫到 ~.bash_profile 里。
創建虛擬環境,指定python的版本,並將虛擬環境命名為python3
mkvirtualenv --python=/usr/local/bin/python3 python3
6. 創建好虛擬環境之后,會自動進入虛擬環境
退出虛擬環境的命令為 deactivate
虛擬環境的一些命令:
workon 會列出所有的虛擬環境
workon [name] 會進入指定的虛擬環境
deactivate 退出當前的虛擬環境
mkvirtualenv [name] 創建虛擬環境
rmvirtualenv [name] 刪除虛擬環境
參考:https://blog.csdn.net/blog_user_zk/article/details/72844452