子进程报错。 "The system cannot find the file specified"

subprocess gives an error. "The system cannot find the file specified"

这是我的代码:

import urllib
import requests

from bs4 import *
from subprocess import Popen,PIPE
import os

connectString = 'SYSTEM/mediadot123'

def runSqlQuery(sqlCommand, connectString):
   session = Popen(['sqlplus', '-S', connectString], stdin=PIPE, stdout=PIPE, stderr=PIPE)
   session.stdin.write(sqlCommand)
   return session.communicate()

session = Popen(['sqlplus','-S','hr/hr'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = session.communicate()

sqlCommand = "insert into food(title, recipe, image) values ('bla','bla','bla');"
queryResult, errorMessage = runSqlQuery(sqlCommand, connectString)
print queryResult

它给了我以下错误:

C:\Python27\python.exe C:/Users/Umer/PycharmProjects/DATACRAWLER/main.py

Traceback (most recent call last):

  File "C:/Users/Umer/PycharmProjects/DATACRAWLER/main.py", line 38, in <module>

session = subprocess.Popen(['sqlplus','-S','hr/hr'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)

  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)

WindowsError: [Error 2] The system cannot find the file specified

考虑为您的 command-execution.
使用绝对路径 根据您的用户、系统和软件安装,某些二进制文件不在 PATH 中。

要找出 sqlplus 所在的位置,运行 cmd.exe 中的以下内容:where sqlplus 应该会为您提供绝对路径。

然后简单地做:

Popen(['C:/path/sqlplus.exe', '-S', ...])

另外,要找出 PATH 环境变量中的实际内容,您可以执行以下操作:

print(os.environ['PATH'])