Python Forum
reading data from command line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading data from command line
#1
I want to automate updating python packages through pip. I think I have most of it. I just don't know how to evaluate the output from results = os.system('pip list --outdated'). I thought putting 0 for none would be sufficient but it doesn't work. It gives me a list of outdated packages but says they are all up to date. It never runs lines 12-21. Wall
def pkg_update(): options = ['Check for updates?', '1 Yes', '2 Exit'] for option in options: print(option) choice = input('> ') if choice == '1': print('Checking for updates.') results = os.system('pip list --outdated') if results == 0: # What do I put here? print('All packages are up to date.\n') pkg_update() else: print(results) while True: update_list = list(results) current = update_list.pop(0) print(f'Updating {current}.') os.system(f'''pip install --upgrade {current}''') print('Done') pkg_update() elif choice == '2': raise SystemExit else: pkg_update() if __name__ == "__main__": pkg_update(
Reply
#2
Do not use os.system() it's deprecated and unsafe,use subprocess .
Example i check for module/package,if it's in outdated list i upgrade it.
import subprocess out = subprocess.run(['pip', 'list', '--outdated'], capture_output=True) response = out.stdout.decode() module_name = 'click' if module_name in response: subprocess.run(['pip', 'install', module_name, '--upgrade']) else: print(f'No module with name <{module_name}> found')
Output:
E:\div_code λ python sub_2.py Collecting click Using cached https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl Installing collected packages: click Found existing installation: click 6.7 Uninstalling click-6.7: Successfully uninstalled click-6.7 Successfully installed click-7.0
I only use pip list in a virtual envorioment,as OS is mess because i do test a lot often to help users install problem on forum.
So i have many modules/package that i don't care about or want to update.
Reply
#3
I use this for list --outdated
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] outdated = call("pip list --outdated " + ' '.join(packages), shell=True) print(outdated)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am a newbie I like to use the command line Mikel2025 1 1,018 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Insert command line in script lif 4 2,168 Mar-24-2025, 10:30 PM
Last Post: lif
  Simplest way to run external command line app with parameters? Winfried 2 2,257 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Reading an ASCII text file and parsing data... oradba4u 2 2,799 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Reading and storing a line of output from pexpect child eagerissac 1 8,025 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Reading in of line not working? garynewport 2 2,489 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 4,371 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Command line argument issue space issue mg24 5 3,083 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  Reading Data from JSON tpolim008 2 3,932 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  accept command line argument mg24 5 3,065 Sep-27-2022, 05:58 PM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.