Prometheus
Prometheus

Reputation: 33605

Python error No such file or directory

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

Upvotes: 1

Views: 5794

Answers (2)

Roland Smith
Roland Smith

Reputation: 43495

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.

Upvotes: 3

xbello
xbello

Reputation: 7443

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])
    

Upvotes: 2

Related Questions