i have this layout
1/mainy.py
2/main.py
3/main.py
........
i wish to run each "main" in its own idle window. not a cmd line because if it crashes i tend to lose the output.
so far i have
for i in range(150):
i+=1 # because theres no zero folder
exec(open(str(i)+"/"+'main.py').read()) # if i run this in idle it tries to run them in the same idle window
i want to have many different idle windows simultaneaously. right now i have to open each manually but i want a script to do it.
i very specfically want each one running in its own idle window so i should have 8 (9 including the one that opens the rest) windows open.
If you don't need to collect output from each idle, you can use subprocess.Popen
from subprocess import *
import os
#Loop over and change script name
for i in range(150):
script_name = os.path.join(str(i), "main.py")
print('script launching:', script_name)
subprocess.Popen("start python " + script_name, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
You can open each Python script in separate window using this:
import subprocess
import time
import pyautogui
for i in range(150):
i+=1 # because theres no zero folder
subprocess.call([ 'C:/Users/tgmjack/AppData/Local/Programs/Python/Python37-32/Lib/idlelib/idle.bat', str(i)+"/"+'main.py'])
time.sleep(2)
pyautogui.press('f5')
time.sleep(2)
Change the first path to match where idle.bat is located and the second according to your Python script.
You can loop through the scripts but you have to use PyAutoGUI package to press F5 to run each script programmatically.
Refer to https://pypi.org/project/PyAutoGUI/
Related
I have a simple tkinter GUI with a dropdown, that when selected, runs a function. I know the anaconda prompt and cmd line are different, however, I can't get the command to work inside the python script without using the external batch file.
I am reading from a folder of csv files to get the list and all that is fine.
Currently, this works:
import tkinter as tk
import os
folder = 'C:/Users/[user]/Desktop'
OptionList = [fname for fname in os.listdir(folder) if fname.endswith('.csv')]
app = tk.Tk()
app.geometry('500x200')
variable = tk.StringVar(app)
variable.set(OptionList[0])
opt = tk.OptionMenu(app, variable, *OptionList)
opt.config(width=500, font=('Helvetica', 12))
opt.pack(side="top")
def callback(*args):
os.system('cmd /k "C:\\Users\\[user]\\Desktop\\notebook_launch.bat"')
app.destroy()
variable.trace("w", callback)
app.mainloop()
Here is the working batch file:
#echo on
call C:\ProgramData\Anaconda3\Scripts\activate.bat
voila "C:\Users\[user]\Documents\notebook.ipynb"
When I try to execute the commands from windows cmd, if I use /k instead of /c to keep the window open, I can see that the anaconda prompt opens, however the next line of code does not execute.
Attempt at getting both commands to run in the script:
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"')
os.system('cmd /c "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
Alternate attempt, same result:
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat" && "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
Noted above, I have also tried using && to combine the commands in one line, but same result, only the anaconda prompt opens and the "voila" command does not execute.
Anyone know what I'm missing or if it possible? My guess is that since the (base) prompt is opening, the os.system function doesn't know what to do with the anaconda prompt. Any help is appreciated.
EDIT:
Turns out I just needed to pause the execution for half a second, as it was executing too quickly. By adding import time and squeezing time.sleep(.5) between the two lines, everything functioned perfectly.
Functioning code:
import time
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"')
time.sleep(.5)
os.system('cmd /c "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
Turns out I just needed to pause the execution for half a second, as it was executing too quickly. By adding import time and squeezing time.sleep(.5) between the two lines, everything functioned perfectly.
Functioning code:
import time
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"')
time.sleep(.5)
os.system('cmd /c "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
I want to make code so that a number goes from 1 to 2 to 3 and so on.
I am running this on Python 3. I have tried:
import time
from sys import stdout
for i in range(1,20):
stdout.write("\r%d" % i)
stdout.flush()
time.sleep(1)
stdout.write("\n") # move the cursor to the next line
But with that code, I get 12345678910111213141516171819.
Can someone help?
EDIT: I ran this code from the console, not from IDLE and it works. Can anyone explain?
For my setup it works (Arch Linux, Python3). Do you start from IDE? Or from console?
Maybe another way works for you:
import time
from sys import stdout
for i in range(1,20):
print(i)
time.sleep(0.1)
stdout.write("\033[F") #back to previous line
stdout.write("\033[K") #clear line
stdout.write("\n") # move the cursor to the next line
Idea found here. This example works in my console but not when I start from PyCharm directly.
Hello everyone I work on a project where I have a raspberry with a Python script that must constantly work but it happens that there are errors and that the script stops with errors and therefore.
this Python script is connected 24 hours a day on the internet and uses a serial communication with electronics and so it happens that there are errors and that it stops the Python script
I am looking for a solution to restart automatically the python file as soon as there is an error or the script stops! maybe another script python checking this script or maybe reboot the systeme .
You could take a look at the following link:
The gist is:
#!/usr/bin/python
from subprocess import Popen
import sys
filename = sys.argv[1]
while True:
print("\nStarting " + filename)
p = Popen("python " + filename, shell=True)
p.wait()
I run the python script using terminal command
python3 myScript.py
It's simply run my program but if i want to open python console after complete run of my script so that i can access my script's variables.
So, What should i do ? and How can i get my script's variables after run the code using terminal ?
Open a python terminal (type 'python' in cmd);
Paste this (replace 'myScript.py' with your script filename):
def run():
t = ""
with open('myScript.py') as f:
t = f.read()
return t
Type exec(run()). Now you will have access to the variables defined in myScript.py.
I needed to do this so I could explore the result of a request from the requests library, without having to paste the code to make the requests every time.
Make the program run the other program you want with the variables as arguments. For example:
#program1
var1=7
var2="hi"
import os
os.system("python %s %d %s" % (filename, var1, var2))
#program2
import sys
#do something such as:
print(sys.argv[1]) #for var1
print(sys.argv[2]) #for var2
Basically, you are running program2 with arguments that can be referenced later.
Hope this helps :)
I've been working on a filebrowser application, and I'd like the first column (file name) to be resized properly on startup. I can type the following code at the python prompt and the column resizes properly, but when I put it in a file and try to run it, the column is not resized. Any idea why?
#!/bin/env python
import sys
import os
from PyQt4.QtGui import *
app = QApplication(sys.argv)
treeView = QTreeView()
fileSystemModel = QFileSystemModel(treeView)
rootDir = fileSystemModel.setRootPath(os.path.expanduser('~'))
treeView.setModel(fileSystemModel)
treeView.setRootIndex(rootDir)
treeView.setGeometry(100,100,1024,768)
treeView.show()
treeView.resizeColumnToContents(0)
app.exec_()
Of course, when I copy it to the python prompt, I leave off the app.exec_(). Is that what is causing the column to not resize? (EDIT: I copied "app.exec_()" to the prompt and it did pretty much what you'd expect - the event loop started, and I was able to use the app, then close it, and then I was returned to the python prompt.)
It seems that replacing the call to treeView.resizeColumnToContents(0) with treeView.header().setResizeMode(0, QHeaderView.ResizeToContents) results in the column being expanded when run from the Python prompt and from a script. I have no clue why resizeColumnToContents is not working as intended.
Side note: should #!/bin/env python be #! /usr/bin/env python? At least on my distro, /bin/env doesn't exist.