简体   繁体   English

workon 命令在 Windows PowerShell 中无法激活 virtualenv

[英]workon command doesn't work in Windows PowerShell to activate virtualenv

I have installed virtualenvwrapper-win and when I try this command我已经安装了 virtualenvwrapper-win,当我尝试这个命令时

workon <envname>

In CMD it works, but not in Windows PowerShell.在 CMD 中它有效,但在 Windows PowerShell 中无效。

In Windows PowerShell I have to do Scripts\\activate.ps1 and then I get the envname before the prompt.在 Windows PowerShell 中,我必须执行Scripts\\activate.ps1 ,然后在提示之前获取 envname。

Can you please let me know how can I make workon command working in PowerShell?你能告诉我如何让 workon 命令在 PowerShell 中工作吗?

workon is a batch script. workon是一个批处理脚本。 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.如果你从 PowerShell 运行它,它会在一个新的 CMD 子进程中启动,在那里做它的事情,然后退出并返回到 PowerShell 提示符。 Since child processes can't modify their parent you lose all modifications made by workon.bat when you return to PowerShell.由于子进程无法修改其父进程, workon.bat当您返回 PowerShell 时,您将丢失workon.bat所做的所有修改。

You basically have two options:你基本上有两个选择:

  • Rewrite workon.bat (and the other batch scripts it calls) in PowerShell.在 PowerShell 中重写workon.bat (以及它调用的其他批处理脚本)。

  • Run workon.bat in without exiting from the CMD child process:在不退出 CMD 子进程的情况下运行workon.bat

     & cmd /k workon <envname>

    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:如果你只想要一个快捷方式workon可以从PowerShell中直接调用就可以换行命令行中的函数,并把函数定义为您的PowerShell配置文件:

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

    Use the scriptname with extension here to avoid infinite recursion.在此处使用带有扩展名的脚本名以避免无限递归。

The answer by Ansgar Wiechers technically works but it uses cmd which means you are basically using the cmd prompt from within PowerShell and you lose the additional functionality provided by PowerShell. Ansgar Wiechers 的答案在技术上有效,但它使用cmd ,这意味着您基本上是在 PowerShell 中使用 cmd 提示符,而您将失去 PowerShell 提供的附加功能。 You can modify the function above to the following:您可以将上面的函数修改为以下内容:

function workon ($env) {
        & .\Envs\$env\Scripts\activate.ps1
}

This will allow you to continue to use PowerShell commands (that do not work in cmd such as ls ) in your virtual environment这将允许您在虚拟环境中继续使用 PowerShell 命令(在 cmd 中不起作用,例如ls

This also assumes that your environments are saved in .\\Envs.这还假设您的环境保存在 .\\Envs 中。 If they are elsewhere, then adjust the path in the function accordingly, or set the WORKON_HOME environment variable, see below.如果它们在其他地方,则相应地调整函数中的路径,或设置WORKON_HOME环境变量,见下文。

If you have set the WORKON_HOME environment variable (which you should !), you can instead use:如果您已经设置了WORKON_HOME环境变量(您应该这样做!),您可以改为使用:

function workon ($env) {
        & $env:WORKON_HOME\$env\Scripts\activate.ps1
}

Additionally, if you are not a Windows user (like me) and need help on where to put that function and how to get it to load when you open PowerShell.此外,如果您不是Windows 用户(像我一样)并且需要有关在何处放置该函数以及如何在打开 PowerShell 时加载它的帮助。 Here are some additional resources that helped me:以下是一些对我有帮助的其他资源:

Background:背景:

How to Write a PowerShell Script Module 如何编写 PowerShell 脚本模块

Importing a PowerShell Module 导入 PowerShell 模块

How to get PowerShell to autoload your module when it starts:如何让 PowerShell 在启动时自动加载您的模块:

How to Create a PowerShell Profile 如何创建 PowerShell 配置文件

Start PowerShell with modules loaded 加载模块后启动 PowerShell

只需在powershell上输入“CMD”,它就会调出cmd然后工作

There is a much simpler solution!有一个更简单的解决方案! Just go to your python scripts folder, where the workon.bat file exists and create a new file named workon.ps1 and add the following line to it只需转到存在workon.bat文件的 python 脚本文件夹并创建一个名为workon.ps1的新文件并将以下行添加到其中

iex ("~\Envs\" + $args[0] + "\Scripts\activate.ps1")

You many need to change this appropriately if you store your virtualenvs elsewhere and also set the Execution Policy to allow scripts.如果您将 virtualenvs 存储在其他地方,并且还将执行策略设置为允许脚本,则您需要适当地更改此设置。 Now you can use workon in both cmd and powershell, since the ps1 will be executed in powershell and bat in cmd.现在你可以在 cmd 和 powershell 中使用 workon,因为ps1将在 powershell 中执行,而bat在 cmd 中执行。

You can also check out my fork (full disclosure: I'm the author of the powershell part) of virtualenvwrapper-win, which contains some rewritten scripts for powershell and should work on both CMD and powershell.您还可以查看的 virtualenvwrapper-win 的fork (完全披露:我是 powershell 部分的作者),其中包含一些为 powershell 重写的脚本,应该可以在 CMD 和 powershell 上使用。 If you want to copy-paste, create two files, workon.ps and `cdproject.如果你想复制粘贴,创建两个文件, workon.ps和 `cdproject。

workon.ps1 : workon.ps1

if (-not (Test-Path env:WORKON_HOME))
{
    $WORKON_HOME = '~\Envs'
} else {
    $WORKON_HOME = ($env:WORKON_HOME).Replace('"','')
}

if (-not (Test-Path env:VIRTUALENVWRAPPER_PROJECT_FILENAME)) {
    $VIRTUALENVWRAPPER_PROJECT_FILENAME = '.project'
} else {
    $VIRTUALENVWRAPPER_PROJECT_FILENAME = ($env:VIRTUALENVWRAPPER_PROJECT_FILENAME).Replace('"','')
}

if ($args.length -eq 0) {
    echo "Pass a name to activate one of the following virtualenvs:"
    echo ==============================================================================
    (Get-ChildItem -Path $WORKON_HOME).Name
    return
}

$VENV = $args[0]

if (!(Test-Path -Path ("$($WORKON_HOME)\$($VENV)"))) {
    echo ("virtualenv $($VENV) does not exist")
    echo "Create it with 'mkvirtualenv $($VENV)'"
    return
}

if (!(Test-Path -Path ("$($WORKON_HOME)\$($VENV)\Scripts\activate.ps1") ))  {
    echo "$($WORKON_HOME)$($VENV)"
    echo "doesn't contain a virtualenv (yet)."
    echo "Create it with 'mkvirtualenv $($VENV)'"
    return
}

iex ("$($WORKON_HOME)\$($VENV)\Scripts\activate.ps1")

if (Test-Path -Path ("$($WORKON_HOME)\$($VENV)\$($VIRTUALENVWRAPPER_PROJECT_FILENAME)")) {
    iex "cdproject"
}

cdproject.ps1 : cdproject.ps1 :

function Show-Usage {
    echo ""
    echo  "switches to the project dir of the activated virtualenv"
}

if (-not (Test-Path env:VIRTUAL_ENV)) {
    echo ""
    echo "a virtualenv must be activated"
    Show-Usage
    return
}

if (-not (Test-Path env:VIRTUALENVWRAPPER_PROJECT_FILENAME)) {
    $VIRTUALENVWRAPPER_PROJECT_FILENAME = '.project'
} else {
    $VIRTUALENVWRAPPER_PROJECT_FILENAME = ($env:VIRTUALENVWRAPPER_PROJECT_FILENAME).Replace('"','')
}

if (-not (Test-Path "$($env:VIRTUAL_ENV)\$($VIRTUALENVWRAPPER_PROJECT_FILENAME)")) {
    echo ""
    echo "No project directory found for current virtualenv"
    Show-Usage
    return
}


$ENVPRJDIR = Get-Content "$($env:VIRTUAL_ENV)\$($VIRTUALENVWRAPPER_PROJECT_FILENAME)" -First 1

# If path extracted from file contains env variables, the system will not find the path.
# TODO: Add this functionality

cd $ENVPRJDIR

I had the similar issue.我有类似的问题。 Fixed it by following some of the above steps.按照上述一些步骤修复它。

Listing it down here:在这里列出它:

  • install both VirtualEnv and VirtualEnvWrapper-win安装 VirtualEnv 和 VirtualEnvWrapper-win
  • Set the ExecutionPolicy to RemoteSigned Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser将 ExecutionPolicy 设置为 RemoteSigned Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  • Add environment Path 'WORKON' to 'envs directory'将环境路径“WORKON”添加到“envs目录”
  • Now try 'workon' command现在尝试'workon'命令

I encountered the same problem using virtualenvwrapper in powershell on Windows10.我在 Windows10 上的 powershell 中使用 virtualenvwrapper 遇到了同样的问题。 I like the answer by @Erock but that overwrrides the workon in such a way that running workon without an argument throws error instead of showing available environments.我喜欢@Erock 的答案,但它以这样一种方式覆盖了workon ,即在没有参数的情况下运行workon会引发错误,而不是显示可用的环境。 Here is my solution.这是我的解决方案。

function workon ($env) {
    if ($env) {
        & $env:WORKON_HOME\$env\Scripts\activate.ps1
    } else {
        Write-Host "Pass a name to activate one of the following virtualenvs:"
        Write-Host" ================================================================"
        Get-ChildItem $env:WORKON_HOME -Name
    }
}

Note that I had already set WORKON_HOME envrionment variable.请注意,我已经设置了 WORKON_HOME 环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM