Swift

Update All Installed Python Packages

Hello again !!!

This is my second blog post, and hopefully, i will be able to keep track of the number of post i make as time goes on.

Ok ok ok, enough talking.

In case you dont know, one of many of the few languages i write (i know how that sounds lol) is Python. I'm not going to give any introduction, there are more than enough resources out there if you are interested in it.

That by the way, i had a problem.

YOU:  what problem?
ME:    i needed to upgrade all my packages
YOU:  ohh. so how did you do it?

ME:    simple. i use pip to install all my packages so following a tutorial online would be
          walkover. i went to our favorite search engine and put my query.
          After looking around, i found a good one liner solution


SOLUTION
I use a flavor of GNU/Linux as my operating system
I opened up my terminal and typed

python -c 'import pip, subprocess; [subprocess.call("pip install -U " + d.project_name, shell=1) for d in pip.get_installed_distributions()]'
  
I would love to explain this more to you, but i'm at work and don't have that much time.
1- "python" from the code above is a program installed on my box already
2- "-c" argument which allows you pass in small codes as a string from your bash terminal
3- Every other thing are python codes
4- import is a python statement to include other python codes into your code
5- pip is a python package  i want to include
6- subprocess is another python package  i want to include
7- "call()" is a function defined in the subprocess package and what it does is to take commands you can run in your terminal and run it for you.
8- "get_installed_distributions()"  is also a function defined in the pip package
9- the code also uses concatenation and loop concepts in python to get all installed packages and for each of them, concatenate it to "pip install -U " and use the call () function to run the command each time.
10- the amount of time it takes to update all installed packages depends on the amount of packages you have installed. Your Internet speed matters too.

TIP: to automate this process, paste the above code into a file and save as  a ".sh" and you could use cron jobs to run the file as frequently as you want.

So, 30 minutes after i started writing this post, my packages are still updating.




You Might Also Like

0 comments