LetzerWille
LetzerWille

Reputation: 5658

powershell to run pip

I want to run python modules upgrades vie powershell script. First line works.
But I do not know how to read the file correctly into the second pip line. I get this error:

Could not find a version that satisfies the requirement Get-Content   

pip freeze| Out-File requirements.txt 
pip install --upgrade  Get-Content requirements.txt
Remove-Item  requirements.txt

UPDATE: Works now with changed second line.

    pip freeze| Out-File requirements.txt 

    foreach($line in Get-Content requirements.txt) 
    {
      pip install --upgrade $line 
    }  

    Remove-Item  requirements.txt

UPDATE 2 Now with python 3.6 I use this script.

$(
$exclude = 'virtualenv', 'prompt-toolkit'
pip list --outdated --format=freeze  | ForEach{ $_.split("=")[0]} | Where-Object { $exclude -notcontains $_ } | ForEach { pip install -U $_ }                               
) *>&1 >> Python_Modules_Updates_Log.txt

Upvotes: 3

Views: 11423

Answers (1)

mklement0
mklement0

Reputation: 437208

The simplest way to achieve your goal:

pip freeze | ForEach-Object { pip install --upgrade $_ }

Each output line from pip freeze is passed through the pipeline, and the ForEach-Object script block invokes pip install --upgrade for each ($_).


As for what you tried:

pip install --upgrade  Get-Content requirements.txt # !! BROKEN

Get-Content and requirements.txt are simply additional arguments passed to pip, which explains the error message you saw.

pip - without -r - only accepts one package (requirements specifier) at a time, so even something like pip install --upgrade (Get-Content requirements.txt) would not have worked (it would have passed the lines of file requirements.txt as individual arguments).

With -r, a filename argument is required, so you could have tried:

pip install --upgrade -r requirements.txt

Note that PowerShell, as of Windows PowerShell v5.1 / PowerShell Core v6.0.2, does not support Bash-style process substitutions, where a command's output can act as a transient file:

pip install --upgrade -r <(pip freeze) # !! WISHFUL THINKING - does NOT work yet

However, such a feature is being considered.

Upvotes: 5

Related Questions