Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

704
Views
FileNotFoundError: [Errno 2] No such file or directory + os.execv

I am creating a wrapper script to execute my python programs. The logic is like:

  1. I will install all my required modules in a docker container
  2. Pull the container and run the script, so anyone can run my scripts without worrying about the depended modules
  3. I created a wrapper script that will be called to execute the desired program

This is my wrapper script:

import os
import sys
import argparse

parse = argparse.ArgumentParser()
parse.add_argument('command', help="give datacenter name")
parse.add_argument('args', nargs=argparse.REMAINDER)
parse_arguments = parse.parse_args()

'''
Co-relate to the command and corresponding scripts to trigger
'''
scripts = {
    'verify' : '/path/verify.py'
    }

if __name__ == '__main__':

    if parse_arguments.command not in scripts:
        print('These are the available scripts to run:')
        print('\n'.join(sorted(scripts.keys())))
    else:
        os.execv(scripts.get(parse_arguments.command), 
[scripts.get(parse_arguments.command)] + parse_arguments.args) .   

I am running this like,

$ docker run -it --rm --net host run-script verify --listenv tpc1
Traceback (most recent call last):
  File "/path/runme.py", line 28, in <module>
    os.execv(scripts.get(parse_arguments.command), 
[scripts.get(parse_arguments.command)] + parse_arguments.args)
FileNotFoundError: [Errno 2] No such file or directory

My Docker image is run-script

If I run the same code from my local machine, it does work. but inside container it shows this file not find error.

Can anyone help me on this?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The first argument passed to sys.argv is the path to the running file, here /path/runme.py. The file is found when run from your local machine, but probably not in the docker container (I'm not sure why).

Try to set the prog parameter when instanciating your ArgParser, something like:

parse = argparse.ArgumentParser(prog="run_me.py")
over 4 years ago · Santiago Trujillo Report

0

I managed to solve this issue. The issue was mainly with the shebang that I have provided. So when I was executing the script in the docker container, it was checking to execute code from shebang location.

I would say, that is a mistake from my side, FileNotFoundError was not giving me a clue to shebang. Finally, figured it out.

@olinox14 - Thanks for your update, "prog" parameter in argparse helped me to output a better help message with the script name in docker container, rather than the full path inside docker container.

over 4 years ago · Santiago Trujillo Report

0

os.execv(program, args) by default doesn't search for program (first argument) based on PATH envrionment variable. os.execvp does.

From os.execv documentation :

os.execv(path, args)

The variants which include a “p” near the end (execlp(), execlpe(), execvp(), and execvpe()) will use the PATH environment variable to locate the program file.

So, either user os.execvp or provide full path to program to be run to os.execv.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!