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.

Using python -S for Enhanced Security

One of the lesser-known but valuable security features in Python is the -S command-line flag.

What does python -S do?

The -S flag instructs Python to skip importing the site module during startup.

By default, Python automatically imports the site module, which performs several important tasks:

When you use python -S, all of these automatic behaviours are disabled, resulting in a cleaner, more isolated Python runtime.

When to Use python -S

ScenarioBenefit
High-Security or Sandboxed ExecutionPrevents loading of potentially malicious code from sitecustomize.py, .pth files, or tampered site-packages.
Running Untrusted ScriptsReduces the risk of supply chain attacks that rely on automatic package loading.
Minimal & Reproducible EnvironmentsEnsures execution without interference from installed third-party packages.
Auditing and ForensicsHelps determine whether unexpected behaviour originates from your code or from the environment.
CI/CD and Automated ToolsCreates consistent, isolated runs for scanners, linters, or security tools.

For maximum isolation, combine -S with -I (isolated mode):

python -I -S script.py

Security Value

Using python -S (especially with -I) significantly reduces the attack surface during Python script execution. It is particularly useful when:

While not suitable for normal application runtime (as most dependencies would become unavailable!), it is a powerful option to analysis scripts, and defensive execution.