Need Help... Having trouble Getting subprocess to work in Maya Script editor

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.

Thanks

Maya does weird things with stdout and stdin, which could be messing with the raw_input calls in your script.

Any ideas on what I could try next?

Does this need to be run as a subprocess?

I am not sure there is another way to launch a Python script in its own cmd prompt, I need the script process to run outside of maya on its own.

\a is an escape character for “play the beep”

flip your path seperators to unix style
subprocess.Popen([‘C:/Python27/python.exe’,‘C:/Scripts/a.py’],shell=True)

or use raw string input
subprocess.Popen([ r‘C:\Python27\python.exe’,r‘C:\Scripts\a.py’],shell=True)

I have tried that before, it doesn’t work. the a.py script never runs.

Things to try:

Is the python executable added to your PATH environment variable? - How to Run a Python Script via a File or the Shell

you could also try:

python r"path-to-file.py"

# or

execfile(r"path-to-file")

Finally from this thread How to use subprocess.Popen() with maya ? they suggest using call instead:

subprocess.call([r"path", r"path"], shell=True)

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…

Nope that doesn’t work either…

Does nothing pop up at all?
Try running it with -i, this should leave the python interpreter running once you’re done.

subprocess.Popen([r'C:\Python27\python.exe', '-i', r'C:\Scripts\a.py'])

This works locally for me, keeping in mind that input in python3 is raw_input from python2.

subprocess.Popen(['py', '-i', '-c', "print('type something:');print('hmm?? {0}'.format(input()))"])

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.

That did it! Thanks Bob : )

When I tried your new line I got the same error

subprocess.Popen([r’C:\Python27\python.exe’, ‘-i’, r’C:\Scripts\a.py’])

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 :slight_smile:

Awesome.
Glad you got it worked out!