1

I get the following error: No such file or directory but I'm unsure why. I know it looks easy to explain but I'm unsure what directory its referring to here

env = os.environ.copy()
env['MY_LIB_PATH'] = '/Users/user/Documents/workspace/projecttest/lib'
subprocess.call(["test_program",image_url],env=env)

Error:

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
8
  • Your test_program executable is not found. Commented Aug 8, 2014 at 14:40
  • What directory? It looks like the test program isn't there. Is it? Commented Aug 8, 2014 at 14:40
  • is test_program a python file? Commented Aug 8, 2014 at 14:42
  • @MartijnPieters thanks for the reply. Would that relate to my environ? I get MY_LIB_PATH to point to the folder where they all are Commented Aug 8, 2014 at 14:42
  • 1
    Why do you think setting MY_LIB_PATH in the environment would have any effect on how subprocess looks for an executable? Where is test_program located? What's your working directory? Commented Aug 8, 2014 at 14:44

2 Answers 2

3

To make sure that a program is found, you need to append to the PATH environment variable.

env = os.environ.copy()
env['PATH'] += os.pathsep + '/Users/user/Documents/workspace/projecttest/lib'
subprocess.call(["test_program", image_url], env=env)

Or you can call it explicitly;

cmd = '/Users/user/Documents/workspace/projecttest/lib/test_program'
subprocess.call([cmd, image_url])

Additionally, your test_program must be executable.

Note that subprocess.call can only execute programs. If you want to use a function from a dynamic link library, you'll have to use ctypes.

Sign up to request clarification or add additional context in comments.

4 Comments

In Windows is ';' no ':'.
@RaydelMiranda From the paths it looks like Spike is running OS X.
yes I know, I just want to point out that in Windows is different. Other user will read this in the future and there is no tag for an specific OS.
@RaydelMiranda Changed it to use os.pathsep. That should work everywhere. Good point.
2

You cannot set a random environment variable and expect python to magically read it. Either you:

  • Insert the path in PATH.
  • Use your random environment variable to construct the path, e.g.:

    subprocess.call([os.path.join(os.environ["MY_LIB_PATH"],
                                  "test_program"),
                    img_url])
    

2 Comments

But does call() not allow you to set env=?
If you set MY_LIB_PATH, you can pass env to call as you say. Inside test_program you can retrieve os.environ["MY_LIB_PATH"].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.