so I'm running into a strange problem using Import[] to import the output of a python 3.6 script.

I have a script my_script.py which just contains print('Hello, World!').

If from within Mathematica I pass

pythonOutput = Import["!python my_script.py", "String"]

I get the expected

Hello, world!

However, I am on Mac OS where the default installation of python is python 2.7 and so if I want to run my code in python 3.x in the terminal I would have to say

python3 my_script.py (which works fine)

However, when I modify my Mathematica code to say

pythonOutput = Import["!python3 my_script.py", "String"]

I get no output! Does anyone know why this might be the case? The command python3 my_script.py works perfectly fine from the terminal but not within an Import statement.

I have also tried every suggestion from this question and none of them work with the python3 command.

  • $\begingroup$ Does your python script literally contain 'print(Hello, World!)' without quotation marks? With quotation marks, it works using both python and python3 on linux. $\endgroup$ – Felix Jul 15 '17 at 22:10
  • $\begingroup$ No, my python script did not have the outer quotation marks. I tried adding the quotation marks and now it doesn't work with python 2 or 3 $\endgroup$ – Mason Jul 15 '17 at 22:28
  • $\begingroup$ Sorry, that was probably confusing. I meant: Did you write print(Hello, World!) or print('Hello, World!')? $\endgroup$ – Felix Jul 15 '17 at 23:22
  • $\begingroup$ Oh I see, yes I did have the quotes I just mistype in my question. $\endgroup$ – Mason Jul 15 '17 at 23:32
  • 2
    $\begingroup$ Or check Environment["PATH"]. Generally the python3 binary lives at /usr/local/bin/python3. In Mathematica I have Environment["PATH"] being "/usr/bin:/bin:/usr/sbin:/sbin". Furthermore RunProcess[{"python3", ExpandFileName@"~/Desktop/my_script.py"}, ProcessEnvironment -> <| "PATH" -> "/usr/local/bin:" <> Environment["PATH"] |>] works but sans ProcessEnvironment it fails. $\endgroup$ – b3m2a1 Jul 16 '17 at 0:25

To make the path permanently available to Mathematica (frontend) and all other applications, first find out the location of your desired python3 executable (e.g. from which python3 in the Terminal), and then add this to the path variable that is set for all applications when the user logs in. Let's say the executable is in /usr/local/bin as @b3m2a1 mentioned in the comment to your question (it may be somewhere else!). Then do the following (in Terminal):

sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The last four locations are the default entries, and I prepended the new location to that list (separated by :). To make this take effect, you have to restart the computer.

| improve this answer | |
  • $\begingroup$ This worked! Thanks! $\endgroup$ – Mason Jul 16 '17 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.