Registry / auth-security / pem
library23.1.0pypypi✓ verified 22d ago

The `pem` library is a Python module designed for parsing and splitting PEM (Privacy Enhanced Mail) files, which contain Base64-encoded DER keys and certificates. It is a lightweight tool with no external dependencies, focusing solely on file parsing without interpreting the certificate data itself. The current version is 23.1.0, and it maintains a steady, though not rapid, release cadence.

pip install pem
INSTALL
IMPORT
SIG · PEM
P
pem
auth-securitypythonv23.1.0
Install
1.5s avg
Import
32ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v23.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.034s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.030s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pem
import pem

This quickstart demonstrates how to parse a PEM file. It first creates a dummy `example.pem` file with a sample certificate, then uses `pem.parse_file()` to read and extract the PEM objects. The type and a truncated string representation of each found object are printed.

import pem # Create a dummy PEM file for demonstration with open("example.pem", "w") as f: f.write("-----BEGIN CERTIFICATE-----\n") f.write("MIIDTDCCArSgAwIBAgIUJgMhCgS1P2Q3R5fL6Z9y/B0X6UowDQYJKoZIhvcNAQEL\n") f.write("BQAwMjELMAkGA1UEBhMCSVQxEDAOBgNVBAgMB0xhdGlvczEQMA4GA1UEAwwHVGVz\n") f.write("dENlcnQwHhcNMjMwMTAxMDAwMDAwWhcNMzMwMTAxMDAwMDAwWjAyMQswCQYDVQQG\n") f.write("EwJJVDERMA8GA1UECAwIQ2l0dGFkZTEPMA0GA1UEAwwGbG9jYWxoZDAwggEiMA0G\n") f.write("CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDEcE5pZz3lF9mJ8p+qG7xR7qK6YmQp\n") f.write("qM2oY0t/J2n7c5y2X6Q5F7pW1Z7rC9Q9X0P0P0P0P0P0P0P0P0P0P0P0P0P0P0P\n") f.write("AgMBAAGjUzBRMB0GA1UdDgQWBBTx3B4e3b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b\n") f.write("MCgGA1UdEQQfMB2CDXNvbWVkb21haW4uY29tggdFWEFNUExFMzANBgkqhkiG9w0B\n") f.write("AQsFAAOCAQEAy/qJ/w+7p+2v+6v+7v+8v/9v/g+v/x+y/0+1/2/3/4/5/6/7/8/\n") f.write("-----END CERTIFICATE-----\n") certs = pem.parse_file("example.pem") for cert in certs: print(f"Found PEM object: {type(cert).__name__}") print(str(cert)[:100] + "...") # Print first 100 chars of the PEM string
Debug
Known issues
gotchaPEM files are frequently used for sensitive cryptographic material like private keys. These should generally NOT be committed to version control systems like Git.
fix
Store PEM files containing sensitive data outside of your repository, preferably in secure storage or environment variables, and load them at runtime. Use `.gitignore` to prevent accidental commits.
affects: All
gotchaWhen working with `requests` or `ssl` modules, ensure your PEM file contains the complete certificate chain (server certificate, intermediate CAs, and root CA) if you are providing it for verification. An incomplete chain can lead to `SSLCertVerificationError`.
fix
Concatenate all necessary certificates (server, intermediate, root) into a single PEM file. Tools like `openssl x509 -in cert.cer -inform DER -outform PEM >> combined.pem` can be used to convert DER and combine PEM files.
affects: All
gotchaThe `pem` library strictly parses PEM-encoded data. If you are attempting to parse a file that is in DER (Distinguished Encoding Rules) format without PEM wrappers, `pem` will not be able to process it.
fix
Ensure your files are correctly PEM-encoded (i.e., start with `-----BEGIN ...-----` and end with `-----END ...-----`). If you have DER files, convert them to PEM format before using `pem`.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pem'
The `pem` library is not installed in the Python environment being used.
fix
Install the library using pip: `pip install pem`
ValueError: Found more than one private key or certificate, or none at all.
When using `pem` with certain APIs (like Twisted's `CertificateOptions`), a `ValueError` is raised if the input PEM file contains an unexpected number of keys or certificates (e.g., more than one key, no key, or no certificate). This can also occur if the PEM file is empty or malformed and `pem` cannot identify any valid blocks.
fix
Ensure the PEM file contains the expected number and type of blocks for the specific function being called. For general parsing with `pem.parse()` or `pem.parse_file()`, ensure the file contains valid PEM formatted data. If dealing with multiple blocks, iterate through the list returned by `pem.parse()` or use `pem.parse_file()` for individual files.
AttributeError: 'Certificate' object has no attribute 'subject'
The `pem` library is designed for basic parsing and splitting of PEM files, not for interpreting or exposing certificate attributes like 'subject', 'issuer', or 'not_valid_before'. The objects returned by `pem.parse()` (e.g., `pem.Certificate`, `pem.RSAPrivateKey`) are lightweight wrappers primarily convertible to strings.
fix
If you need to access detailed attributes of certificates or keys (like subject, issuer, expiry dates, or cryptographic details), use a dedicated cryptography library such as `cryptography` or `PyOpenSSL` to parse the string representation obtained from the `pem` objects.
Upgrade
Version history
23.1.0latest on PyPI · released Jun 21, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
pem — pip install pem · libregistry