Using Bash to run 2 python scripts at the same time - bash

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.

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.

Running Python Function from Bash

I have created a function configure_propeties() in configure.py that I'm trying to call in a bash script using the following command:
python3.6 -c "import configure; print configure.configure_properties()"
After executing the the bash script I get the following warning:
warning
Both the script and configure.py are in the same directory. Not sure what's happening it doesn't seem to fail on the import. Has anyone seen this issue before?
print "blabla" its only available in python 2, with python 3 you have to use parentheses print("blabla"), it would be better to set your python script inside .py file, just make sure the python executable is in your PATH environment variable then add in your script
python3 path/to/the/python_script.py
In the bash script file bashFile.sh, put this :
#!/bin/bash
python3 path/to/the/python_script.py
then you can execute your bash file bashFile.sh :
bash path/to/bashFile.sh
Use parentheses for print, as is necessary for Python 3

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.

Importing a ruby file (or) code in python script

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?

Resources