从此刻开始,你的所有工作都是在虚拟环境中的,其允许你使用临时环境来安全地开发。使用此工具链,你可以根据工作所需,设置多个项目并在它们之间切换:
$ mkvirtualenv test1 Using base prefix '/Users/moshe/.pyenv/versions/3.7.3' New python executable in /Users/moshe/.virtualenvs/test1/bin/python3 Also creating executable in /Users/moshe/.virtualenvs/test1/bin/python Installing setuptools, pip, wheel... done. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/preactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/postactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/get_env_details (test1)$ mkvirtualenv test2 Using base prefix '/Users/moshe/.pyenv/versions/3.7.3' New python executable in /Users/moshe/.virtualenvs/test2/bin/python3 Also creating executable in /Users/moshe/.virtualenvs/test2/bin/python Installing setuptools, pip, wheel... done. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/preactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/postactivate virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/get_env_details (test2)$ ls $WORKON_HOME get_env_details postmkvirtualenv premkvirtualenv initialize postrmvirtualenv prermvirtualenv postactivate preactivate test1 postdeactivate predeactivate test2 postmkproject premkproject (test2)$ workon test1 (test1)$
此处,使用 deactivate 命令可以退出当前环境。
推荐实践
你可能已经在比如 ~/src 这样的目录中添加了长期的项目。当要开始了一个新项目时,进入此目录,为此项目增加子文件夹,然后使用强大的 Bash 解释程序自动根据你的目录名来命令虚拟环境。例如,名称为 “pyfun” 的项目:
$ mkdir -p ~/src/pyfun && cd ~/src/pyfun $ mkvirtualenv $(basename $(pwd)) # we will see the environment initialize (pyfun)$ workon pyfun test1 test2 (pyfun)$ deactivate $
(编辑:ASP站长网)
|