我有以下代码,即使定义了shlex,也会遇到以下错误。
import shlex
import sys
import subprocess
def main ():
branch_name = sys.argv[1]
print "branch_name"
print branch_name
print "start repo..."
RepoInitCmd = 'repo init -u git://git.quicinc.com/platform/manifest.git -b ' + branch_name
proc = subprocess.Popen(shlex.split(RepoInitCmd), stderr=subprocess.PIPE)
out, error = proc.communicate()
print "Done repo..."
if __name__ == '__main__':
main()错误:-
Traceback (most recent call last):
File "repoinit.py", line 15, in <module>
if __name__ == '__main__':
File "repoinit.py", line 10, in main
RepoInitCmd = 'repo init -u git://git.quicinc.com/platform/manifest.git -b ' + branch_name
NameError: global name 'shlex' is not defined发布于 2013-11-28 13:59:36
如果未定义shlex名称,则意味着您尚未在代码中导入该模块。这意味着真正的代码与您在问题中编写的代码不匹配。如果没有安装shlex,它将在import shlex行上失败。
发布于 2021-04-13 06:19:59
重构以将shlex.split调用移出subprocess.Popen可能是这里的解决方案。我在调用subprocess.run时遇到了同样的错误。
import shlex
shlex.split('repo init -u git://git.quicinc.com/platform/manifest.git -b ' + branch_name)
proc = subprocess.Popen(RepoInitCmdArgs, stderr=subprocess.PIPE)https://stackoverflow.com/questions/20258631
复制相似问题