I am a novice user in python and I am having a problem in executing external command with command-line switch (using python 2.7 in Windows 8 64-bit):
lammpsExe = 'C:/Program Files (x86)/LAMMPS 32-bit 20150403/bin/lmp_serial.exe'
os.system(lammpsExe + " -in in.lmps")
It gives the following error message:
'C Program' is not recognized as an internal or external command, operable program or batch file
It seems that os.system cannot understand the string path for lammpsExe.
Then I tried subprocess.call and replace '/' with '\\' in the path:
lammpsExe = 'C:\\Program Files\\LAMMPS 64-bit 20150330\\bin\\lmp_serial.exe'
subprocess.call([lammpsExe,'-in in.lmps'], shell=True)
But it still doesn't work as the command prompt gives the following warning:
IndentationError: unexpected indent
I suspect the command-line switch '-in' is the problem. I have tried various combination of ", ', \, and /, and I still get error messages.
The subprocess docs has a section titled Replacing os.system():
status = os.system("mycmd" + " myarg")
# becomes
status = subprocess.call("mycmd" + " myarg", shell=True)
I would suggest:
lammpsExe = 'C:\\Program Files\\LAMMPS 64-bit 20150330\\bin\\lmp_serial.exe'
subprocess.call(lammpsExe + ' -in in.lmps', shell=True)
The docs do note that "Calling the program through the shell is usually not required."
I think I have found the solution. Instead of trying to define the path in the python script for the external command, I just need to put the path in the Windows system variables, then calling the command directly in the script:
subprocess.call(['lmp_serial.exe','-in','in.lmps'],shell=True)
Related
I have the following script utils/pdf2image.sh:
#!/bin/bash
pdftoppm -png $1 $2
and the following snippet of Python code:
script_path = os.path.join(os.getcwd(), 'utils', 'pdf2image.sh') + ' ' + invoice_path + ' ' + invoice_path[:-4]
subprocess.call(script_path, shell=True)
When I call the script in command line, it works, but when I call it from Python, it doesn't, saying that pdftoppm doesn't work. I checked and poppler-utils is installed. I suspect that pdftoppm can't be seen from Python environment. Any idea why?
The user's PATH and the script's PATH aren't necessarily the same, so it seems that the BASH script being called from inside the Python script doesn't know where pdftoppm is.
You have two options:
Calling the full path to pdftoppm inside the BASH script:
#!/bin/bash
/usr/bin/pdftoppm -png $1 $2
Calling pdftoppm directly from the Python script:
script_path = os.path.join(os.getcwd(), 'utils', 'pdf2image.sh') + ' ' + invoice_path + ' ' + invoice_path[:-4]
subprocess.Popen(['/usr/bin/pdftoppm', '-png', invoice_path, invoice_path[:-4]])
From what I'm understanding on your problem, the Python code is unable to locate the pdftoppm . This can happen if that binary is not present in the $PATH.
I would recommend to make use of the absolute path to the pdftoppm if that's the case in that shell script and then try invoking the shell script from Python.
Though as the comments to your question have pointed out already that the script's piece of work can be done by subprocess itself, you should give that a shot as well :)
I have been working on an issue that requires a Python script to run via the PowerShell command line. The script should pass the command to the command line and save the output. However, I'm running into an issue where some command line arguments are not recognized.
import subprocess
try:
output = subprocess.check_output\
(["Write-Output 'Hello world'"], shell=True)
# (["dir"], shell=True)
except subprocess.CalledProcessError as e:
print(e.output)
print('^Error Output^')
If I use the current command with the check_output command, I get an error stating that:
'"Write-Output 'Hello world'"' is not recognized as an internal or external command,
operable program or batch file.
If I just use the "dir" line, the script runs just fine. I'm at odds here as to why this would be happening. This is not the exact script that I'm running, but it produces the same problem on my machine. If I just type the problem command into the command line, it would output "Hello world" onto the new line just as expected.
Any insight as to why this would be happening would be greatly appreciated. If it's of relevance, I would like to not use any sort of admin privilege workaround.
I believe this is because in Windows your default Shell is not PowerShell, you could Execute a Powershell command, calling the executable by executing Powershell with the arguments you need.
For Example
POWERSHELL_COMMAND = r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe'
subprocess.Popen([POWERSHELL_COMMAND,
'-ExecutionPolicy', 'Unrestricted',
'Write-Output', 'Hello World'],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
if powershell is not in path you could use the full path for the executable
or if it's in path you could use just POWERSHELL_COMMAND = "powershell" as command, becareful, with the backslashed windows paths, to avoid errors you could use raw strings.
To verify that you have powershell in path, you could go to the configurations and check, or you could just open a cmd and type powershell and if It works, then you could assume that powershell is in path.
From the docs:
On Windows with shell=True, the COMSPEC environment variable specifies the default shell.
So set COMSPEC=powershell allows to make shell=True use powershell as default instead of cmd
I am trying to run following python script on windows powershell which is throwing following error
ERROR:
The term 'x86' is not recognized as the name of a cmdlet, function, script file
, or operable program. Check the spelling of the name, or if a path was include
d, verify that the path is correct and try again.
SCRIPT:
import os
import sys
Watchdog_config = 'C:\\Program Files (x86)\\Common Files\\ibm\\icc\\cimom\\data\\wmia.properties'
command1 = "PowerShell -Command \"& {(cat "+Watchdog_config+" )|%{$_ -replace {\"off\",\"on\"}}|set "+Watchdog_config+"}\""
os.system(command1)
Can you try :
Watchdog_config = '\"C:\\Program Files (x86)\\Common Files\\ibm\\icc\\cimom\\data\\wmia.properties\"'
I'am not confortable with python, I just want to enclose path into double quotes "".
I'm writing a programme that needs to run on both Linux and Windows and use executables (with parameters) that exist in the path. (Assumed)
Currently I'm having trouble running executables in windows using Subprocess.Call and Subprocess.Popen.
For a code like this, in windows 8
def makeBlastDB(inFile, inputType, dbType, title, outDir):
strProg = 'makeblastdb'
strInput = '-in ' + inFile
strInputType = '-input_type ' + inputType
strDBType = '-dbtype ' + dbType
strTitle = '-title ' + title
strOut = '-out ' + os.path.join(os.sep, outDir, title)
cmd = [strProg, strInput, strInputType, strDBType, strTitle, strOut]
result = Popen(cmd, shell=True)
I get the error message in console
'makeblastdb' is not recognized as an internal or external command,
operable program or batch file.
Even though I can run the same command using cmd.exe
I get the same response with shell=False.
Any ideas on how I can run the command assuming that the executable is in PATH environment variable? Thanks
You can control the environment variables available in the spawned subprocess by passing a mapping with the env keyword argument. E.g.
proc = subprocess.Popen(args, env={'PATH': '/some/path'})
Or to inherit PATH from the system environment variable, without necessarily chucking in everything else from the system environment:
proc = subprocess.Popen(args, env={'PATH': os.getenv('PATH')})
It might be easier/simpler just to use an absolute path, though.
Ok here is how I got it to work.
env = os.environ
proc = subprocess.Popen(args, env=env)
I struggled with this myself until I found this python bug report.
"If you add a directory into PATH on Windows so that the directory is in quotes, subprocess does not find executables in it." Since the quotes aren't required by windows removing them fixes my problem (in 2.7).
A colleague of mine has reproduced this issue with Python 3.6.5 on Windows 10 64-bits.
The installed version of Python was however 32-bits.
Re-installing 64-bits version of Python fixed this issue.
System Info:
Windows 7,
GNU Wget 1.11.4,
Python 2.6
The problem:
Im running a python script that fires a wget shortcut, the problem is that wget (even when run purely in command line from the exe) cuts off '&''s. For example when i run the code below, this is what i get:
C:\Program Files\GnuWin32\bin>wget.exe
http://www.imdb.com/search/title?genres=action&sort=alpha,asc&start=51&title_type=feature
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc syswgetrc = C:\Program
Files\GnuWin32/etc/wgetrc
--2013-01-18 12:48:43-- http://www.imdb.com/search/title?genres=action Resolving
www.imdb.com... 72.21.215.52 Connecting to
www.imdb.com|72.21.215.52|:80... failed: Connection refused.
=alpha,ascThe system cannot find the file specified.
The system cannot find the file 51. 'title_type' is not recognized as an internal or
external command, operable program or batch file.
As you can see, wget counts all text before the '&' as the URL in question, and windows take the last half as a new command(s).
There has got to be some way of allowing wget to capture that whole string as the URL.
Thanks in advance.
EDIT:
When i call the command in command line with brackets around it, it works great, however, when i run the script through python:
subprocess.Popen(['start /B wget.lnk --directory-prefix=' + output_folder + ' --output-document=' + output_folder + 'this.html "http://www.imdb.com/search/title?genres=action&sort=alpha,asc&start=51&title_type=feature"'], shell=True)
I get the following error:
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc
"http://www.imdb.com/search/title?genres=action&sort=alpha,asc&start=51&title_ty
pe=feature": Unsupported scheme.
It's not Wget that cuts off the URL, but the command interpreter, which uses & to separate two commands, akin to ;. This is indicated by the =alpha,ascThe system cannot find the file specified. error on the following line.
To prevent this from happening, quote the entire URL:
wget.exe "http://www.imdb.com/search/title?genres=action&sort=alpha,asc&start=51&title_type=feature"