2009-09-01 21 views
17

嗨夥計pythonistas,在PowerShell中使用virtualenv時似乎有問題。

當我嘗試激活我的環境中使用PowerShell如..

> ENV /腳本/激活

..沒有任何反應。 (shell提示符應該和PATH env變量一樣改變。)

我想問題是PowerShell會產生一個新的cmd。進程只是爲了運行activate.bat,因此在完成後渲染變化activate.bat對shell死了。

您對此問題有任何解決方法嗎? (我現在堅持使用cmd.exe)

回答

9

Here是一個包含Powershell腳本的文章,它允許您運行持久修改其環境變量的批處理文件。該腳本將任何環境變量更改傳回給調用的PowerShell環境。

+0

謝謝Vinay,清楚地解釋了這個問題。 – 2009-09-02 02:38:29

+0

這有效,但不能添加提示參數。在設置提示符之前,必須有一個可以調用Write-Host的activate.ps1。 – 2011-04-05 14:59:55

+2

這個答案不再有效,因爲virtualenv支持powershell開箱即用,正如http://stackoverflow.com/questions/1365081/virtualenv-in-powershell/10030999#10030999 – 2014-03-14 21:45:15

10

快速解決方法是調用cmd,然後從cmd會話中運行activate.bat。例如:

PS C:\my_cool_env\Scripts> cmd 
Microsoft Windows [Version 6.1.7600] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 

C:\my_cool_env\Scripts>activate.bat 
(my_cool_env) C:\my_cool_env\Scripts> 
+0

這對我有效。非常容易。謝謝! – irene 2017-08-09 11:07:21

45

The latest version of virtualenv支持PowerShell中出的現成

只要確保你運行:的

Scripts\activate.ps1 

代替

Scripts\activate 

後者將執行activate.bat,這不會對PowerShell的工作。

+17

您可能需要運行'set-executionpolicy RemoteSigned'來允許腳本運行(我之前設置爲'Restricted')[docs](http://technet.microsoft.com/en-us/library/ee176961.aspx ) – 2012-12-22 08:29:53

+0

,只需鍵入'deactivate'來撤消 – 2017-05-04 13:35:19

+0

有沒有辦法使用這種方法改變提示的顏色? – 2017-06-04 03:59:31

1

試試這個: . .\env\Scripts\activate.ps1 手錶點和空間

+1

。\ [my-venv] \ Scripts \ activate.ps1爲我工作。 – 2016-03-31 18:28:49

0

我寫了這個快速的小腳本來處理我的激活和啓動的開發服務器。

$ep = Get-ExecutionPolicy 

if ($ep -eq 'RemoteSigned') { 

    $root = "C:\Users\ALeven\OneDrive\!code_projects\!django_projects\" 

    $test = Read-Host -Prompt 'Would you like to activate the python environment? y/n' 
    if ($test -eq 'y') { 

     $activatestr = ($root + "\work_venv\Scripts\Activate.ps1") 
     & $activatestr 

    } 

    $test = Read-Host -Prompt 'Would you like to run the python server? y/n' 

    if ($test -eq 'y') { 

     $whichserver = Read-Host -Prompt 'Enter the name of the project.' 
     $path = ($root + $whichserver) 
     $runserverstr = ($path + "\manage.py") 
     python.exe $runserverstr runserver 

    } 

} else { 

    Write-host "Execution Policy does not allow this script to run properly" 
    Write-host "If you have the proper permissions," 
    Write-Host "Please close powershell," 
    Write-host "then right click the powershell icon and run as administrator" 
    Write-host "Once in the powershell environment, execute the following:" 
    Write-host "Set-ExecutionPolicy RemoteSigned -Force" 

} 

享受。