I run Python 3.x on a Windows 10 laptop and I have a daily routine that involves opening three Python scripts in succession, and running them all concurrently, each one in its own, separate instance of Idle. I have been wondering whether I could automate the entire process.

I imagine this might need to involve some sort of Windows batch file initially to open each Idle instance and, within each instance, to open and run a Python script. I don't even know whether such a sequence of actions, stepping between Windows batch file programming and Idle application programming, is possible; still less how to do it. In detail, I would like to carry out the following:

  1. Click on Idle.bat file to open Idle shell window1;
  2. In Idle shell window1, click on File/Open… to open Python script1;
  3. In script1 window, click on Run/F5;
  4. Wait for file to execute, with its output displayed in the Idle edit window;
  5. Move and resize the windows (currently done using the mouse);
  6. Repeat 1 -5 for script2;
  7. Repeat 1 - 5 for script3;

If anyone can tell me whether this is possible and, if so, how to do it, I would be grateful.

Many thanks
Peter

Recommended Answers

All 9 Replies

Is there a reason you need to run the scripts from within idle as opposed to just running the scripts directly? If so then I suggest you look at AutoItX. I've been using AutoIt for years in both a corporate and home environments, originally from vbScript but now from Python. While the splash page says "basic like scripting language", it also installs a scriptable (from vbScript/Python) component that you can create by

import win32com.client
aut = win32com.client.Dispatch("AutoItX3.Control")

There is an AutoItX help file that you can refer to after installation to get details. In includes extensive controls for window and mouse manipulation (and a lot more).

It's free.

commented: That looks like a "way of achieving this" to me! +15

Re comment and question by Reverend Jim:

Thank you for your reply and your suggestion.
To answer your question: the reason I have been running my Python scripts inside an Idle environment is that I need the three scripts to be running concurrently (all day) and I have no idea how to achieve this using threads or multiprocessing. Idle is, I think, a lightweight environment, so I can have three instances running independently without overburdening the laptop processor. I'm not aware of any other way of achieving this.

Peter.W

Depending on what the scripts are doing and how fast they need to react you could always build in pauses to reduce cpu loading. Or, if your processing depends on, for example, the appearance of files, you could implement a folder watch which would idle itself until a predefined folder event occurs.

I have a script that I run when I want to download a sequence of images from one or more websites. The main loop watches for the creation of a window titled "Save As". When this window is detected it scans the current folder for names like "Image ####.jpg" and auto-generates the next available number in the sequence. It then pastes this name into the filename box of the Save As window. It then clicks the "Save" button. It saves me a lot of useless typing and clicking. The main loop runs with a one second sleep so there is essentially no overhead when there is nothing to do.

What is it your scripts are supposed to do? Perhaps I can offer some suggestions. The majority of what I did before retiring involved automating real time processes that had to run continuously.

In the event that this is for work rather than for personal use I highly recommend adTempus. It is an excellent package for scheduling processes and supports a large variety or trigger types including folder events. I'm not paid to endorse it. I just used the product for several years and was impressed by it's capabilities and the quick response to any questions I had. It is also very reasonably priced. Just a suggestion in case you have an actual budget and the need for such software.

Re: Further suggestions by Reverend Jim:

Thank you for your continued advice. You asked what it is I am trying to do, so here goes.
I am also retired and have, over the past few years, out of personal interest, developed a number of python progs to scrape streamed (financial) data from a couple of websites. They do this regularly, at 5 sec intervals, and present the data on screen in a growing list so it is easier for me to monitor changes. One of the programs also stores the data in a .txt file each day. Another program logs me in to a broker, facilitates any trading I might want to do and records any actions I have taken during the day, such as trades made, stop-loss values and alerts I have set, etc. It doesn't yet tell me how to trade profitably, but one day, who knows! I have enjoyed developing the programs, and also my knowledge of python along with them.
I spent this last weekend investigating Autoit, as you suggested, in order to see if I can automate the start-up process I undertake each morning (see my earlier post). Autoit is, clearly, a powerful program in a Windows environment. However, I have hit a snag with it: it won't pick up the 'windows' generated by Idle as they are not Windows windows, if you get my drift. It is a pity, but I don't think Autoit is going to help me with this, unless I can think of a work-around. I've tried using mouse positioning but it seems, annoyingly, that Idle opens its shell in diffferent locations on the desktop, so pre-set cursor positions don't hit their mark. Not a big deal - I will just carry on as before, if I have to.
Another possibility, based on your question, 'Why use Idle?' is to see if there is another way I can run my programs concurrently, and get the screen outputs, without Idle (and in an environment that would allow Autoit to work). I haven't tried that before now simply because Idle has worked for me so far. Before adopting that approach, I investigated threads and multiprocessing but backed off because it became too complex and it is known that difficulties arise when multiprocessing is combined with the Tkinter GUI (according to some heavyweight posters on StackOverflow).
All good fun. Thank you for your interest; I will let you know if I make any progress.
Peter.W

It seems to me that a simple solution would be to set up each script on a loop and put a pause at the end for the interval you want.

import os
import sys
import time

try:

    while True:
        #put your  processing code here
        print('tick')
        time.sleep(5)   #sleep for 5 seconds

except KeyboardInterrupt:

    #This gets triggered on CTRL-C from the keyboard
    #Put your clean-up code here
    sys.exit()

If you wanted to keep all your data in one place you could set up a sqlite database.

Thanks for the suggestion about the sleep() fn, but that wasn't the problem.

I use the sleep() fn in one of my four modules (the one that regularly scrapes prices data). This outputs the data to a growing list in a window on screen. (Note: I use a similar fn, after() in another module as, unlike sleep(), this does not suspend the prog while it waits, and so allows other processes to continue.)

A second module simply displays streamed graphs; while a third prog is event-driven and waits for me to click action buttons or provide stop-loss limits, etc. This one also regularly checks live prices to see if my limits have been exceeded. If they have, the listener initiates action via the fourth module. The third module also prints status messages to a separate window on screen.

The four programs all work fine, but it is the fact that they are all doing different things concurrently, and displaying info in different windows, that led me to the idea of using four instances of Idle. This provides a window for each instance, of course.

Re: my original question, and your suggestion to use Autoit, I had a small breakthrough yesterday. I noticed Autoit has a function, WinGetCaretPos(), which gives coords of the flashing caret in the active window. I have successfully used this to provide an 'origin' from which to calculate further cursor movements needed to get to buttons, etc. This has allowed me to locate the cursor accurately, wherever the Idle shell window is on screen; I am hopeful that, using this approach, I will be able to automate the whole of my start-up routine.

Thank you for your time and help, and particularly the Autolt suggestion. I think we have cracked it.
Peter.W

One final suggestion. You could use wxPython to create an app with multiple frames and use a timer for each separate function. Each function could maintain its output in a dedicated frame/panel containing a multi-line textbox. Using this approach would allow you to trigger on timers as well as buttons/menus.

There are several wxPython tutorials on this site as well. I recommend wxPython over tkinter.

That's interesting. Thanks, I'll investigate it.
Peter.W

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.