我收到以下错误:没有这样的文件或目录,但我不确定原因。我知道它看起来很容易解释,但我不确定它所指的目录
env = os.environ.copy()
env['MY_LIB_PATH'] = '/Users/user/Documents/workspace/projecttest/lib'
subprocess.call(["test_program",image_url],env=env)
错误:
Traceback (most recent call last):
File "test.py", line 20, in <module>
model = get_model(image.get_image_content())
File "/Users/user/Documents/workspace/projecttest/utilities.py", line 51, in get)model
env=env
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
答案 0 :(得分:3)
要确保找到程序,您需要附加到PATH
环境变量。
env = os.environ.copy()
env['PATH'] += os.pathsep + '/Users/user/Documents/workspace/projecttest/lib'
subprocess.call(["test_program", image_url], env=env)
或者您可以明确地调用它;
cmd = '/Users/user/Documents/workspace/projecttest/lib/test_program'
subprocess.call([cmd, image_url])
此外,您的test_program
必须是可执行的。
请注意,subprocess.call
只能执行程序。如果您想使用动态链接库中的函数,则必须使用ctypes
。
答案 1 :(得分:2)
你不能设置随机环境变量,并期望python神奇地读取它。你要么:
PATH
。使用随机环境变量构建路径,例如:
subprocess.call([os.path.join(os.environ["MY_LIB_PATH"],
"test_program"),
img_url])