5
$\begingroup$

What is the correct way to provide Mathematica with a path to Python user scripts?

Simply executing Python as a shell command, using something like

usefulOutput = Import["!python -c 'import mypkg; mypkg.use()'", "Text"];

fails, because Mathematica — like many IDEs and Python launchers — has no access to (or at least ignores) PYTHONPATH or any sys.path.append in 'shell_config.py'. I seem to be able to Run shell scripts without errors, but that's not what I'm looking fore here.

Does Mathematica provide a setting for extending python.path or is there a related directory setting in Mathematica that would result in my scripts being found?

$\endgroup$
3
  • $\begingroup$ stackoverflow.com/questions/19917492/how-to-use-pythonpath possibly useful to you or others trying to help $\endgroup$
    – george2079
    Commented Feb 1, 2014 at 14:33
  • $\begingroup$ look at os.environ to see what you are getting.. $\endgroup$
    – george2079
    Commented Feb 1, 2014 at 14:49
  • $\begingroup$ @george2079: Sorry, my example was terrible. Fixed. $\endgroup$
    – orome
    Commented Feb 1, 2014 at 15:01

1 Answer 1

2
$\begingroup$

This may be useful..:

Prior to launching mathematica from a linux bash shell:

> NEWENV=WHATEVER
> export NEWENV
> mathematica

Now in the notebook:

 env = Import["!python -c 'import os;print os.environ'","Text"]

returns the whole environment as one big string. Now find the one I just made up..

Select[First@ImportString[StringTake[env,{2,-2}],"CSV",StringMatchQ[#,"*NEWENV*"]& ]

->

{ 'NEWENV': 'WHATEVER' }

Use the same approach to see if PYTHONPATH is even set.

You can also set an environment variable on the fly like this:

Import["!NEWENV=XX\nexport NEWENV\npython ... ","Text"]

Note that mathematica seems to launch a bourne or bash shell even if launched from some other shell.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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