1

In my R script I have to execute an external Python script and I'd use the System() function.

system("python -m premailer -f daily-report.html -o
ready-weekly-report.html")

But after running this in the R console this is what I get

enter image description here

/usr/bin/python: No module named premailer

If I run the python command from the terminal everything works.

EDIT

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
leonardofed
  • 936
  • 2
  • 9
  • 24
  • python module `premailer` doesn't installed? If this is the case, then `pip install premailer` before trying again. – Sixiang.Hu Jan 18 '17 at 23:42
  • `premailer` it's actually installed and it perfectly works from the terminal. It doesn't work when you invoke it from the R script. – leonardofed Jan 18 '17 at 23:44
  • I tried the `system` command provided on win 10 (64bit) R 3.3.2 with Python 3.5.2, it works except cannot find the html files. Guess you are using *nix os? – Sixiang.Hu Jan 18 '17 at 23:49
  • Interesting. But yes, I'm on Unix OS. – leonardofed Jan 18 '17 at 23:51
  • @Sixiang.Hu Here's a screenshot. https://i.stack.imgur.com/bw5HB.png – leonardofed Jan 18 '17 at 23:59
  • em.. not familiar with *nix os. Can only guess is it something to do with environment variable, which tells os where to search for modules? https://scipher.wordpress.com/2010/05/10/setting-your-pythonpath-environment-variable-linuxunixosx/ – Sixiang.Hu Jan 19 '17 at 00:03
  • Following up from the previous comment, does `python -c 'import sys; print sys.path'` return identical outputs when run from the command line and when invoked through `system(...)` in R? – Weihuang Wong Jan 19 '17 at 00:20
  • @WeihuangWong Nope. from System()`['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', ...]` & from cmd line `['', '/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', ...]` – leonardofed Jan 19 '17 at 00:22
  • Run a Unix command from an R `system()`-call to see if the premailer package is located in a directory that your $PATH environment can "see". The $PATH may be different in R than it is in your bask console. You may need to extend your path in your .Rprofile file – IRTFM Jan 19 '17 at 04:59
  • Perhaps some of the suggestions here may help: http://stackoverflow.com/questions/25383030/rpython-using-wrong-python-installation-on-mac-osx, e.g. http://stackoverflow.com/a/37134510/6455166. You probably want the `usr/local/Cellar` version of Python to run when it's called from R, so modify the linked answer accordingly. – Weihuang Wong Jan 19 '17 at 06:50

1 Answers1

0

This is how I fixed this.

Adding the following line to the end of that file, showed me the correct path in Rstudio after restarting my R kernel:

.Internal(Sys.setenv("PATH", paste("/usr/local/bin", Sys.getenv("PATH"), sep=":")))
leonardofed
  • 936
  • 2
  • 9
  • 24