CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Python Process Environment and Working Directory

A child process starts with a copy of your environment and your working directory. Both can be set per child, and both have a trap: env= replaces the whole environment rather than adding to it, so a child given one variable has no HOME and a PATH it did not expect; and os.chdir changes the directory for everything after it, whereas cwd= affects only that one child.

OS and Process Automation Guide 19 of 39 Intermediate

Nothing on this page is version-dependent. os.environ has behaved this way since Python 2 and cwd= has been a subprocess argument from the start.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

This guide includes

Use this before spawning anything. This matters because env= replaces the environment rather than adding to it - so a child given one variable loses PATH and everything else it needed.

Before you start

  1. A child inherits the environment

  2. env= replaces rather than adds

  3. The working directory a child gets

  4. So prefer cwd= over os.chdir

  5. What a process knows about itself

Official sources