Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

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

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

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.