1

EDIT: It turns out the shell that the subprocess module does not have the same directories assigned to the PATH variable as my system's shell. Thus the solution is simply to either call Maxima using the full path (found through which maxima) or changing env={'PATH':maxima_paths} in the subprocess.run() parameters. As @RobertDodier pointed out, this is not an issue with Maxima specifically.

I have a file called testMaxima.txt that contains the following Maxima code:

write_data([[1,2,3]], "test.txt");

This Maxima code creates the file test.txt in the working directory.

I am looking to run this file using a Python script. I do not want to open Maxima (a math engine) manually in order to do this.

What I've tried: I know that I can run this file in Command Line/Terminal using maxima -b testMaxima.txt and I have tried to use the python module subprocess to emulate this code with the following python code:

import subprocess
subprocess.run("maxima -b testMaxima.txt", shell=True)

but this does not create the new test.txt file like it should. If this helps, when I run

>>> subprocess.call("maxima -b testMaxima.txt", shell=True)
127

I get an output of 127. From what I understand, this means that the terminal can't find the given commands. However, there is no issues when I execute the code maxima -b testMaxima.txt through the terminal directly, so I'm not sure why it cannot find the given commands. Does anyone have any thoughts on what I should try?

Thank you in advance!

EDIT: From @mkrieger 's advice, I am using a string "maxima -b testMaxima.txt" instead of the sequence ["maxima", "-b", "testMaxima.txt"]. Unfortunately, this does not make a difference.

  • If you pass the command as a list to `subprocess.run` then you need to use `shell=False` (which is preferred). If you have to use `shell=True` for some reason, then you need to pass the command as a single string. – mkrieger1 Aug 01 '20 at 21:45
  • 1
    Does this answer your question? [subprocess.call using string vs using list](https://stackoverflow.com/questions/15109665/subprocess-call-using-string-vs-using-list) – mkrieger1 Aug 01 '20 at 21:47
  • @mkrieger1 Unfortunately, using a string does not change the result--it still does not produce the desired output. – Oliver Cheng Aug 01 '20 at 21:54
  • @mkrieger1 Also, setting ```shell=False``` does not make a difference. Thank you for the ideas though! – Oliver Cheng Aug 01 '20 at 21:58
  • Hm, it works for me as shown. I'm working on Linux. Couple of ideas. (1) Maybe it helps to put the full path of the maxima executable? `/usr/local/bin/maxima` or whatever. I guess `which maxima` will say. (2) Can you execute other programs via `subprocess` or `os.system`? Maybe the problem is not with Maxima specifically. Hope this helps. – Robert Dodier Aug 03 '20 at 05:01
  • For the record, I've tried it on macOS Catalina 10.15.5 and it works as shown. I installed Maxima via Homebrew; dunno if that makes a difference. – Robert Dodier Aug 03 '20 at 19:54
  • @RobertDodier Oh! You are correct. I changed the subprocess.run() env variable to ```env = {'PATH':path_to_maxima_and_everything_else}``` and it works great. Thank you so much! EDIT: Using the full path also works--as you mentioned this is not Maxima exclusive. – Oliver Cheng Aug 04 '20 at 01:33

1 Answers1

0

how about using os.system in python.

import os
os.system('maxima -b testMaxima.txt')

Also make sure that you are running this python script in the location where testMaxima.txt file is present

Alpha Green
  • 98
  • 10
  • Unfortunately, this returns the error ```32512``` which seems similar to the error 127 when I run subprocess.call EDIT: Also yes--the script is in the same location. – Oliver Cheng Aug 01 '20 at 21:52
  • In my case i am getting ***test.txt*** file generated by using both mine or your code. Also i have checked the ***test.txt*** file which has data as ***1 2 3*** inside. – Alpha Green Aug 01 '20 at 22:41
  • Interesting! I wish I had your machine then haha. The file is exactly what it should be... My guess right now is that maxima can't be accessed within the python that I am using. I fear this is because I am using MacOS Catalina 10.15. If you don't mind: is there some reference to a maxima application inside your ```PATH``` variable if you access your shell's variables inside the python console? If so, what is the directory? – Oliver Cheng Aug 01 '20 at 23:01