Execute /bin script from other python script - python

I have a script in my project's bin directory, and I want to execute it from a cron. Both scripts are written in python.
Target file :
App_directory/bin/script_name
Want to execute script_name script with some parameters from App_directory/cron/script_name1.py
How do I achieve that ?

Try:
import os
os.system('/path/to/App_directory/bin/script_name')
Or if script_name is not executable and/or doesn't have the shabang (#!/usr/bin/env python):
import os
os.system('python /path/to/App_directory/bin/script_name')

The subprocess module is much better than using os.system. Just do:
import subprocess
subprocess.call(['/path/to/App_directory/bin/script_name'])
The subprocess.call function returns the returncode (exit status) of the script.

It works for me...
import subprocess
process = subprocess.Popen('script_name')
print process.communicate()

Related

How to redirect stdin/stdout/stderr when replacing process using os.execl

Consider the following sample script:
import os
import sys
print(1)
os.execl(sys.executable, sys.executable, '-c', 'print(2)')
print(3)
The result is
1
I was expecting
1
2
I think it is because the replacing process is not using the same stdin/stdout/stderr?
How can I achieve what I was expecting for while using execl?
I'm using Python 3.6 on Windows.
This is not a bug about PyCharm as I cannot reproduce it with IDEA too. IDEA is using the same core as PyCharm using.
This is because of the way you launch your script. If you launch your script with Run, it works. If you launch it with Debug, it doesn't.
Because Run just run script in a terminal, but Debug will launch a debugger and connect that process to this debugger. The output you see is actually from debugger but not directly from your script. When you replace your process, debugger won't rebuild connection to that new-created process.
That's why you didn't get 2 outputted.
In Linux,there is a flag FD_CLOEXEC,you can test it by fcntl.fcntl(sys.stdout,fcntl.F_GETFD)
the behavior you described can reproduce in ubuntu16 by
import os
import sys
import fcntl
print(1)
ret = fcntl.fcntl(sys.stdout, fcntl.F_GETFD)
ret |= fcntl.FD_CLOEXEC
fcntl.fcntl(sys.stdout, fcntl.F_SETFD, ret)
os.execl(sys.executable, sys.executable, '-c', 'print(2)')
print(3)
So when you run in PyCharm,it must redirect the stdout and set the equivalent windows flag.

python: how to run a program with a command line call (that takes a user's keystroke as input) from within another program?

I can run one program by typing: python enable_robot.py -e in the command line, but I want to run it from within another program.
In the other program, I imported subprocess and had subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e']), but I get an error message saying something about a callback.
If I comment out this line, the rest of my program works perfectly fine.
Any suggestions on how I could change this line to get my code to work or if I shouldn't be using subprocess at all?
If enable_robot.py requires user input, probably it wasn't meant to run from another python script. you might want to import it as a module: import enable_robot and run the functions you want to use from there.
If you want to stick to the subprocess, you can pass input with communicate:
p = subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e'])
p.communicate(input=b'whatever string\nnext line')
communicate documentation, example.
Your program enable_robot.py should meet the following requirements:
The first line is a path indicating what program is used to interpret
the script. In this case, it is the python path.
Your script should be executable
A very simple example. We have two python scripts: called.py and caller.py
Usage: caller.py will execute called.py using subprocess.Popen()
File /tmp/called.py
#!/usr/bin/python
print("OK")
File /tmp/caller.py
#!/usr/bin/python
import subprocess
proc = subprocess.Popen(['/tmp/called.py'])
Make both executable:
chmod +x /tmp/caller.py
chmod +x /tmp/called.py
caller.py output:
$ /tmp/caller.py
$ OK

Launch python script with abaqus command

I have a command file (.cmd) which I use to launch Abaqus command line windows.
Then, I use the command 'abaqus python test.py' to launch python command inside Abaqus.
Now, I would like to use a python script to do that.
I try something like this but doesn't work. Someone know the trick ?
Thanks !!
import subprocess
AbaqusPath=r"C:\Abaqus\script\abaqus.cmd"
args= AbaqusPath + "-abaqus python test.py"
subprocess.call(args)
Using .cmd-file:
This way might work with cmd file:
abaqusPath = "C:\\Abaqus\\script\\abaqus.cmd /C"
args = AbaqusPath + "abaqus python test.py"
subprocess.call(args)
Flag /C is needed to run command and then terminate.
Easiest way:
Just add the folder with abaqus commands (typical path C:\Abaqus\Commands) into the PATH variable of the system. This will give the access to commands like abaqus, abq6141 etc. in cmd directly.
When just use the following in your script:
subprocess.call("abaqus python test.py")
Using .bat-file:
If the configuration of PATH variable is impossible and the first way does not work, .bat-files from abaqus can be used as follows:
abaqusPath = "C:\\Abaqus\\Commands\\abaqus.bat "
args = AbaqusPath + "python test.py"
subprocess.call(args)
I've never had any success using just string arguments for subprocess functions.
I would try it this way:
import subprocess
abaqus_path = r"C:\Abaqus\script\abaqus.cmd"
subprocess.call([abaqus_path, '-abaqus', 'python', 'test.py'])

Can I open an application from a script during runtime?

I was wondering if i could open any kind of application in Python during runtime?
Assuming that you are using Windows you would use one of the following commands like this.
subprocess.call
import subprocess
subprocess.call('C:\\myprogram.exe')
os.startfile
import os
os.startfile('C:\\myprogram.exe')
Using system you can also take advantage of open function (especially if you are using mac os/unix environment. Can be useful when you are facing permission issue.
import os
path = "/Applications/Safari.app"
os.system(f"open {path}")
Try having a look at subprocess.call http://docs.python.org/2/library/subprocess.html#using-the-subprocess-module
Use the this code : -
import subprocess
subprocess.call('drive:\\programe.exe')
Try this :
import os
import subprocess
command = r"C:\Users\Name\Desktop\file_name.exe"
os.system(command)
#subprocess.Popen(command)
Of course you can. Just import import subprocess and invoke subprocess.call('applicaitonName').
For example you want to open VS Code in Ubuntu:
import subprocess
cmd='code';
subprocess.call(cmd)
This line can be also used to open application, if you need to have more information, e.g. as I want to capture error so I used stderr
subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
Some extra examples for Windows, Linux and MacOS:
import subprocess
# Generic: open explicitly via executable path
subprocess.call(('/usr/bin/vim', '/etc/hosts'))
subprocess.call(('/System/Applications/TextEdit.app/Contents/MacOS/TextEdit', '/etc/hosts'))
# Linux: open with default app registered for file
subprocess.call(('xdg-open', '/tmp/myfile.html'))
# Windows: open with whatever app is registered for the given extension
subprocess.call(('start', '/tmp/myfile.html'))
# Mac: open with whatever app is registered for the given extension
subprocess.call(('open', '/tmp/myfile.html'))
# Mac: open via MacOS app name
subprocess.call(('open', '-a', 'TextEdit', '/etc/hosts'))
# Mac: open via MacOS app bundle name
subprocess.call(('open', '-b', 'com.apple.TextEdit', '/etc/hosts'))
If you need to open specifically HTML pages or URLs, then there is the webbrowser module:
import webbrowser
webbrowser.open('file:///tmp/myfile.html')
webbrowser.open('https://yahoo.com')
# force a specific browser
webbrowser.get('firefox').open_new_tab('file:///tmp/myfile.html')

subprocess.Popen running infinite loop with py2exe

I'm trying to use py2exe to compile a python script into an executable.
I've set up the setup.py file just like it's described in documentation:
from distutils.core import setup
import py2exe
setup(console=['agent.py', 'test.py'])
The agent.py file simply uses subprocess.Popen to open another script:
import sys
import subprocess
print 'is this working?'
child = subprocess.Popen([sys.executable, 'test.py'])
The test.py file is
while 0 == 0:
print 'test'
When running this as a python script, it works fine. When running as a py2exe-compiled executable, it does not run.
When I try to change the file reference in agent.py from 'test.py' to 'test.exe', running the compiled agent.exe simply prints 'is this working?' on an infinite loop. What have I done wrong?
sys.executable points to agent.exe instead of python.exe when run as compiled executable. You need to change your Popen to:
child = subprocess.Popen(['test.exe'])
when running compiled executable. You can use hasattr(sys, "frozen") to determine whether you're in frozen (py2exe) or not (Python script) mode.
That didn't quite work, but all I had to do was replace your answer with the full path name. Thanks! This worked:
app_path = os.path.realpath(os.path.join(
os.path.dirname(sys.executable), 'test.exe'))
child = subprocess.Popen(app_path)

Categories

Resources