workon 命令在 Windows PowerShell 中无法激活 virtualenv [英] workon command doesn't work in Windows PowerShell to activate virtualenv

查看:265
本文介绍了workon 命令在 Windows PowerShell 中无法激活 virtualenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 virtualenvwrapper-win,当我尝试这个命令时

I have installed virtualenvwrapper-win and when I try this command

workon <envname>

在 CMD 中它有效,但在 Windows PowerShell 中无效.

In CMD it works, but not in Windows PowerShell.

在 Windows PowerShell 中,我必须执行 Scripts\activate.ps1,然后在提示前获取 envname.

In Windows PowerShell I have to do Scripts\activate.ps1 and then I get the envname before the prompt.

你能告诉我如何让 workon 命令在 PowerShell 中工作吗?

Can you please let me know how can I make workon command working in PowerShell?

推荐答案

workon 是一个批处理脚本.如果你从 PowerShell 运行它,它会在一个新的 CMD 子进程中启动,在那里做它的事情,然后退出并返回到 PowerShell 提示符.由于子进程无法修改其父进程,因此当您返回 PowerShell 时,workon.bat 所做的所有修改都会丢失.

workon is a batch script. If you run it from PowerShell it's launched in a new CMD child process, doing its thing there, then exit and return to the PowerShell prompt. Since child processes can't modify their parent you lose all modifications made by workon.bat when you return to PowerShell.

你基本上有两个选择:

  • 在 PowerShell 中重写 workon.bat(以及它调用的其他批处理脚本).

  • Rewrite workon.bat (and the other batch scripts it calls) in PowerShell.

在不退出 CMD 子进程的情况下运行 workon.bat:

Run workon.bat in without exiting from the CMD child process:

& cmd /k workon <envname>

如果您只想要一个可以直接从 PowerShell 调用的快捷方式 workon,您可以将该命令行包装在一个函数中,并将函数定义放入您的 PowerShell 配置文件中:

If you just want a shortcut workon that you can call directly from PowerShell you can wrap that commandline in a function and put the function definition into your PowerShell profile:

function workon($environment) {
  & cmd /k workon.bat $environment
}

此处使用带扩展名的脚本名以避免无限递归.

Use the scriptname with extension here to avoid infinite recursion.

这篇关于workon 命令在 Windows PowerShell 中无法激活 virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆