Python Forum
PowerShell script file for updating packages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PowerShell script file for updating packages
#7
(Jun-19-2022, 02:50 AM)pnachtwey Wrote: [quote="Gribouillis" pid='158393' dateline='1655357364']
If you mean in the pip command line, you could replace it by something like
from pathlib import Path
import subprocess as sp
import sys

with Path('req0.txt').open('w') as ofile:
    sp.run([
        sys.executable, '-m', 'pip', 'list', '--outdated', 
        '--exclude', 'idna', '--exclude', 'mistune',
        '--exclude', 'tomli', '--format=freeze', '--user'], stdout=ofile)
Excellent! Your code is probably better than mine which isn't using the latest tricks. I will study your code and try it out.

import os
import subprocess
cmd="pip list --outdated --exclude astroid --exclude idna --exclude mistune --exclude mccabe --exclude wrapt --format=freeze --user > req0.txt"
os.system(cmd)
in_file = open("req0.txt", 'r')
out_file = open("req1.txt", 'w')
while True:
    in_line = in_file.readline()
    if not in_line:
        break
    out_line = in_line.replace("==",">=")   
    out_file.writelines(out_line)
    
in_file.close()
out_file.close()

try:
    if os.path.getsize("req1.txt") > 0:
        os,system("pip install --user --upgrade -r req1.txt")
        os.system("pip list --outdated > o.txt")
        os.system("pip check >> o.txt")
        o_file = open("o.txt","r")
        while True:
            o_line = o_file.readline()
            if not o_line:
                break
            print(o_line.strip())
        o_file.close()
    else:
        print("Nothing to update")
except OSError as e:
    print("File does not exist")
In any case. We have made improvements over the method at
https://www.activestate.com/resources/qu...-packages/

BTW, isn't there a better way of excluding packages than using --exclude package for each package?
I looked and couldn't find any.

@Gribouillis
edit, wait! I did see where you edit the lines and update the packages!
Does python have something like sed and xargs? I couldn't find that either.

I have a bash and zsh script where I use xargs. It is much cleaner than my Python script.

Should I put this in a separate thread?

pip list --outdated --format=freeze --exclude idna --exclude mistune --exclude mccabe --exclude pycurl --exclude PyGObject --exclude dbus-python --exclude pycairo | sed 's/==/>=/g'| xargs pip install --upgrade --user
pip list --outdated
pip check

I just checked my python script. Now I have it in my python path so I can just type python -m pyupd to update my packages
Reply


Messages In This Thread
RE: PowerShell script file for updating packages - by pnachtwey - Jun-20-2022, 05:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020