# execute\_process: how to pass argument to python script

**URL:** https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999
**Category:** Usage
**Created:** [February 14, 2022, 3:22pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999 "2022-02-14T15:22:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jwuttke](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jwuttke](https://discourse.cmake.org/u/jwuttke)
#### Post date: [February 14, 2022, 3:22pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/1 "2022-02-14T15:22:58Z")

</div>

```auto
execute_process(COMMAND ${Python3_EXECUTABLE} my_script.py my_arg)

```

doesn’t work as intended: `my_script.py` is executed, but does not see the argument `my_arg`.

Whereas on the command line

```auto
$ python3 my_script.py my_args

```

does work.

As a minimal example, suppose `my_script.py` is

```auto
import sys
print("my_script found argument", sys.argv[1])

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 14, 2022, 5:19pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/2 "2022-02-14T17:19:51Z")

</div>

This works locally:

```auto
% cmake -P run.cmake
-- Found Python3: /usr/bin/python3.9 (found version "3.9.10") found components: Interpreter 
my_script found argument my_arg
% cat my_script.py
import sys
print("my_script found argument", sys.argv[1])
% cat run.cmake
find_package(Python3 REQUIRED COMPONENTS Interpreter)
execute_process(COMMAND ${Python3_EXECUTABLE} my_script.py my_arg)

```

Can you provide a fuller example that shows the problem?

---

<div class="post-metadata">

### Author: ![jwuttke](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jwuttke](https://discourse.cmake.org/u/jwuttke)
#### Post date: [February 15, 2022, 11:05am UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/3 "2022-02-15T11:05:39Z")

</div>

Thank you very much, Ben.

Things didn’t work in my more complex code because I confused upper and lower case in a variable name.

Apologies, Joachim

---

<div class="post-metadata">

### Author: ![Nguyen\_Khuong](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nguyen_khuong/32/3977_2.png) [@Nguyen\_Khuong](https://discourse.cmake.org/u/Nguyen_Khuong)
#### Post date: [November 3, 2023, 3:40pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/4 "2023-11-03T15:40:49Z")

</div>

Hello everyone,

I faced issue when send back argument from python to Cmake I used  
os.environ[“MY\_DATA”] = ‘2’  
print(os.environ[“MY\_DATA”])

and I read the env variable in Cmake was

set(env $ENV{MY\_DATA})  
message(“Helloo Python your value is : ${env}”)

It not work , Can someone explain for me why

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 3, 2023, 4:42pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/5 "2023-11-03T16:42:06Z")

</div>

Can you provide the relationship between these two snippets of code? Is the Python script doing the `os.environ` setting launching CMake somehow? Can you share that?

---

<div class="post-metadata">

### Author: ![Nguyen\_Khuong](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nguyen_khuong/32/3977_2.png) [@Nguyen\_Khuong](https://discourse.cmake.org/u/Nguyen_Khuong)
#### Post date: [November 3, 2023, 5:13pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/6 "2023-11-03T17:13:18Z")

</div>

Hi Ben ,

Thanks for reply

For 2 snippet code I wanted trial to send a message from Python to CMake parents , when it call to Python I trial to export an env variable by Python to use in CMake for other purpose. I used with excute\_process to call Python script in CMake parents and message to print value Python return back . But it was not work

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 3, 2023, 5:53pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/7 "2023-11-03T17:53:25Z")

</div>

A child process cannot set the environment of the parent process, so a Python script setting its environment has no effect on the CMake process calling it via `execute_process`.

---

<div class="post-metadata">

### Author: ![Nguyen\_Khuong](https://discourse.cmake.org/user_avatar/discourse.cmake.org/nguyen_khuong/32/3977_2.png) [@Nguyen\_Khuong](https://discourse.cmake.org/u/Nguyen_Khuong)
#### Post date: [November 3, 2023, 10:49pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/8 "2023-11-03T22:49:56Z")

</div>

Thanks for your reply Ben .

Are there anyway Python scripts can export env for other scripts use or it just only set its env and other scripts can’t see ? I used to set env variable in CMake and shell. Python scripts use env (export env in shell and set env $ENV{env} in CMake) . I just trial some way to export env variables from Python.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 6, 2023, 11:25pm UTC](https://discourse.cmake.org/t/execute-process-how-to-pass-argument-to-python-script/4999/9 "2023-11-06T23:25:49Z")

</div>

The environment can only be modified for the currently running processes and new processes started (via passing them to the `execve` or `CreateProcess` family of APIs).
