译(三十)-Python退出虚拟环境

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

怎么退出Python虚拟环境?

  • Apreche asked:

    • 在用virtualenv和virtualenvwrapper时,我能通过workon命令很好的切换虚拟环境。

    • me@mymachine:~$ workon env1
      (env1)me@mymachine:~$ workon env2
      (env2)me@mymachine:~$ workon env1
      (env1)me@mymachine:~$ 
    • 但是怎么退出虚拟环境去使用原本机器的环境?现在我切换到me@mymachine:~$的唯一办法是退出这个进入shell再开个新的,太麻烦了。有命令能直接实现这点吗?如果没有,怎么样实现一个?

  • Answers:

    • Brandon Rhodes – vote: 3222

      • 通常,激活virtualenv后可以使用shell函数:
      • $ deactivate
      • 这可以用来回到原来的环境。
      • 我刚刚又读了一遍virtualenvwrapper的代码,没错,它也支持通过deactivate退出虚拟环境。
      • 如果是为了离开Anaconda环境,则取决于conda版本。Conda 4.6可以直接使用下面的命令:
      • conda deactivate
      • 早期的conda则独立运行一个脚本来退出虚拟环境:
      • source deactivate
    • DarkRider – vote: 71

      • Use:
        试试这个:
      • $ deactivate
      • 上面如果不行,再试试这个:
      • $ source deactivate
      • 了解Bash source工作方式的人会觉得这样做很奇怪,但一些实现虚拟环境的wrappers或workflows以它作为source activate的等效。所以试试哪个能用吧。
    • Bob Stein – vote: 57

      • 我定义了一个别名workoff,来实现与workon相反的功能:
      • alias workoff='deactivate'
      • 这样就很好记了:
      • 
        [bobstein@host ~]$ workon django_project
        (django_project)[bobstein@host ~]$ workoff
        [bobstein@host ~]$`

How to leave/exit/deactivate a Python virtualenv

  • Apreche asked:

    • I\’m using virtualenv and the virtualenvwrapper. I can switch between virtualenv\’s just fine using the workon command.
      在用virtualenv和virtualenvwrapper时,我能通过workon命令很好的切换虚拟环境。

    • me@mymachine:~$ workon env1
      (env1)me@mymachine:~$ workon env2
      (env2)me@mymachine:~$ workon env1
      (env1)me@mymachine:~$ 
    • How do I exit all virtual machines and work on my real machine again? Right now, the only way I have of getting back to me@mymachine:~$ is to exit the shell and start a new one. That\’s kind of annoying. Is there a command to work on nothing, and if so, what is it? If such a command does not exist, how would I go about creating it?
      但是怎么退出虚拟环境去使用原本机器的环境?现在我切换到me@mymachine:~$的唯一办法是退出这个进入shell再开个新的,太麻烦了。有命令能直接实现这点吗?如果没有,怎么样实现一个?

  • Answers:

    • Brandon Rhodes – vote: 3222

    • Usually, activating a virtualenv gives you a shell function named:
      通常,激活virtualenv后可以使用shell函数:

    • $ deactivate

    • which puts things back to normal.
      这可以用来回到原来的环境。

    • I have just looked specifically again at the code for virtualenvwrapper, and, yes, it too supports deactivate as the way to escape from all virtualenvs.
      我刚刚又读了一遍virtualenvwrapper的代码,没错,它也支持通过deactivate退出虚拟环境。

    • If you are trying to leave an Anaconda environment, the command depends upon your version of conda. Recent versions (like 4.6) install a MARKDOWN_HASH19d07b1f2eb7bd8f0c8e967b228f57d2MARKDOWNHASH function directly in your shell, in which case you run:
      如果是为了离开[Anaconda](https://en.wikipedia.org/wiki/Anaconda(Python_distribution))环境,则取决于conda版本。Conda 4.6可以直接使用下面的命令:

    • conda deactivate

    • Older conda versions instead implement deactivation using a stand-alone script:
      早期的conda则独立运行一个脚本来退出虚拟环境:

    • source deactivate

    • DarkRider – vote: 71

    • Use:
      试试这个:

    • $ deactivate

    • If this doesn\’t work, try
      上面如果不行,再试试这个:

    • $ source deactivate

    • Anyone who knows how Bash source works will think that\’s odd, but some wrappers/workflows around virtualenv implement it as a complement/counterpart to source activate. Your mileage may vary.
      了解Bash source工作方式的人会觉得这样做很奇怪,但一些实现虚拟环境的wrappers或workflows以它作为source activate的等效。所以试试哪个能用吧。

    • Bob Stein – vote: 57

    • I defined an alias, workoff, as the opposite of workon:
      我定义了一个别名workoff,来实现与workon相反的功能:

    • alias workoff='deactivate'

    • It is easy to remember:
      这样就很好记了:

    • [bobstein@host ~]$ workon django_project
      (django_project)[bobstein@host ~]$ workoff
      [bobstein@host ~]$`

You may also like...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注