Importing a ruby file (or) code in python script - ruby

I had a ruby script which uses few ruby libraries to perform some action and gives an output. I want this ruby script output to be stored as a dictionary in a python script. For this, I want to import the ruby file similar to import python modules. Is there anyway to directly import the ruby file in python so that I can use the functionality of the ruby script inside python program instead executing the ruby script from python using modules like Popen?

Related

cant read python3 library in in nifi executestreamcommand

i want to use ExecuteStreamCommand in nifi for running a python script say script.py
my script has declared #!/usr/bin/python3
but its not able to read library from python3
I am able to run the script as python3 script.py but cant same on ExecuteStreamCommand of nifi
anyone has the solution
Adding on to Matt's answer
You can point directly to the python3 bin from ExecuteStreamCommand
e.g.
Command path: /usr/bin/python3
Command arguments: /path/to/script.py;x;y;
Secondly, you should not use #!/usr/bin/python3 as a Python3 shebang, instead using #!/usr/bin/env python3 - see here
Should I put #! (shebang) in Python scripts, and what form should it take?
In your example above you are not invoking python3 directly, which is what ExecuteStreamCommand expects (the script path should be an argument). The environment used by NiFi may not be the same as the one you use from a Terminal program, and looking into a file for the shebang line is likely a feature of your shell, which may not be available to the NiFi JVM when executing an external process.

How can I load a module from inside a bash script?

I'm trying to get a script running. Essentially I need to load some modules and then execute a python script. So something like this...
#!/usr/bin/env bash
module use /PATH/TO/MODULEFILE
module load MY_MODULE
python my_script.py
The problem that I get is that every time I try to execute the script I get this error - "module: command not found". I don't understand how to change the script to fix this. I am able to run the module command from the shell. I also ran "whereis -b module" and I just got "module:".
What do I need to do to get this module loaded from inside the script? Thanks
You can enable the module command from your python script by sourcing the python.py initialization script provided in the init directory of the Modules installation.
Following piece of python code defines module function, then enables a modulepath and load a modulefile:
#!/usr/bin/python
import os
exec(open('/PATH/TO/MODULES/init/python.py').read())
module('use', '/PATH/TO/MODULEFILE')
module('load', 'MY_MODULE')
If you want to enable the module within your bash script, it could be as simple as using a different command to run the script. For me, running a script script.sh as . script.sh or as ./script.sh will correctly load the module, but running it as sh script.sh will lead to the error you described above.

How to execute a bash script from within my python project

I am writing some automation to control a simulator. I am also using WebDriverAgent along with openatx python bindings in pycharm. There are some things that are fast using cURL in a bash script and some methods that are stronger in the python solution. I want to use a mix of both. I have imported bashSupport into pycharm and would like to execute a bash script from within my file system of the project.
I have tried subprocess import and os but it doesn't seem to be executing my script. Here is an example:
import subprocess
subprocess.call(['launch_wda.sh'])
with device.session('com.apple.mobilesafari') as app:
print app.orientation
app(label="Address").tap()
app(label="Address").set_text("facebook.com \n")
the launch_wda.sh is in my file structure of the project. Is my syntax incorrect or am I missing something else?
Try these changes:
Add ./ before script name. As this could be some path related issue.
Try adding shell=True if you are not under Posix system and you do not have something like this #!/bin/sh as a first line of your bash script.
Last but not the least, check the permissions for the bash script.
Hope this helps.

Using Bash to run 2 python scripts at the same time

I want to run two Python Scripts at the same time. I found that using Bash you can do that. So i wrote the next code
#! /usr/bin/env bash
import camera_centroid
import testsss
python camera_centroid.py &
python testsss.py &
When i run it i get a SyntaxError: invalid syntax
Why?
Looks like you have mixed between python and bash,
you don't need the import in the bash script.
#!/usr/bin/env bash
python camera_centroid.py &
python testsss.py &
wait # wait for jobs to be done
make sure you adding execute permissions to the scripts
chmod +x testsss.py camera_centroid.py
and finally run the script ./your_file.sh
When you write:
import foo
in a shell script, you are not importing the python module, instead, you are calling the $(which import) command/alias/function.
For example, if you have ImageMgick installed, very likely, you are making screenshot for window(s).
If you want to import python module, those import foo should be in your python files.

Use the python interpreter packaged with py2exe

I have a python script I want to distribute to Windows, where people might not have python installed. So I use py2exe. The problem is in the script I run other python scripts by using subprocess, which requires python interpreter as the program to execute. As I don't have python interpreter installed on Windows, is there any way I could ignore the interpreter and work around the problem? Is there any way I could call the python interpreter pakcaged by py2exe?
It's probably more simple than you think: Instead of starting sub-processes, use the built-in eval() command to execute the scripts.
[EDIT] To redirect stdio, replace sys.stdout/sys.stderr with buffers or something else that supports "write()".
To restore the original values, the sys module offers __stdout__, etc.
[EDIT2] I haven't tried this but it might work: Add "python.exe" to the set of files which py2exe creates.
From the main code, copy all files that py2exe created + the python.exe into a temporary directory. Then add all your scripts.
Now start the new python interpreter with a small script that adds the temp folder and library.zip to the sys.path
Note: Python doesn't have to be "installed" like a Windows application. In fact, you can simply copy all the files to a new place. As long as the search path is correct, this works.

Resources