1

I want to run a python script in C in the following manner: (I already forked)

err = execlp("python", "my_script.py", "test", (char*) NULL);

In bash, I can successfully run

python my_script.py test

(test is an argument for the python script)

However, the program outputs

my_script.py: can't open file 'test': [Errno 2] No such file or directory

What am I doing wrong? :3

6
  • Error is your answer. give full path of "my_script.py" Commented Jan 26, 2014 at 18:11
  • Is the current directory the same in both cases? Commented Jan 26, 2014 at 18:12
  • Yes. It is trying to open 'test' but I never told it to. It should be passing test as an argument. Commented Jan 26, 2014 at 18:12
  • Can you verify that the current directory is the same I.e by setting it from within C? Commented Jan 26, 2014 at 18:13
  • Also, try hard coding the path to Python to verify that Python will run correctly when run in this manner. Commented Jan 26, 2014 at 18:14

1 Answer 1

6

Ah, I figured it out. It should be:

execlp("python", "python", "my_script.py", "test", (char*) NULL);

The first argument, by convention, is the filename of the executable. I thought that this was automatically passed but apparently it is not.

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

2 Comments

Just FYI, there is no need ever to cast NULL, at least in C. I assume the same is true for C++, but I'm not totally sure.
or one can use system("python my_script.py arg1, arg2").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.