Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ''. 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?

share|improve this question possibly useful to you or others trying to help – george2079 Feb 1 '14 at 14:33
look at os.environ to see what you are getting.. – george2079 Feb 1 '14 at 14:49
@george2079: Sorry, my example was terrible. Fixed. – raxacoricofallapatorius Feb 1 '14 at 15:01

This may be useful..:

Prior to launching mathematica from a linux bash shell:

> 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*"]& ]



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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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