1

I am trying to use execl() to execute a new program but it keeps returning an execv() error saying that arg2 must not be empty.

if pid == 0:
    print("This is a child process")
    print("Using exec to another program")
    os.execl("example_prg_02.py")

Why would this be the case when using execl()? Does execl() require args too?

Andy
  • 7
  • 7
  • Try `os.execl("python", "example_prg_02.py")` – rdas Apr 12 '20 at 12:40
  • @rdas tried that too but it doesn't work. Returned the same error. – Andy Apr 12 '20 at 13:08
  • 1
    `execl` is just a wrapper around `execv` that lets arguments be passed as separate varargs, instead of as a single array of strings. It's still doing the same thing under the hood, just with a different calling convention. – Charles Duffy Apr 12 '20 at 14:03

1 Answers1

2

"example_prg_02.py" is not a path to executable file, you have to specify

  • a path to executable file as a 1st parameter,
  • the name of executable as a 2nd one,
  • parameter(s) as 3rd (4th, 5th, ...)

So instead of your

os.execl("example_prg_02.py")

use

os.execl(sys.executable, "python", "example_prg_02.py")

(you have first import sys, of course).

sys.executable is the absolute path of the executable binary for the Python interpreter.


Addendum (from my comment):

Why error from execv(), when I used execl()?

Both execv() and execl() do the same thing, they differ in how command-line arguments are passed:

  • if the last letter is v (variable number of arguments), you have to provide a list or tuple for argv (i.e. arguments),

  • if the last letter is l (it probably means list them — for constant number of them), you have to provide argv as individual arguments.

execl() is only a “syntactical sugar” — it calls internally execv(), so you obtained the error from execv().

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • That's perfect, works now and makes more sense to me, thanks. Do you know why it was returning a execv() error for an execl() function? – Andy Apr 12 '20 at 13:01
  • 1
    Both `execv()` and `execl()` do the same thing, they differ in how command-line arguments are passed: if the last letter is **v** (variable number of parameters), you have to provide a *list or tuple* for `argv` (i.e. arguments), if it is **l** (it probably means *list them* - for constant number of them), you have to provide `argv` *as individual arguments* . — `execl()` is only a “syntactical sugar” — it calls internally `execv()`, so you obtained `execv()` error. – MarianD Apr 12 '20 at 13:29
  • what's the purpose of "the name of executable as a 2nd one", since we already have "a path to executable file as a 1st parameter", so can the second parameter different from the first (I mean point to different executable) , what's the usage of the second one? – D C dQ Dec 21 '20 at 10:16
  • @DCdQ, you're very clever, no other asked this. The short answer is that almost all executables totally ignore this 2nd parameter (so you may write *any* string for it, e.g. `"abracadabra"`). If you want to understand why, please write your comment as a new question and I will answer it. – MarianD Dec 21 '20 at 12:40
  • Thanks, just raised : https://stackoverflow.com/questions/65403089/what-the-usage-of-2nd-parameter-in-os-execl – D C dQ Dec 22 '20 at 04:00