Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Running Python in Isolated Mode (python -I)

One of the most effective built-in security features of Python is isolated mode, activated using the -I command-line flag.

What does python -I do?

When Python is started with the -I flag, it runs in isolated mode. This mode significantly restricts the environment in which Python executes:

Isolated mode is often combined with -S (no site) for even stronger isolation:

python -I -S script.py

When to Use Isolated Mode

ScenarioSecurity / Reliability Benefit
High-Security EnvironmentsPrevents malicious or unintended modifications via environment variables.
Running Untrusted CodeReduces the risk of code being influenced by external PYTHONPATH settings or user-installed packages.
Security Tooling & ScannersEnsures consistent, reproducible behaviour when running SAST, SCA, or custom security scripts.
CI/CD PipelinesEliminates “it works on my machine” issues caused by local environment variables.
Auditing and ForensicsProvides a clean baseline to analyse whether behaviour stems from the code or the environment.
SandboxingHelps contain potentially dangerous scripts by limiting their access to the broader system configuration.

Security Value

Running Python with -I (and especially -I -S) is a strong defensive technique that reduces the attack surface by:

This is particularly valuable when processing untrusted input, analysing suspicious scripts, or operating in zero-trust environments.

For maximum security and isolation, use the following command:

python -I -S -X dev -W error script.py

While isolated mode is excellent for security tools and analysis, it is generally not suitable for normal application runtime, as many dependencies rely on standard environment behaviour.