2009-09-01 7 views
17

こんにちは仲間pythonistas、virtualenvがPowerShellで使用されているときに問題があるようです。PowerShellのvirtualenv?

私のようなPowerShellで私の環境をアクティブにしよう...

> ENV /スクリプト/

を活性化させる。..何も起こりません。 (シェルプロンプトがPATH環境変数と同様に変更されているはずです)

問題は、PowerShellが新しいcmdを生成するということです。 activate.batを実行するだけのプロセスで、activate.batの変更は、完了した後にシェルに行き渡ります。

問題の回避策はありますか? (私はcmd.exeを今のところ固執しています)

答えて

9

Hereは、環境変数を永続的に変更するバッチファイルを実行できるPowershellスクリプトを含んでいます。スクリプトは、環境変数の変更を呼び出し元のPowerShell環境に伝播します。

+0

Vinayに問題があることを明確に説明してくれてありがとうございます。 –

+0

これは動作しますが、プロンプト引数を追加することはできません。プロンプトが設定される前にWrite-Hostを呼び出すことができるactivate.ps1が必要です。 –

+2

この回答は、virtualenvがhttp://stackoverflow.com/questions/1365081/virtualenv-in-powershell/10030999#10030999 –

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

で指摘されているように、すぐに使用できるPowerShellをサポートしているため、もう有効ではありません。とても簡単です。ありがとう! – irene

45

The latest version of virtualenvは、PowerShellをサポートしてすぐに

ちょうどあなたが実行していることを確認します

Scripts\activate.ps1 

代わりの

Scripts\activate 

後者はPowerShellの上では動作しませんactivate.batを実行します。

+17

スクリプトを実行できるようにするには 'set-executionpolicy RemoteSigned'を実行する必要があります(以前は' mashin 'Restricted'に設定されていました)[docs](http://technet.microsoft.com/en-us/library/ee176961.aspx ) –

+0

と入力して、元に戻すには 'deactivate'と入力してください –

+0

この方法でプロンプトの色を変更する方法はありますか? –

1

はこれを試してみてください。 . .\env\Scripts\activate.ps1 時計ドットとスペース

+1

。\ [my-venv] \ Scripts \ activate.ps1が私のために働いた。 –

2

このエラーは、スクリプトがあなたがそれを承認せずに、あなたのシステム上で実行させませんセキュリティ対策に起こります。あなたが管理者権限でPowerShellを開くことによって行うことができます(メインメニューでのPowerShellを検索し、コンテキストメニューから[管理者として実行]を選択)して入り:

セットexecutionpolicy多くのため

をremotesigned: http://www.faqforge.com/windows/windows-powershell-running-scripts-is-disabled-on-this-system/

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" 

} 

お楽しみください。