Hi, I am not a python newbie, but I am by no means an expert or a tech artist. I would say I am more on the power user side. So I work mostly as an 3D Artist who can write his own tools. I have been banging my head on a problem for about too days now and I think I just need some help.
I am trying to execute and external Python script from the Maya script editor in its own command prompt using subprocess
Code exectued in Maya script Editor
import subprocess
import os
subprocess.Popen([‘C:\Python27\python.exe’,‘C:\Scripts\a.py’],shell=True)
The script executes and I hear a beep but the a.py file does not execute. This is the basic code that is happening in the a.py file
Simple creation of a txt file
import os.path
save_path = ‘C:/Scripts/’
name_of_file = raw_input(“What is the name of the file: “)
completeName = os.path.join(save_path, name_of_file+”.txt”)
file1 = open(completeName, “w”)
toFile = raw_input(“Write what you want into the field”)
file1.write(toFile)
file1.close()
So the confusing thing is it runs in PyCharm IDE but not in Maya. I must be making a simple mistake that is usually the case. Any help would be appreciated.
Try it without the shell=True, by default Maya hides the console window when that flag is present, so its very possible you’ve been spawning the process, but couldn’t actually see it to interact with it. subprocess.Popen([ r‘C:\Python27\python.exe’,r‘C:\Scripts\a.py’])
In general you only need shell=True if you’re trying to run shell commands, which on windows would be things like echo, dir etc…
But it pops open a terminal window, asks me to type a thing, and then prints it back out to the shell, and because I’ve got -i in there, the python interpreter keeps running.
It did the same thing all the other options I have tried did. It would execute and beep and I would get nothing. But you mentioned using Python 3 so I tried that. This time the window stayed up and I could see what was going on.
So I had the syntax correct this whole time, and I am sure all the other examples people have posted are correct as well. The actual problem was two fold I ended up having a Permissions issue with the C:\Scripts folder itself and then the a.py file had a double a.py.py but I didn’t know it because I had no output window. Painful lesson… Thanks again everybody
Cheers