automatically update python script
Asked Answered
P

4

11

TL;DR I need a module which will automatically update my script in the background, silently.

I'm have a Python script which I distribute to users. I frequently update this, and then ask them to update it (via PIP). Obviously, this isn't a high priority for users, who just want to use the app, not think about updating it.

I'd like it to update my app automatically, like Google Chrome does, silently, in the background, automatically. Is there a library that allows me to do this already? If not, is there a straightforward way to use the PIP/distribute module to do it?

Partlet answered 12/9, 2011 at 18:24 Comment(2)
fyi - pip installs into the system directories using sudo/admin privs, so users might not have the privileges to updateTegument
Can it be used to install in the user's homedir (say, under my ~/.myCLIapp/)? in the worst case, I could surely install a virtualenv?Partlet
W
4

If pip install already works for you, why can't you just do os.system("pip install -U myscript") at script startup? This is kinda dirty, but so is distributing via pip for non-developers.

Whitebook answered 12/9, 2011 at 21:33 Comment(0)
H
1

The easiest way to do this is to set up a web service which the script pings when it is run. The web service can return a version number, which the script can check against its own version number. If the version number is higher, it can update itself and re-run.

Herbertherbicide answered 12/9, 2011 at 18:37 Comment(2)
Sure, Ive thought of this, and have half a prototype built. But things get a little complicated - what if it becomes more than a script, what if it has dependencies, etc. That's why I figure it should probably use PIP.Partlet
I have used this technique with success in the past for simple scripts, Wilduck. Paul is correct that it does have problems with dependencies.Herbertherbicide
T
1

I've experimented with BitRock for an open-source application I'm developing. They provide cross-platform installers for an application that have automatic updates. Licensing is free if your application is open-source, but commercial products require purchasing a license. It might be overkill if your application is small, but I thought I'd still give it as one option.

Tegument answered 12/9, 2011 at 18:41 Comment(0)
T
0

Another easy way is to update the script using cronjobs. For example, you want to update the module once a week on Sunday. So for this purpose, write the following command in the terminal

crontab -e

Then write the following line at the end of this file.

00 00 * * SUN python3 -m pip install <module_name>

The above line will again install the module at 00:00 on Sunday automatically.

Note: This will only work on Linux or Ubuntu.

Tiger answered 7/4, 2022 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.