# Multiprocessing with spawn and environment variables

**URL:** <https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749>\
**Category:** Python Help\
**Created:** [November 14, 2023, 10:28pm UTC](https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749 "2023-11-14T22:28:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [November 14, 2023, 10:28pm UTC](https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749/1 "2023-11-14T22:28:07Z")

</div>

Hey folks,

When using `spawn` as the multiprocessing start method, is there an easy way to persist the environment variables (as a snapshot) from `os.environ` into the new processes?

We have an old case where `fork` was used and this all worked out, but now with `spawn` we lose the environment variables changed in the parent python process when the child starts up. I’m sure I could save them and pass them as a function parameter or something but that seems like something that someone may have already solved for.

Thanks!

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [November 14, 2023, 10:40pm UTC](https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749/2 "2023-11-14T22:40:02Z")

</div>

Answering my own question:

Something else is probably wrong. The environment variables should pass on their own. Example:

```auto
from multiprocessing import Pool, get_start_method
import os

def print_test():
    print(str(os.environ.get('TEST')))

if __name__ == ' __main__':
    print(get_start_method())

    os.environ['TEST'] = 'hello world'
    with Pool() as p:
        p.apply_async(print_test).get()

    os.environ['TEST'] = 'hello again'
    with Pool() as p:
        p.apply_async(print_test).get()

```

gets us:

```auto
spawn
hello world
hello again

```

So something else is afoot.

---

<div class="post-metadata">

**Author:** ![franklinvp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/franklinvp/32/11142_2.png) [@franklinvp](https://discuss.python.org/u/franklinvp)\
**Post date:** [March 13, 2024, 5:59pm UTC](https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749/3 "2024-03-13T17:59:40Z")

</div>

What was the problem at the end? I am running into something similar.

In my case, spawned subprocesses are about to load a c++ module but this doesn’t see the environment variables. The parent process and the subprocesses do see them.

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [March 13, 2024, 6:16pm UTC](https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749/4 "2024-03-13T18:16:09Z")

</div>

I don’t fully remember. I think it had to do with the Pool being started before the environment variable was set. (The env var has to be set before the spawn/fork happens).

---

<div class="post-metadata">

**Author:** ![franklinvp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/franklinvp/32/11142_2.png) [@franklinvp](https://discuss.python.org/u/franklinvp)\
**Post date:** [March 13, 2024, 6:17pm UTC](https://discuss.python.org/t/multiprocessing-with-spawn-and-environment-variables/38749/5 "2024-03-13T18:17:13Z")

</div>

Ok. Thank you. In my case the environment is set before the Python interpreter even starts.
