can't execute the blender python through subprocess - python

My blender is at the path of '/home/abc/Destkop/blender/blender-2.78'. When command line is at the path '/home/abc/Destkop/blender/blender-2.78', executing './blender -b /home/abc/Destkop/blender/car.model' through command line is working.
I am trying to execute blender in python through subprocess, but the code doesn't work with an error "/usr/bin/python: can't open file ./blender -b /home/abc/Destkop/blender/car.model", but the path is all right
here is my code
import os
import sys
import subprocess
if __name__="__main__":
os.chdir("/home/abc/Destkop/blender/blender-2.78")
subprocess.Popen([sys.executable],"./blender -b /home/abc/Destkop/blender/car.m

Try this:
subprocess.check_call(["./blender", "-b", "/home/abc/Destkop/blender/car.m"])
Note that the shell arg defaults to False, which means the args must be a sequence (list or tuple) of words making up the command-line, not a single command-line string.

Related

Having trouble using a file as a command-line argument in the same directory as the python script

I'm trying to write a .py script that takes in another file as a command argument, with the cmd-argument file in the same directory as said script.
What I have so far is:
import sys
java_file = open(sys.argv[1],"r")
<rest of file>
Which works fine if I have a command-line argument like this:
python.exe "D:\<user>\<more file directories>\balanced.py" "D:\<user>\<same file directories>\Driver.java"
What I'd like to do is have the command-line argument be something like:
python.exe "D:\<user>\<more file directories>\balanced.py" Driver.java
Running on Windows 10 command prompt using Python 3.7.3.
on Windows, sys.argv[0] should be the full path to your script, so you can use os.path.split(sys.argv[0])[0] to get the path, then os.path.join() to join it
import sys, os
java_file = open(os.path.join(os.path.split(sys.argv[0])[0], sys.argv[1]))

subprocess cannot run python executable in non-interactive mode?

I am using python 3.6.3 and subprocess module to run another python script
# main.py
#!/bin/env python
from subprocess import Popen,PIPE
from sys import executable
p = Popen([executable, 'test.py', 'arg1'],shell=True, stdout=PIPE)
p.wait()
print(p.stdout.read().decode())
and
# test.py
import sys
print(sys.argv)
I expect it will run and execute test.py. However, it opens an python interpreter in interactive mode!
I tested shell=False option, it works. I tested string form rather than list form of args, it works.
I am not sure if it is a bug or not.
You need to remove shell=True or change the first argument to be executable + ' test.py arg1' instead of [executable, 'test.py', 'arg1'].
As explained in the documentation, with shell = True, it will run it as /bin/sh -c python test.py arg1, which means python will be run without arguments.

"Failed to execute child process (No such file or directory)" when calling gnome-terminal subprocess in Python 3 [duplicate]

This question already has answers here:
subprocess.Popen() error (No such file or directory) when calling command with arguments as a string
(2 answers)
Closed 2 years ago.
I am trying to write a program that opens a gnome-terminal window and executes a python file in it.
When I call the gnome-terminal subprocess with the subprocess module like this:
import subprocess
subprocess.call(['gnome-terminal', '-x', 'python3 '+filename])
I get the following error:
Failed to execute child process "python3 /home/user/Documents/test.py” (No such file or directory)
I have tried to cd to the directory /home/user/Documents/test.py first and then run the file, but it didn't work.
You're trying to execute the literal command python3 /home/user/Documents/test.py which obviously doesn't exist on your system.
When you type that line in a shell, the shell will split it on spaces and in the end it will call python3 with /home/user/Documents/test.py as argument.
When using subprocess.call, you have to do the splitting yourself.
I believe you need to pass your filename as another element in the array. I don't have gnome-terminal, but I replicated your issue with plain sh.
import subprocess
subprocess.call(['gnome-terminal', '-x', 'python3', filename])
Try this:
from os import system
system("gnome-terminal -e 'bash -c \"python3 %s\"'"%filename)
Add other commands using a semicolon:
system("gnome-terminal -e 'bash -c \"python3 %s; [second command]\"'")
Try this (i assume that python3 is set in PATH)
from subprocess import Popen
command="gnome-terminal -x python3"+filename
proc=Popen(command)
if this not works
then try to run your python file first , and see if it works or not
python filename

"Syntax error: Unterminated quoted string" error when calling a bash script from Python

I have a situation where, I need to call a bash script inside a python script which is in turn called inside another python script.
download-output-files.py:
#!/usr/bin/env python
import os
import sys
for i in node_path:
cmd="python watcher.py "+i
os.system(cmd) ##calling another python script
watcher.py:
#!/usr/bin/env python
import os
import time
if 'finish' in child:
print "finish found"
cmd="./copy_output_file.sh "+node
os.system(cmd) ##Calling shell script here
copy_output_file.sh:
#!/bin/bash
filepath=$1
cp ff /home/likewise-open/TALENTICA-ALL/mayankp/kazoo/$filepath
When I run download-output-files.py , it calls watcher.py , which in turn calls copy_output_file.sh and below is the error I face:
mayankp#mayankp:~/kazoo$ python download-output-files.py
finish found
sh: 1: Syntax error: Unterminated quoted string
When I run the same commands in Python shell, it runs bash script successfully. What am I missing?
It's generally unwise to concatenate strings into shell commands. Try inserting print(cmd) before your os.system(cmd) calls to find out exactly what commands you're trying to run, and I supect you'll notice what the problem is (likely a file name with an apostrophe in it).
Try using subprocess.call(['python', 'watcher.py', i]) instead of os.system("python watcher.py "+i), and subprocess.call(['/copy_output_file.sh', node]) instead of os.system(cmd="./copy_output_file.sh "+node).

how to run python script from the console as if called from command prompt?

The python script I would use (source code here) would parse some arguments when called from the command line. However, I have no access to the Windows command prompt (cmd.exe) in my environment. Can I call the same script from within a Python console? I would rather not rewrite the script itself.
%run is a magic in IPython that runs a named file inside IPython as a program almost exactly like running that file from the shell. Quoting from %run? referring to %run file args:
This is similar to running at a system prompt python file args,
but with the advantage of giving you IPython's tracebacks, and of
loading all variables into your interactive namespace for further use
(unless -p is used, see below). (end quote)
The only downside is that the file to be run must be in the current working directory or somewhere along the PYTHONPATH. %run won't search $PATH.
%run takes several options which you can learn about from %run?. For instance: -p to run under the profiler.
If you can make system calls, you can use:
import os
os.system("importer.py arguments_go_here")
You want to spawn a new subprocess.
There's a module for that: subprocess
Examples:
Basic:
import sys
from subprocess import Popen
p = Popen(sys.executable, "C:\test.py")
Getting the subprocess's output:
import sys
from subprocess import Popen, PIPE
p = Popen(sys.executable, "C:\test.py", stdout=PIPE)
stdout = p.stdout
print stdout.read()
See the subprocess API Documentation for more details.

Categories