Practical Python

How to Keep Secrets Out of Python Projects

Separate credentials from code, keep example configuration harmless, and know what to inspect before running unfamiliar software.

Keep API keys and passwords in a private configuration source and use placeholders in shared examples. Inspect unfamiliar code before running it, including what it reads, where it connects, and which files it changes. If a real key appears in a commit or screenshot, stop using that key and rotate it through its issuer.

Check code, outputs, logs, and screenshots for secrets

Passwords, API tokens, private keys, session cookies, and recovery codes are obvious examples. Customer records, private case material, internal hostnames, and personal information can also be inappropriate for a public fixture even when they are not authentication secrets.

Use synthetic examples in tutorials and tests. Replacing a name may not be enough if the surrounding records still identify a person or organisation. Preserve the structure needed to explain the problem while removing the real sensitive content.

Separate configuration from code

An application can read environment variables or a private configuration file rather than embedding credentials in a Python module. The appropriate mechanism depends on the deployment. A .env filename alone does not make a file private; its permissions, location, backup handling, and source-control rules matter.

If a secret was committed or published, removing the visible line is not sufficient. Treat it as exposed and follow the provider's revocation or rotation process. Avoid posting the old value again while asking whether it was sensitive.

Inspect dependencies deliberately

Check the project's stated requirements and the identity of a package before installing it. Similar names can refer to different packages. Use an isolated project environment and the interpreter's -m pip so you understand where the installation is going.

An environment helps manage dependencies; it is not a security sandbox for hostile code. Run unfamiliar programs in an appropriate review environment under your organisation's rules. Do not assume a README, a checksum, or a popular-looking screenshot proves trustworthy behavior.

Keep trust checks enabled

When HTTPS fails, investigate the clock, URL, proxy, and certificate configuration. Disabling verification changes which servers the program may trust. It is not a neutral repair for a networking problem.

Give collection tools a narrow purpose. A feed collector can retrieve an approved feed without automatically downloading every linked article or attachment. Fewer automatic actions make the workflow easier to review and explain.

Build a safe first fixture

Create a configuration example containing placeholder names, document the fields, and write a test for a missing required value. Confirm that errors do not print the secret itself. A helpful error can name the missing setting without exposing its contents.

Before adding a dependency for a new project, check which interpreter receives installed packages so you know which environment you changed.

Sources and further reading

Related reading

Browse Practical Python · Explore project tutorials

Search PythonProjects.org