5

I am trying to run python abaqus through the command prompt using

os.system('abaqus CAE noGUI=ODBMechens')

It doesn't seem to run anything, but if I go to the command prompt myself and type in

abaqus CAE noGUI=ODBMechens

it works. I am using python 2.7 on Windows 10. Thanks

kgui
  • 4,015
  • 5
  • 41
  • 53
Mark Gordon
  • 59
  • 1
  • 1
  • 4
  • 1
    What do you mean by *it doesn't seem to run?* – Stephen Rauch Mar 27 '17 at 02:32
  • You **really** shouldn't use `os.system`. Use `subprocess` instead. – math2001 Mar 27 '17 at 02:32
  • Are there any non-Ascii characters in the working one? The program is in the current directory so you could open the python shell and do `[(name, name.lower().=='abaqus.exe') for name in os.listdir('.')]`. Is the command there? Does it say True? – tdelaney Mar 27 '17 at 02:59
  • That last comment from tdelaney was a little too complicated for me. Do I just copy/paste what you wrote into the console window (I'm using Spyder)? I switched to subprocess as suggested (still not working as shown in comments below). – Mark Gordon Mar 27 '17 at 03:08
  • Stephen - When it runs it takes a while and then generates another file. This "runs" super quickly and doesn't generate the file. – Mark Gordon Mar 27 '17 at 03:21
  • do `which abaqus` at your command prompt to get the full path and use that full path in your system call. ( on windows do `where abaqus` ) – agentp Mar 27 '17 at 14:43
  • That ended up being the problem. I still don't understand why it couldn't find abaqus when the command prompt could, but putting in the full path made it work. Thank you for your help. – Mark Gordon Mar 28 '17 at 05:38
  • I had a similar issue. After a fair amount of digging, I think the issue is that the PATH for my python install is done through Anaconda (this is how I got Spyder) so I have a different path this vs CMD. I fixed this by adding the full Abaqus location to the command. ```subprocess.call(r'C:\SIMULIA\Commands\abaqus job=Job-1 ask_delete=OFF interactive', shell=True)``` – Austin Downey Jul 11 '20 at 21:22

2 Answers2

5

try using the subprocess module (it's newer) instead: for example,

subprocess.call(["ls", "-l"])

and in your example, it would be:

subprocess.call('abaqus CAE noGUI=ODBMechens')

More info on the difference between subprocess module and using os.system call:

The Difference between os.system and subprocess calls

Community
  • 1
  • 1
kgui
  • 4,015
  • 5
  • 41
  • 53
  • 1
    I replaced my command with the second one that you have there, but it gives the following error: WindowsError: [Error 2] The system cannot find the file specified What directory does it use to look for the files? At the command prompt I run it out of a folder on my H drive which is where the python file is. – Mark Gordon Mar 27 '17 at 02:44
  • I just checked "print os.getcwd()" and it has the same folder that I am using in the command prompt. Does the file need to be where python is installed? – Mark Gordon Mar 27 '17 at 02:51
  • No, it doesn't need to be where you installed python. but, maybe try going inside that folder and then running that python command. Try to replicate the exact environment within python as you would in the command prompt. – kgui Mar 27 '17 at 02:53
  • In the command window the following runs: H:\NoSpaces>abaqus CAE noGUI=ODBMechens The python file that I am running is H:\NoSpaces\RunINPfile.py I should note that abaqus is not in that folder (it is a separate executable, but it runs fine from the command prompt) – Mark Gordon Mar 27 '17 at 03:00
  • so you're running that file which has the above python commands? – kgui Mar 27 '17 at 03:01
  • Yeah, it is currently: import subprocess subprocess.call('abaqus CAE noGUI=ODBMechens') – Mark Gordon Mar 27 '17 at 03:03
  • WindowsError: [Error 2] The system cannot find the file specified – Mark Gordon Mar 27 '17 at 03:09
  • are you sure you named the file correctly? and running it through python via: ```python name_of_file``` or ```python ./name_of_python_file``` – kgui Mar 27 '17 at 03:09
  • I was just running it through Spyder (F5 button) but I ran it through the command prompt and got the same error "H:\NoSpaces>python RunINPfile.py" – Mark Gordon Mar 27 '17 at 03:15
  • if you type ```ls``` is that the exact name of the file (including upper and lower case characters)? – kgui Mar 27 '17 at 03:16
  • I'm not exactly sure what you mean. I tried typing ls into the command prompt and it gave errors. I put it into the python console window and it showed everything in the folder (including ODBMechens.py). – Mark Gordon Mar 27 '17 at 03:20
  • I then made sure I had things typed correctly buy coping the command (that works) out of the command prompt and pasting it into the python file but that still didn't work. – Mark Gordon Mar 27 '17 at 03:21
  • After some investigation I believe it is having problems finding abaqus as when change it to "subprocess.call('abaqus')" it gives the same error. However, when I type "abaqus" into the command prompt it starts asking questions about Identifiers and Input Files rather than giving an error. I just want a command that takes exactly what I give it and runs it at the command prompt. – Mark Gordon Mar 27 '17 at 03:45
  • how are you starting python? – kgui Mar 27 '17 at 13:19
  • 1
    I ended up fixing it by adding the full path for abaqus. If you still care, I was running abaqus by loading the file into spyder and running it through there. I believe that I tried running it through the command prompt once too, but I don't remember the state of the code at that time. Thank you so much for your help. – Mark Gordon Mar 28 '17 at 05:40
  • @MarkGordon yup, np. I should of recommended trying the full path! It's a great first start – kgui Mar 28 '17 at 12:54
0

You should add before your code

import os
import subprocess
try:
    os.environ.pop('PYTHONIOENCODING')
except KeyError:
    pass

And then:

cmd = subprocess.Popen('abaqus CAE noGUI=ODBMechens',cwd=jobPath, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE, shell=True).communicate()[0]

Variable cmd contains your output. I found that this way it works.

UN4
  • 587
  • 5
  • 19