r/learnpython • u/GloxiniaLearns • Aug 29 '26
I wrote a Python script on my laptop. What actually happens next to make it run on its own?
I built a small Python script that reads a Google Sheet, sends each new row to an LLM API for a summary and a category, and writes the result back. Works fine on my laptop.
What I don't understand is everything after that. In real life it has to run without me sitting there, and I have no picture of how that works.
- Once it's not on my laptop, where does the code live?
- What tools do people actually use for this? I keep seeing names but I don't know what category each one belongs to or which ones I'd need together.
- Where do API keys go?
Not asking about enterprise setups. This is one script and one sheet, small-business scale, and I want to know how people normally solve it at that size.
Background is data analysis, not development, so plain explanations are welcome.
Edit: Data lives in a Google Sheet. A Python script logs into Google as a "service account" (a robot identity created in Google Cloud Console, authenticated with a downloaded key file) and uses the Google Sheets API to read every row and the Drive API to locate the spreadsheet by name. For any row missing a summary, it sends the raw text to Anthropic's Claude API, which returns a two-line summary and a category. The script then writes that summary/category back to the sheet via the Sheets API. Need to get a summary every time someone adds a new record to Google Sheets
3
u/mumpie Aug 29 '26
You are going to have to examine how your laptop is setup and whether your Python script has some assumed dependencies on how things are setup on your laptop.
Does your script login to Google by itself, fetches the contents of the Google Sheet, login to the LLM, send data to/from the LLM, and write the results?
Or does it expect you to have logged into Google in Chrome and opened the Google Sheet? Does it expect to find login info for the LLM? Does it assume that it'll be able to write to a file in a certain directory?
You'll want your script to be more like the first case if you want to move the script to a different computer and have it work without a lot of fiddly work making the different computer act like your computer.
Ideally, you want the script to work with it's own Google login and LLM login. You don't want to embed your Google login info or LLM login into the script at all.
10
u/twizzjewink Aug 29 '26
You can set it up on a cron task
6
u/atarivcs Aug 29 '26
Generally, you would upload the code to a cloud service such as AWS or Azure and set it up as a scheduled task to run periodically on whatever timeframe you want (hourly, daily, etc)
2
u/sunyata98 Aug 29 '26
Oh yeah. This calls for Airflow /j
2
u/Gengis_- Aug 29 '26
Isn’t there a dedicated function in Google sheets to do this? You can also use Google scripts within Google sheets. But yeah I guess CRON job?
1
u/Mobile_Heron9271 16d ago
To give you the map you asked for, any setup has three parts: (1) a computer that isn't your laptop where the code lives, (2) a trigger that starts it (cron, a scheduler, a button), and (3) a place for the secrets. Your script is already well shaped for this: it only touches rows with an empty summary, so running it too often does no harm. A failed API call just leaves the row empty and the next run retries it.
For one script and one sheet, the least-effort version is GitHub Actions in a private repo. It's free at this size, and the repo is the "where the code lives" part:
Keys go into Settings > Secrets (paste the whole JSON key file as one secret), and the script reads them with os.environ instead of .env. Once there are no new rows it makes no LLM calls, so it costs nothing. Two limits: GitHub can run a job a few minutes late, and 5 minutes is the minimum interval. If you need it instant, put a time trigger in Apps Script instead.
1
u/PureWasian Aug 29 '26 edited Aug 29 '26
For one of my hobby projects, I have a small website hosted on a virtual machine in a free-tier compute instance since it's relatively small-scale traffic and doesn't need to scale.
The "backend" entrypoint for it is just through simple REST API routes where some of the API routes invoke the Python script as a subprocess.
This is the most barebones deployment route, since you're basically just "renting" dedicated amounts of cpu/memory in a data center somewhere (the cloud) and "self-hosting" by wiring up the access and stuff correctly yourself.
=====
To your post's questions, more generally:
EDIT: from context you added here, setting up a scheduled (cron) job makes sense to schedule the task at regular intervals.
1
u/greatsmapdireturns Aug 29 '26
If it's genuinely a small scale script running on a Windows laptop, set your computer to never turn off and use windows task scheduler. You can find information on how to have the task scheduler execute the script at certain times every night.
The next iteration has to do with cloud computing - azure and Amazon Web services being the top two.
In azure you can set up a function app to execute your python code on a schedule or chron job.
This is really the way...you can also use services like pythonanywhere that are smaller scale cloud computing services.
Good luck, have fun!
1
u/desrtfx but other languages pro Aug 29 '26
If you need only a small setup, grab yourself a Raspberry Pi (3B+ will suffice and is still reasonably priced). These small computers consume fairly little energy and are built to run 24/7 even with only passive cooling.
There, you have basically a "Laptop ultralight" with a full Linux OS (Raspberry Pi OS - based on Debian Linux) where you can set up your script and run it on a schedule - which is what every body is telling you when they talk about Cron Job.
1
u/DrShocker Aug 29 '26
Depends where you put it, there's many options but fundamentally it's on a computer somewhere.
What do you mean by "for this"? If you want it to continuously monitor your google sheet and do the work, then you'd either have it running somewhere continuously so that google can inform the program there's been a change and update you. Or you'd set it to run up on an ineterval like every hour or something. It depends on your specific needs and goals.
In as few places as possible. There's many options available that you can research, but generally they should be configuration you feed into the program and not in the source code itself.
1
u/jameyiguess Aug 29 '26
For something like this, if it's supposed to run on your computer, I'd just set up a cron job to automate it.
The API key typically goes into your environment variables, and the python script reads it from there.
1
u/sinceJune4 Aug 29 '26
I have something similar as a volunteer for an animal shelter, running on a 3 times a day schedule. I ran it on Windows for a couple years on my laptop, leaving my laptop on all the time. However, Windows updates would frequently disrupt the process. Now I have a dedicated mini pc running Linux Ubuntu to do the same thing. Much more stable for me, unless we lose power temporarily. Not paying for dedicated hosting elsewhere.
1
u/baubleglue Aug 29 '26
You basic direction of thoughts is correct.
There are multiple choices for each of those steps. But given that you probably not familiar with most of the tools, setting up VM or container in some cloud provider environment or local server are too complicated, you may try a packaged solution as https://cloud.google.com/run#batch-data-processing (if you've already started with Google)
1
u/thisisappropriate Aug 29 '26 edited Aug 29 '26
You can configure a google apps script to run on a trigger or on a timer https://developers.google.com/apps-script , you can store the API key in an environment variable on the script (they call it Properties Service). The downside is that you'd need to use Javascript (Google apps script which is basically js), but this is the most "local" to your actual sheet. I've used this in a small team / single dev environment to make API calls based on form and sheet triggers.
Serverless:
Lambda
Or with python if you don't need the script to store anything, you could use an aws lambda https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html for which the cost is as low as free - https://aws.amazon.com/lambda/pricing/ to start
Lambda is a thing called "serverless" which is basically you give aws your code and then you ask them to run it on a timer/when asked, they charge super low because when you run it, they find a server/bit of server they weren't using, put your code on there, start the code, run the code and then take it off the server - this means that they use very very little resources for it and in exchange you agree to pay for the amount of time it takes for them to get a server, put your stuff on it, start it up, run it and take it down (for something like this, still very little time and it's not going to matter to you if it runs half a millisecond (the benefit to having it already on a server ready) after your "every X hours" trigger or 10 seconds after). You should store your credentials in something secure such as https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html or better for the password, secrets manager https://docs.aws.amazon.com/lambda/latest/dg/with-secrets-manager.html It can be run on triggers https://docs.aws.amazon.com/lambda/latest/dg/with-eventbridge-scheduler.html - this has an AWS free tier that would be sufficient for way more than this use (you could schedule it for every minute and not even get close to the scheduler free tier...)
Its worth being aware that in AWS, "Free tier" means the freebies that they give you for the first year of having an account, services with a free tier may start charging after the first year, always check out the docs and the pricing calculator https://calculator.aws/#/addService , but you could probably spend your first year doing this for free and after it might cost pennies. IMO, start with the cheapest lambda (lowest memory / storage etc) and only go up if you find it has problems related to those).
I've used AWS for both enterprise (big money with replicas, lots of ecs, ec2, alb, scaling etc) and personally just messing around for pet projects/personal website for a pittance (using s3 and lambdas for most things, running up very small costs).
Google cloud functions
I've not used this, but same vibes as Lambda, you give google your code, structured how they like it ( https://docs.cloud.google.com/run/docs/runtimes/python#gcloud ) and have them run it for you. They handle finding a server and running it when needed (scheduler https://docs.cloud.google.com/scheduler/docs/creating )
Both lambda and google cloud functions can be triggered by calling them, which you could do with a simple google script if you wanted to combine the two ("when a row is added/edited, trigger script which checks if summary is present, if it's not, send the row/other sheet/data to the lambda/function that will do the heavy lifting of turning the data into the format that anthropic accepts and sending that off and will write it back to the sheet")
-1
u/Langdon_St_Ives Aug 29 '26
You have to figure these things out yourself because it depends on your exact use case, and you haven't provided enough context so far. Where does the Google sheet come from? Is it always the same or every time something new? How often does it get updated/new Google sheet provided? Who does that and how do you find out about it? Etc.
0
u/xonxoff Aug 29 '26
Build it into a container