How to leave/exit/deactivate a Python virtualenv
我使用的是virtualenv和virtualenvwrapper。我可以使用
1 2 3 4 | me@mymachine:~$ workon env1 (env1)me@mymachine:~$ workon env2 (env2)me@mymachine:~$ workon env1 (env1)me@mymachine:~$ |
但是,如何退出所有虚拟机并再次在真正的机器上工作?现在,我唯一能回到
1 | me@mymachine:~$ |
是退出shell并开始新的shell。这有点烦人。是否有一个命令来处理"无",如果有,它是什么?如果这样的命令不存在,我将如何创建它?
通常,激活virtualenv会为您提供一个名为:
1 | $ deactivate |
使事情恢复正常。
编辑1我刚刚又专门研究了
如果您试图离开一个Python环境,过程会有点不同:运行两个单词的命令
1 2 3 4 | bash-4.3$ deactivate pyenv-virtualenv: deactivate must be sourced. Run 'source deactivate' instead of 'deactivate' bash-4.3$ source deactivate pyenv-virtualenv: no virtualenv has been activated. |
我将别名workoff定义为与workon相反的:
1 | alias workoff='deactivate' |
易于记忆:
1 2 3 | [bobstein@host ~]$ workon django_project (django_project)[bobstein@host ~]$ workoff [bobstein@host ~]$ |
1 | $ deactivate |
如果这不起作用,试试看
1 | $ source deactivate |
任何知道bash
要激活python虚拟环境:
1 2 | $cd ~/python-venv/ $./bin/activate |
停用:
1 | $deactivate |
使用
1 2 | (my_env) user@user:~/my_env$ deactivate user@user-Lenovo-E40-80:~/my_env$ |
注意,
您可以使用
安装
1 | pip install virtualenvwrapper |
如果您使用的是标准shell,请打开您的
1 2 | export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh |
要激活现有的virtualenv,请使用命令workon:
1 2 | $ workon myenv (myenv)$ |
要停用您的virtualenv:
1 | (myenv)$ deactivate |
以下是我的教程,逐步介绍如何安装virtualenv和virtualenvwrapper
我使用基于autoenv的zsh autoenv。
zsh-autoenv automatically
sources (known/whitelisted).autoenv.zsh files, typically used in
project root directories. It handles"enter" and leave" events,
nesting, and stashing of variables (overwriting and restoring).
下面是一个例子:
1 2 3 4 5 6 7 8 9 10 11 | ; cd dtree Switching to virtual environment: Development tree utiles ;dtree(feature/task24|?); cat .autoenv.zsh # Autoenv. echo -n"Switching to virtual environment:" printf"\e[38;5;93m%s\e[0m ""Development tree utiles" workon dtree # eof dtree(feature/task24|?); cat .autoenv_leave.zsh deactivate |
所以当我离开
在处理安装程序脚本时,我自己也遇到了同样的问题,我查看了bin/activate_this.py做了什么,并将其颠倒。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #! /usr/bin/python # -*- coding: utf-8 -*- import os import sys # path to virtualenv venv_path = os.path.join('/home', 'sixdays', '.virtualenvs', 'test32') # Save old values old_os_path = os.environ['PATH'] old_sys_path = list(sys.path) old_sys_prefix = sys.prefix def deactivate(): # Change back by setting values to starting values os.environ['PATH'] = old_os_path sys.prefix = old_sys_prefix sys.path[:0] = old_sys_path # Activate the virtualenvironment activate_this = os.path.join(venv_path, 'bin/activate_this.py') execfile(activate_this, dict(__file__=activate_this)) # Print list of pip packages for virtualenv for example purpose import pip print str(pip.get_installed_distributions()) # Unload pip module del pip # deactive/switch back to initial interpreter deactivate() # print list of initial environment pip packages for example purpose import pip print str(pip.get_installed_distributions()) |
不是100%确定如果它按预期工作,我可能完全错过了什么。