每次我打開終端我要運行這個命令的workon命令如何編輯.bashrc中使用virtualenvwrapper
source $(which virtualenvwrapper.sh)
要使用
workon myenv
我想知道我需要什麼添加到.bashrc中,所以我可以使用workon
命令瞬間, 不使用source
前
我使用Ubuntu 14.04
每次我打開終端我要運行這個命令的workon命令如何編輯.bashrc中使用virtualenvwrapper
source $(which virtualenvwrapper.sh)
要使用
workon myenv
我想知道我需要什麼添加到.bashrc中,所以我可以使用workon
命令瞬間, 不使用source
前
我使用Ubuntu 14.04
按照該virtualenvwrapper docs,你可以添加以下.bashrc
:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
我個人非常喜歡,因爲它使shell啓動速度快lazy loading option。
我在.bash_profile中有這個功能來幫助在虛擬環境之間移動。它包含了不必每次都需要獲取shell腳本的信息,但它還包括一種在您進入目錄時自動「運行」環境的方法。
我不確定如果你只有一個,我會看到virtualenvs的觀點。
export WORKON_HOME=~/.virtualenvs
export PROJECT_HOME=~/Development/python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
# Call virtualenvwrapper's "workon" if .venv exists.
# Source: https://gist.github.com/clneagu/7990272
# This is modified from--
# https://gist.github.com/cjerdonek/7583644, modified from
# http://justinlilly.com/python/virtualenv_wrapper_helper.html, linked from
# http://virtualenvwrapper.readthedocs.org/en/latest/tips.html
#automatically-run-workon-when-entering-a-directory
check_virtualenv() {
if [ -e .venv ]; then
env=`cat .venv`
if [ "$env" != "${VIRTUAL_ENV##*/}" ]; then
echo "Found .venv in directory. Calling: workon ${env}"
workon $env
fi
fi
}
venv_cd() {
builtin cd "[email protected]" && check_virtualenv; ls -FGlAhp
}
# Call check_virtualenv in case opening directly into a directory (e.g
# when opening a new tab in Terminal.app).
check_virtualenv
alias cd="venv_cd"
我在.bashrc
文件自動workon
最近啓動的虛擬環境中使用此。
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
source /usr/local/bin/virtualenvwrapper.sh
# Set up hooks to automatically enter last virtual env
export LAST_VENV_FILE=${WORKON_HOME}/.last_virtual_env
echo -e "#!/bin/bash\necho \$1 > $LAST_VENV_FILE" > $WORKON_HOME/preactivate
echo -e "#!/bin/bash\necho '' > $LAST_VENV_FILE" > $WORKON_HOME/predeactivate
chmod +x $WORKON_HOME/preactivate
chmod +x $WORKON_HOME/predeactivate
if [ -f $LAST_VENV_FILE ]; then
LAST_VENV=$(tail -n 1 $LAST_VENV_FILE)
if [ ! -z $LAST_VENV ]; then
# Automatically re-enter virtual environment
workon $LAST_VENV
fi
fi
fi
的preactivate
和deactivate
掛鉤修改,以便在虛擬環境中被激活,虛擬環境的名稱將被轉儲到一個文件中,當它禁用了文件的內容被刪除。這與@Todd Vanyo的答案類似,但是在激活/停用而不是在導航目錄時起作用。
N.b. '.sh'文件[有時會放到其他地方](https://stackoverflow.com/a/16705807/1461850)(例如使用'pip install') – atomh33ls 2017-09-13 22:11:47