How can I get the result from R program with Python? - python

I have the R program which result I want to get with Python. First, I have tried this:
import subprocess
subprocess.call ("C:\py\test.R", shell=True)
But this program returns 1, and it is not that I`d like to get. Also I tried to install rpy2, but I had error with its installing.
Then I tried this:
import subprocess
command = ("C:\R400\bin\Rscript.exe " "--vanilla C:\py\test.R")
subprocess.Popen(command)
variable_name = subprocess.check_output(command)
print(variable_name)
But this code (Jupyter Notebook and IDLE Python 3.8 64 bit on Windows 10) gets an error:
FileNotFoundError
What could be the reason? How can I fix it? How can I run a R program with Python, but without using rpy2?
Also I tried with cmd. I create a file run.py in the folder with test.R:
import subprocess
subprocess.check_call(['Rscript', 'test.R'], shell=False)
But where was nothing when I tried python3 run.py with cmd.
Also I failed with python3 run.py > testout.txt when I tried to write the result down in the special textual file.

Related

Run matlab code through subprocess module in python

I have a matlab code which I would like to call and execute via subprocess in my python
code. To run the matlab code via the terminal I have
matlab2021a -nodesktop -r "addpath('path/to/dependencies'); matlab_file 1 1 0.5, exit"
To replicate the same in my python script I do the following:
#myscript.py
import subprocess
subprocess.run(["matlab2021a",
"-nodesktop",
"-r" , "addpath('/path/to/dependencies');", "/path/to/matlab_file", "1" , "1" ,"0.5", "exit"])
with the above code I end up within the matlab terminal >>> and I think my python script doesn't call the matlab file.
Can someone suggest the correct way to call and execute a matlab code while adding path for dependencies.

Execute commands from python prompt using os.system

I am trying to execute a few commands using python shell to import a module ABC. For that I have created a python file test.py having the following lines:
import os
os.system('python')
os.system('import ABC')
Now I am trying to execute that file as follows - python test.py. Here, it runs till the 2nd line i.e. os.system('python') properly. Then when it enters into the python prompt and there it remains stuck as it is not command prompt any more but python prompt. So how to execute the next command of importing the module ABC on the python prompt?
Command line and environment (Python Documentation)
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
In your terminal, when you run python followed by the name of a script (test.py), you are executing the contents of the script. Within that script, when you call 'python' using os.system('python'), you are starting a new interactive session, similar to if you were to just call 'python' from your terminal.
import os
os.system('python -c "import ABC"')
You can use the -c argument with python.
For example,
import os
os.system('python -c "import ABC"')
It sounds like what you need is to put the commands you require in a Python script, e.g. my_imports.py:
import ABC
import my_module
And run it from interactive Python with something like:
exec(open('my_imports.py').read())
That will execute the code in the script in the current context, making the imports available to you on the Python CLI.

Run .py script via sh import module error

This is a very basic question on how to code in python and run your script from a very beginner.
I'm writing a script using Xcode9.4.1 which is supposed to be for python3.6. I then have an sh script run.sh, in the same folder of the script (say "my_folder") which simply looks like
python my_script.py
The python script looks like
from tick.base import TimeFunction
import numpy as np
import matplotlib.pyplot as plt
v = np.arange(0., 10., 1.)
f_v = v + 1
u = TimeFunction((v, f_v))
plt.plot(v, u.value(v))
print('donne!\n')
But as I try to run my_script.sh from the terminal I get a "ImportError: No module named tick.base" error.
But the tick folder is actually present in "my_computer/anaconda3/lib/python3.6/site-packages" and up to last week I was using Spyder from anaconda navigator and everything was correctly working, so no "import error" occurred.
The question is quite trivial, in some sense it simply is "what's the typical procedure to code and run python script and how modules are supposed to be imported-downloaded when running on a given machine?"
I need it since my script is to be run on another machine through ssh and using my laptop to make some attempts. Up to last year I used to work in C and only need to move some folders with code and .h files.
Thank for help!
EDIT 1:
From the Spyder 3.2.7 setting, where the script was giving non problem, I printed the
import sys
print(sys.path)
The -manually- copied the content to the sys.path variable in my_script.py and rerun 'run.sh' and now getting a new (strange) error:
Traceback (most recent call last):
[...]
File "/Users/my_computer/anaconda3/lib/python3.6/site-packages/tick/array/build/array.py", line 106
def tick_double_array_to_file(_file: 'std::string', array: 'ArrayDouble const &') -> "void":
^
SyntaxError: invalid syntax
First, check the python which you are calling the script with is pointing to the anaconda python and it is of the same version you are expecting it to be. You can do "which python" command in Linux and Mac to which the path which points to python. It if is pointing to some different version or build of python than the one which you are expecting then add the needed path to the system environment PATH variable. In Linux and Mac this can be done by adding the following line in the .bashrc file at the /home/ folder:
export PATH=/your/python/path:$PATH
And then source the .bashrc file.
source .bashrc
If you are on a operating system like cent os ,breaking the default python path can break your yum so be careful before changing it.
I am running a script in PyCharm and under the Project Interpretor I have the path
C:\envs\conda\keras2\python.exe
When I try to run the script via ssh on the server I get a 'no module named' error. I get
/usr/bin/python as the ans to 'which python' on the server itself. Could you tell me which path I must add for the script to run properly?

Running mayapy on mac

I am trying to run a python script using maya's python interpreter. I am writing this script to be placed in a pipeline, so that maya runs in batchmode. Nothing is happening when I run this command:
maya -batch -script maya.py $1
I get the following message after running this command:
I also tried using the python interpreter directly with a test file
/Applications/Autodesk/maya2017/Maya.app/Contents/bin/mayapy test.py
test.py looks like this
`import maya.standalone
try:
maya.standalone.initialize()
except:
print "standalone already running"`
I get this error ImportError: No module named cmds
I have looked at this post, but it did not help me. What am I doing wrong?

How can I execute commands through python in terminal MAC?

When I manually run this command in Terminal, it executes, but through Python it gives the error that the directory is not available in Python packages.
I am using the following command
source ~/trytry/shell.sh
This is my test shell file:
#!/bin/sh
echo hello
when I executed " source ~/test.sh ", it will print hello at console.
This is my python code:
>>> import commands
>>> commands.getstatusoutput("source ~/test.sh")
(0, 'hello')
It works without any problem. So, would you please show your code?
What it looks like to me is that you have a shell script, and not a python file which would have the .py extension instead of .sh. The error may have to do with the fact that it isn't a python file you're trying to run.

Categories

Resources