I saw several threads dedicated to this error, but none solved my problem. Thought I'd post so folks can look at my code and maybe we can figure out another solution for everyone.
I'm attempting to run a Python script whose first task is to run a batch file. The batch file actually runs Wget to download the files that the Python script will work on.
If I run the entire Python script manually, it works perfectly. However, if I run it using Windows Task Scheduler or from Command Line, it has issues with the batch script.
If I comment out the batch script part, Task Scheduler/CMD can run the Python script just fine. Task Scheduler/CMD can also run the batch file independently with no issue.
Here's my Python code:
import time
import os
import sys
from subprocess import Popen
import zipfile
import win32com.client as win32
#1# Run the downloader batch file
p = Popen("NDICDownloader.bat", cwd=r"C:\NDICUpdate")
stdout, stderr = p.communicate()
p.wait()
Here's the error I get in command line:
c:\Python27\python.exe c:\NDICUpdate\NDICUpdater.py
Traceback (most recent call last):
file "c:\NDICUpdate\NDICUpdater.py", line 9, in (module)
p = Popen("NDICDownloader.bat", cwd=r"C:\NDICUpdate")
file "c:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite)
file "c:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Here's the batch file:
cd C:\NDICUpdate\NDIC
wget --arguments include sensitive info--
The batch file uses Wget to download files to the NDIC folder. All scripts are located in the root folder C:\NDICUpdate. All files exist.
The problem is trying to use Windows to run the batch file within the Python script. Why are Windows and Python butting heads here??
(Answered in the Comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
#MC ND wrote:
Change your code to call cmd.exe as the started process with /c NDICUpdate.bat as parameters to the executable:
p = Popen(["cmd.exe", "/c NDICDownloader.bat"], cwd=r"C:\NDICUpdate")
The OP wrote:
that worked. This also works: os.system("c:\windows\system32\cmd.exe /c C:\NDICDownloader.bat"). What I ended up doing in the end was writing a batch file that runs the Wget and then runs the Python. Lots of headache/wasted time, but at least a workable solution. Still no idea how to get cmd.exe to run Python's subprocess.
Related
I'm trying to run a python script when the system boots on a Windows 10 workstation. My python script runs fine when I kick it off in a different directory as a user with administrative privileges but when I run it from a bat file in the C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup folder on system boot I receive the following error in my logs.
2018-11-13 04:43:58,913 - VP - Level 55 - nmap args: ['"C:\\Program Files (x86)\\Nmap\\nmap.exe"', '-oX', 'C:\\\\nmap\\NmapResults\\scan-192.168.1.5-20181113044358.xml', '-sS', '-n', '-p-', '--allports', '--scan-delay', '1ms', '-sU', '-sV', '--version-all', '192.168.1.5']
Traceback (most recent call last):
File "secnmap\nmap_scanner.py", line 596, in <module>
exit_code = main()
File "secnmap\nmap_scanner.py", line 248, in main
run_nmap(args, scan_options, host_data_list, use_service_name_check)
File "secnmap\nmap_scanner.py", line 268, in run_nmap
nmap_proc = subprocess.Popen(nmap_cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
Here is the content of the .bat file.
cd C:\\nmap_startup
python nmap_startup.py
I have tried using the full nmap.exe path as shown in the code snippet as well as plain old nmap. I get the access denied error either way. I didn't see much in subprocess.py that would lead me to believe this to be an error with the python code either so I am leaning on it being an issue with System user running the script. If someone thinks otherwise or has any ideas, I'd love to hear them. Thanks
EDIT: I chose to run from a bat file because my python version is 32bit and I was having issues running from the C:\windows\system32 directory
It turns out that the path to nmap, C:\Program Files (x86)\Nmap, was not included in the system environment variables. When the script ran as a System user, it did not recognize the location of nmap
Big picture is I'm trying to automate my deployment process of building with maven and deploying to a web logic server. Little picture is I'm using subprocess to see if I can call maven from within python. When I attempt this subprocess mistakes mvn for a file.
Here is my code so far:
import subprocess
def main():
print(subprocess.check_output(["mvn", "-v"]))
if __name__ == '__main__':
main()
And here's my error:
C:\pythondev\python.exe "C:/pythondev/development/deployment scripts/redploy-to-localhost.py"
Traceback (most recent call last):
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 9, in <module>
main()
File "C:/pythondev/development/deployment scripts/redploy-to-localhost.py", line 5, in main
subprocess.check_output(["a"])
File "C:\pythondev\lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "C:\pythondev\lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\pythondev\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\pythondev\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 1
Although my issue is with subprocess I'm open to answers that suggest a better alternative.
I ran into the same issue and was hesistant to use shell=True, because the internet tells me this is evil.
When I run where mvn in my cmd.exe, I can see that there are two matches:
mvn, which is a Unix shell-script (it starts with #!/bin/sh),
mvn.cmd, which is a Windows batch file.
I think what happens when you execute mvn something -something in cmd.exe is the following: Windows tries finding an executable called mvn. It finds the mvn file, but realizes that this file is not executable. It then tries finding files like mvn.com, mvn.exe, ... (see the %PATHEXT% system variable). When it finds mvn.cmd, it executes that and everyone is happy.
As far as I understand it, the problem with subprocess.check_output (and subprocess.run, and so on) is that the path-"expansion" via %PATHEXT% is not being performed. So the solution is that you have to give the extension manually and run your command as
print(subprocess.check_output(["mvn.cmd", "-v"]))
Try this it worked for me.
print(subprocess.check_output(["mvn", "-v"], shell=True))
I am trying to intall antiSMASH on my research group's server but hitting an issue with the final stage of downloading the relevant databases. The makers provide a script "download_databases.py" to do this for you (see code at https://bitbucket.org/antismash/antismash/src/718da23d059742048bf044a1ed663806051eb0b2/download_databases.py?at=master&fileviewer=file-view-default).
Sadly there seems to be some kind of access issue (I'm not root and sudo doesn't help).
Server is RedHat CentOS 7.2.1511
Command run is "python download_databases.py" in antismash directory. Output is below:
Creating checksum of Pfam-A.hmm.gz
Extraction of Pfam-A.hmm.gz finished successfully.
Traceback (most recent call last):
File "download_databases.py", line 221, in <module>
main()
File "download_databases.py", line 198, in main
compile_pfam(filename)
File "download_databases.py", line 161, in compile_pfam
execute(command)
File "download_databases.py", line 51, in execute
stderr=subprocess.PIPE)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Does anyone with experience of this have an idea of what I need to do? I've tried a local install of Python (2.7) where I have read/write access but that doesn't seem to help as the script is still looking elsewhere for the subprocess script (my own install is at the top of the PATH). Do I possibly need to install something extra with the local install or customise the install locations to include the subprocess etc stuff?
Thanks in advance. I'm no expert and really appreciate the help.
EDIT: Well I feel quite stupid now. Thanks to Hannu for suggesting I check $PATH and what binary the script was trying to execute. It turned out to be one of antiSMASH's dependencies (hmmpress) which I hadn't yet added to the $PATH on this machine. It hadn't occurred to me that it would be required prior to actually running antiSMASH.
Add print commands or something like that to your execute function and see what it tries to execute. The only explanation to get the error in that command is that the script or binary it tries to execute cannot be found.
Try adding a full path to your binary. That alone might solve the issue. Check that there are no typos in the command. Remember commands needs to be a list if it contains parameters, for example ["/usr/bin/foo/myprogram", "-a", "42", "-b", "/tmp/outputfile"] or whatever it is you need to pass to the program.
I am trying to learn how to run command line commands from Python. I am able to do this with DOS:
import subprocess
subprocess.call("dir",shell=True)
This is fine, but I need to be able to do this for linux commands because my company uses linux servers. I am using Mobaxterm to run a local linux session. When I try this:
import subprocess
subprocess.call("ls",shell=True)
I get this error from the terminal:
'ls' is not recognized as an internal or external command, operable program or batch file.
Which sounds ridiculous to me, because ls is clearly a linux command.
If I don't include shell=True I get this error:
Traceback (most recent call last):
File "ConsolePractice.py", line 4, in <module>
subprocess.call("ls")
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 560, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\my_username\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
So what am I doing wrong? How can I get this to work?
EDIT: Thanks for the comments. Python for Windows won't work for what I'm trying to do. I connected to a linux server and did:
cat > ConsoleCmd.py
import subprocess
subprocess.call("ls",shell=True)
^C
python ConsoleCmd.py
... and ls ran. Found the issue. I'll just have to get my scripts on to the server and run them from there. Thanks everyone
I was able to resolve this issue by opening an SFTP Session within Mobaxterm, transferring the python file to the server, then running the file directly on the server (python was already installed on the server).
So i'm trying to setup the python code checkers suggested in the emacs wiki. However, I'm unable to run those scripts in my command shell let alone emacs.
The section is found here:
http://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc7
And I tried the script located here and here
In both cases I changed the first line from #!usr/bin python with the full path of my python executable and when I run the scripts via
python pylint_etc_wrappers.py someModule.py
or
python pycheckers.py soemModule.py
both boil down to the same error, most likely because they try to open a subprocess. Here's the trace:
Traceback (most recent call last):
File "pycheckers.py", line 254, in <module>
runner.run(source_file)
File "pycheckers.py", line 91, in run
process = Popen(args, stdout=PIPE, stderr=PIPE)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The second script suggests to change the first line to the path of the interpreter (which I did) and to change the path in the main function which looks something like :
os.environ['PATH'] = \
path.dirname(sys.executable) + ':' + os.environ['PATH']
which was a bit unclear to me. Any ideas?
I have pylint 0.25.1, installed using easy_install (Python 2.7, Win XP). Both pylint and pylint.bat were installed in Python27/Scripts (this directory is in my PATH).
I too get the "The system cannot find the file specified" error when running the pylint_etc_wrapper.py script unchanged.
Running pylint from the script does work if
command = 'pylint'
is changed to
command = 'pylint.bat'
Another way to make it work is to add shell=True to the Popen() call.
I can't really explain all this, but there is an unresolved Python bug that looks like it might be relevant: http://bugs.python.org/issue8557.