Python:子流程“调用"函数找不到路径 [英] Python: Subprocess "call" function can't find path

查看:93
本文介绍了Python:子流程“调用"函数找不到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在服务器上运行bash命令的python程序.当我在本地目录中运行bash命令时,该程序将运行:

I have a python program that needs to run a bash command on a server. The program works when I run a bash command in my local directory like this:

导入子过程从子流程导入调用call(['bash','Script_Test.sh'])

但是,在SSH连接到服务器并运行下面类似的代码行以及服务器上通往bash脚本的路径之后,出现错误无此文件或目录"

However, after SSH'ing to a server and running a similar line of code below with a path on the server to a bash script, I get the error "No such file or directory"

call['bash', path]

出于多种原因,这没有任何意义.我三重检查了路径是否正确.我继续使用Putty,连接到那里的服务器,并使用相同的路径运行了一个bash命令,该命令可以正常工作,因此它不能成为路径.我还进行了许多测试,以确保将SSH连接到正确的服务器,并且确实如此.我以为我运行bash时服务器上存在安全问题,所以我改用cat.不,仍然无法找到路径.

This doesn't make sense for a number of reasons. I triple checked that the path is correct. I went on Putty, connected to the server on there, and ran a bash command with the same path and it worked, so it can't be the path. I also ran a number of tests to make sure I was SSH'd into the correct server, and I was. I thought there was a security issue on the server with me running bash, so I tried cat instead. Nope, still unable to locate the path.

我对python子进程不是很熟悉,因此任何指向我在此处调用"所缺少的内容的指针都将非常有帮助.

I'm not super familiar with python subprocesses, so any pointers to anything I'm missing here with "call" would be very helpful.

推荐答案

确保脚本已准备好执行

  1. 给脚本一个剧本行

首先,在类似Unix的系统上的脚本中,重要的是在您的脚本中包含shebang行.对于脚本的可移植性,我 ,建议您使用#!/usr/bin/env bash .

First things first, it is important that you include a shebang line in your script on Unix-like systems. I recommend, for your script's portability, that you use #!/usr/bin/env bash.

文件扩展名的说明:

我建议您从脚本中删除 .sh 扩展名.如果您使用的是Bourne Again Shell(bash )执行脚本,则使用 .sh 扩展名会产生误导.简而言之, Bourne Shell( sh )与Bourne Again Shell( bash )不同-因此,请勿使用建议您使用与您使用的外壳不同的文件扩展名其实是!

I would recommend that you remove the .sh extension from your script. If you are using the Bourne Again Shell (bash) to execute your script, then using the .sh extension is misleading. Simply put, the Bourne Shell (sh) is different than the Bourne Again Shell (bash) - so don't use a file extension that suggests you are using a different shell than you actually are!

如果您不更改文件扩展名,这还不是世界末日-您的脚本仍然会如果您具有正确的 bash shebang行,则以 bash 脚本执行.不过,还是最好使用无文件扩展名(强烈建议使用 Script_Test )或 .bash 文件扩展名( Script_Test.bash ).

It's not the end of the world if you don't do change your file extension - your script will still be executed as a bash script if you have the proper bash shebang line. Still, it is good practice to either use no file extension (Script_Test -- strongly preferred) or the .bash file extension (Script_Test.bash).

  1. 为脚本提供适当的文件权限

出于您的目的,也许仅授予当前用户读取和执行脚本的权限很重要.在这种情况下,请使用 chmod u + x Script_Test.sh .这里重要的是正确的用户( u + )/组( g + )有权执行脚本.

For your purposes, maybe it is only important to give the current user permissions to read and execute the script. In that case, use chmod u+x Script_Test.sh. What is important here is that the correct user (u+) / group (g+) has permissions to execute the script.

  1. 确保您的脚本的路径位于 $ PATH 环境变量中

在Python脚本中执行 bash 脚本

完成这些步骤后,您的Python脚本应该会按照您在问题中的调用方式进行工作:

Executing your bash script in a Python script

Once you've followed these steps, your Python script should work as you've called it in your question:

import subprocess
from subprocess import call

your_call = call("Test_Script.sh")

如果您不想将脚本移动到 $ PATH 环境变量中,只需确保您引用了脚本的完整路径(即当前目录 ./,以您为例):

If you would rather not move your script into the $PATH environment variable, just make sure that you refer to the script's full path (which is the current directory, ./, in your case):

import subprocess
from subprocess import call

your_call = call("./Test_Script.sh")

最后,如果您的脚本没有shebang行,则需要在您的 call 函数中指定一个附加参数:

Lastly, if your script does not have a shebang line, you will need to specify an additional parameter in your call function:

import subprocess
from subprocess import call

your_call = call("./Test_Script.sh", shell=True)

但是,我不建议使用最后一种方法.有关,请参见 Python 2.7.12文档.子进程 ...

However, I would not recommend this last approach. See the Python 2.7.12 documentation for the subprocess package...

警告:使用 shell = True 可能会带来安全隐患.有关详细信息,请参见常用参数下的警告.

Warning: Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.

如果您仍然遇到问题,请查看 @zenpoy对类似StackOverflow问题的解释.

Please check out @zenpoy's explanation to a similar StackOverflow question if you are still having problems.

祝您编程愉快!

这篇关于Python:子流程“调用"函数找不到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆