1、安裝
pip install virtualenv
pip install virtualenvwrapper
2、配置
# 1、創建目錄用來存放虛擬環境
mkdir $HOME/.virtualenv
# 2、virtualenvwrapper.sh文件的路徑
find / -name virtualenvwrapper.sh
# 參考答案 /usr/local/bin/virtualenvwrapper.sh
# 3、查找Python3的路徑
which python3
# 參考答案 /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
# 4、打開~/.bashrc文件,並添加如下:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
# 5、運行
source ~/.bashrc
3、使用
# 創建虛擬環境 指定python3
mkvirtualenv -p python3 虛擬環境名稱
# 查看虛擬環境
workon
# 進入虛擬環境
workon 虛擬環境名稱
# 刪除虛擬環境
deactivate
重點!!!:
如果你的環境是Python3, 重載以後,可能會出現一下的問題
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
解決辦法:
1、修改 virtualenvwrapper.sh
vim /usr/local/bin/virtualenvwrapper.sh
2、找到 virtualenvwrapper.sh 中如下代碼
# Setup:
#
# 1. Create a directory to hold the virtual environments.
# (mkdir $HOME/.virtualenvs).
# 2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
# to your .bashrc.
# 3. Add a line like "source /path/to/this/file/virtualenvwrapper.sh"
# to your .bashrc.
# 4. Run: source ~/.bashrc
# 5. Run: workon
# 6. A list of environments, empty, is printed.
# 7. Run: mkvirtualenv temp
# 8. Run: workon
# 9. This time, the "temp" environment is included.
# 10. Run: workon temp
# 11. The virtual environment is activated.
#
# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
3、註釋下面加上一行,修改 VIRTUALENVWRAPPER_PYTHON 的默認值
# Locate the global Python where virtualenvwrapper is installed.
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
4、重新加載 virtualenvwrapper.sh
./usr/local/bin/virtualenvwrapper.sh
5、重新加載 .bashrc
source ~/.bashrc