Run bash scripts from python. MAC OS - python

I am trying to run scripts for python in MAC OS.
I was unable to run scripts that run Bash.sh scripts, any one here have an idea about how can I run bash.sh From python in Mac OS.
Thanks you for any help :)!
(This is the scripts:
Called: RunScript.sh, and I need to run it from Python Script.)

Does os.system not work?
import os
os.system("/path/to/script.sh")

Presumably you would use the subprocess module.
The questions become:
Do you intend to capture output from this script?
Do you intend to feed input into it?
Does it need to interact with the user via the terminal (that Python is using)?
Do need to capture and or deal with error messages or error codes (return values)?

Related

I have python script which included some bash commands in os.system(). Will this work in Windows?

I have a python script which included some bash commands in os.system() method .
If I convert this python script to exe using Pyinstaller, will this exe file work properly in windows OS or will I face any issues since Windows can't run bash commands ?
the bash commands include pdftk utility.
Example : pdftk input_pdf output output_pdf userpw password
Should I install pdftk utility also in Windows.
What should I do or install to make it work in Windows ?
Please help me..
Thank you
It won't work, os.system is os specific, in Windows it will just spawn a cmd process and try to execute that command and cmd != bash.
Edit: powershell has a lot of common bash commands implemented on windows, you could try to figure out in the code what os are you running on and if powershell supports your bash commands you could use the subprocess module to spawn powershell processes
It probably will not work, from what I have seen when using bash commands inside of code on windows.
Solutions:
Change the commands to commands that work on Windows.
Use some kind of python api (if you know of one post it in the comments and I will put it here.) that allows you to use the commands you need.
Simply run the script using the bash terminal on windows, but you won't be able to make it a exe as far as I know.

How to use a python IDE as a substitute for command prompt?

It is possible to open a program on your pc using the command prompt. Is there a way to use a Python IDE or an idea for code that helps me do that?
You can use:
import os
os.startfile("application_name.exe")
Or if you want to run external applications too, you need to check the path for this application.
import os
os.chdir(r'C:\program files\programfolder')
os.startfile("application_name.exe")

Call alias using a Python Script

I'm writing a python script that utilizes Ureka (a distribution of different astronomy packages). In order to run any Ureka's packages, the user must first initialize Ureka by typing "ur_setup" in the terminal. It turns out "ur_setup" is an alias for the following command:
'eval `/Users/rem/.ureka/ur_setup -csh \!*`'
How would I be able to include this in my python script and have it work?
Thanks in advance!
Use os.execv if you need to take over the running process else use subprocess.
I don't know about ureka, but for running any expression or command using python, you can certainly use the os module.
import os
os.system('eval `/Users/rem/.ureka/ur_setup -csh \!*`')
This Should work.

Getting Python error "from: can't read /var/mail/Bio"

I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell

How to start child cmd terminals in separate windows, from python script and execute scripts on them?

I have been trying rather unsuccesfully to open several terminals (though one would be enough to start with) from say an ipython terminal that executes my main python script. I would like this main python script to open as many cmd terminals as needed and execute a specific python script on each of them. I need the terminal windows to remain open when the script finishes.
I can manage to start one terminal using the command:
import os
os.startfile('cmd')
but I don't know how to pass arguments to it, like:
/K python myscript.py
Does anyone have any ideas on how this could be done?
Cheers
H.H.
Use subprocess module. Se more info at. Google>>python subprocess
http://docs.python.org/2/library/subprocess.html
import subprocess
subprocess.check_output(["python", "c:\home\user\script.py"])
or
subprocess.call(["python", "c:\home\user\script.py"])

Categories

Resources