Running Rscript via Python using os.system() or subprocess() - python

I am facing problems running a Rscript via Python using os.system() or subprocess().
Using os.system() to run commands via python works generally fine for me (e.g. with gdalwarp.exe) but not with Rscript.exe.
The only difference I can see are spaces in the path.
Avoiding problems with spaces in the path are easy overcome in the CMD-window by putting the paths in quotation marks.
Executing the following command is successfull.
"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"
But I am stuck with Python.
What I tried so far with python:
os.system("C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r'"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"')
subprocess.call([r'C:/Program Files/R/R-3.0.2/bin/Rscript.exe', r'D:/.../otsu_Script.R'])
Does anybody see what I am doing wrong?
Thanks in advance,
Eike

After getting mental on such a simple problem. I decided to reinstall RStatistics to a path with no spaces or points, like:
C:/R/bin/Rscript.exe.
Now
subprocess.call(["C:/R/bin/Rscript.exe", "D:/otsu_Script.R"] ) or
os.system("C:/R/bin/Rscript.exe D:/otsu_Script.R") are working just fine.
Should have tried it two days ago...
... but now I am a happy monkey anyway :-)

It probably is way too late now and I have seen you solved the issue, but I was having a similar issue (although in a Linux system) and it might help someone else now; this command was not working when called inside python although it worked directly on the terminal/command-line.
os.system("R CMD BATCH ./read_lengths_batch.R")
I tried many solutions, including subprocess and others but found it to be easier than that. In my case, and I understand it might be different in Windows, I just had to add a & at the end of the call for it to run in the background. Somehow it seemed R would shut down with the Python script instead of doing its work.
os.system("R CMD BATCH ./read_lengths_batch.R &")
Strangely, it was also working if in my folder I would have the same file copied with a .txt extension: read_lengths_batch.R and read_lengths_batch.txt.
Hope it helps someone!

Related

os.listdir() returns nothing, not even an empty list

During a presentation yesterday I had a colleague run one of my scripts on a fresh installation of Python 3.8.1. It was able to create and write to a csv file in his folder (proof that the csv library was working correctly), but everything else failed due to not being able to find the needed files. To try and isolate the problem and figure out why, we tried the below simple script, which also failed.
He had this test.py script in "D:/TEST", which also contained some folders and image files. Running this script printed nothing to the console. No empty list, no error message, no newline. Maybe the print() function was also not working, but I didn't get around to testing that.
import os
print(os.listdir())
This script works fine on my computer and my other colleagues computers (all Windows 10, similar hardware). I didn't have time to look into the issue more thoroughly and don't have access to his computer anymore. What could be the problem? What other things could I have him look into in order to fix this? In case this problem appears again during a future presentation, what steps could I take to figure out the cause of it?
My colleague uninstalled Python and reinstalled it. After doing this apparently the "python" command will no longer run his scripts, but using "py" instead will. Now that he is using "py" to run his scripts, it is working as expected.

Problem running a R script from Python (Windows)

I've recently started programming in python for my job, so I'm quite new to it. My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3).
The problem comes when I have to call the R interpreter from python to run an R file that is generated (run.R file). The curious thing is that this only happens when I try to run my script in Windows, not in Linux. In both cases, I am trying to use the os module from python.
​
This is the code that is not working for Windows:
os.chdir(outRW) #first I change the working directory to the one where the run.R file is
os.system("C:\R-3.6.1\bin\Rscript run.R")
When I execute this, it changes the directory successfully, but when it comes to calling the R interpreter, it shows me this error:
The filename, directory name, or volume label syntax is incorrect.
However, I have tried running the "C:\R-3.6.1\bin\Rscript run.R" command in the Windows Command Prompt and it works perfectly.
I have also tried adding the path to R to the environmental variables, but again I could only make it work in the Command Prompt, not with python.
I guess there is something very obvious that I am missing here, but I cannot see it.
Any help or comments are very much appreciated!
Thank you!
Use double backslashes.
In R you need to use double backslashes \\, otherwise it'll try to interpret it as an Escape Character.
Use this and it will work:
os.system("C:\\R-3.6.1\\bin\\Rscript run.R")
Hope this helps.

impala shell, shell command with capitals

I've got a lot of commands running in impala shell, in the middle of them I now have a need to run a python script. The script itself is fine when run from outside the impala shell.
When I run from within the impala shell using ! or "shell" (documentation found here) it changes the commands to be fully lower case.
The path to the script itself would be something like this: /home/DOMAIN_USERS/somemorefolders/python/script.py
so in my impala shell I'm running: !/home/DOMAIN_USERS/somemorefolders/python/script.py
the error I get back is
sh: /home/domain_users/somemorefolders/python/script.py: No such file
or directory
Is there any way to force it to not make it into lower case? I've tried putting both single & double quotes round the path but that makes no difference.
I guess if there's no way I'll have to come out of the impala shell, run the python bit then go back in. Its just a bit more work when I figured the "shell" command in the impala shell is there for that exact benefit.
This is caused by a known bug IMPALA-4664.
A workaround is to leave a space after "!". Can you try this (note the space):
! /home/DOMAIN_USERS/somemorefolders/python/script.py
Thanks to #BoboDarph for help in getting there.
I was able to use !~/somemorefolders/python/script.py as I could get there from my home directory.
I still think it's a bit shortsighted of impala to force things into lower case but there you go.

Learning Python the Hard Way terminal setup

So I'm trying to setup the terminal in conjunction with Notepad++ and I'm a little confused. I've managed to download Python 2.7 and load it up in Powershell but I don't really know where to go from there. Do I even need to use a terminal? I know it says you can't use IDLE but it would be a lot easier. If anyone can guide me through getting the right setup to go on with the book I'd appreciate it a lot.
If you managed to get python running in the powershell following the Exercice 0, and you read the "Warning For Beginners" section, you can proceed to Exercice 1.
Why do they recommend that setup? Notepad++ is on Windows a powerful text editor very similar to other text editors, so you will soon get familiar with it. You will need to save your scripts in files and type a special line in the powershell to run and test them. That way you will get accustomed to running programs with arguments and if you make a failure, you can edit your code, save it and run it again.
In IDLE, you are already in a Python environment, so you cannot give arguments to your program inside it (actually you can but it's complex). You can also get an editor, save your file and run the script but also here it's a harder and less obvious way than Notepad++. When you get comfortable with the environment in that book you may change to get something that suits better your way of doing.

Python code runs fine from command line, but won't work from IDLE

I've found a lot of people having the reverse issue, but haven't yet found a question that involves IDLE not being able to run something that runs fine from the command line. I'm using a new module that I haven't used before that uses one .pyd file and one .dll, and involves a device that connects through USB. I sadly can't post in-depth code snippets since this is copyrighted code, but if anyone knows where to start on a problem like this I would be very grateful.
IDLE swaps out the sys.stdout and sys.stderr objects at the Python level this causes issues with some pyd modules. Try using another debugger.

Categories

Resources