WindowsError [错误5]访问被拒绝
我正在使用killableprocess包(建立在子进程之上)运行进程 每当我在脚本中运行“killableprocess.Popen(command)”代码段时,以下错误:WindowsError [错误5]访问被拒绝
File "killableprocess.py", line 157, in _execute_child
winprocess.AssignProcessToJobObject(self._job, hp)
File "winprocess.py", line 37, in ErrCheckBool
raise WinError()
WindowsError [error 5] Access is denied
Exception TypeError: "'NoneType' object is not callable" in <bound method AutoHANDLE.__del__ of <AutoHANDLE object at 0x025D42B0>> ignored
但是,当我从python交互式控制台(python 2.6)运行它时,它工作正常。 这可能意味着当我从脚本运行该脚本时存在权限问题,但我不知道如何解决它们。我尝试从以管理员身份运行的cmd运行脚本,但没有帮助。 试图寻找类似的帖子,但没有找到任何好的解决方案。希望在这里找到一些帮助 我在Windows上运行,特别是Windows 7旗舰版x64,如果它有任何帮助。
感谢
另外,如果你的模块不能正常工作,您可以使用«子»模块:
import subprocess, os, time
process = subprocess.Popen("somecommand", shell=True)
n = 0
while True:
if not process.poll():
print('The command is running...')
if n >= 10:
pid = process.pid()
os.kill(pid, 9) # 9 = SIGKILL
else:
print('The command is not running..')
n += 1
time.sleep(1)
你指定完整路径为可执行要传递到Popen
(第一项在argv
)?
我解决了类似的问题,我通过切换到进程目录(我试图用Inkscape中),它解决了我的问题
import subprocess
inkscape_dir=r"C:\Program Files (x86)\Inkscape"
assert os.path.isdir(inkscape_dir)
os.chdir(inkscape_dir)
subprocess.Popen(['inkscape.exe',"-f",fname,"-e",fname_png])
也许切换到进程目录会为你工作了。
当我用子进程模块运行时发现,我发现'args'中的第一个条目(subprocess.Popen()
的第一个参数)只是没有路径的可执行文件名,我需要在参数中设置executable
列表到我的可执行文件的完整路径。
app = 'app.exe'
appPath = os.path.join(BIN_DIR, app)
commandLine = [app, 'arg1', 'arg2']
process = subprocess.Popen(commandLine, executable=appPath)
也要注意当前的工作目录;其他答案建议在启动过程之前需要'os.chdir(other_dir)',这可能是真实的,具体取决于过程本身的实现。但是,您也可以使用'cwd = other_dir'参数来指定'Popen'来设置cwd,而无需为脚本本身进行更改。 – 2017-07-05 16:13:21
取出括号中的'process.pid()'(“类型错误:‘诠释’对象不是可调用“) – 2011-06-08 17:42:50