Install & Compatibility
Where this runs
tested against v0.4.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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.029s · 17.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.7s · import 0.025s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
parse
✓ from dsnparse import parse
Commonly imported directly for DSN string parsing.
parse_environ
✓ from dsnparse import parse_environ
Used for parsing DSNs from environment variables.
ParseResult
✓ from dsnparse import ParseResult
Base class for customizing parsing results.
This quickstart demonstrates basic DSN parsing, extracting information from environment variables, and customizing the parsing result class.
import dsnparse
import os
# Example 1: Basic DSN parsing
dsn = "prom.interface.postgres.Interface://testuser:testpw@localhost:1234/testdb?appname=my_app#frag1"
r = dsnparse.parse(dsn)
print(f"Scheme: {r.scheme}") # prom.interface.postgres.Interface
print(f"Username: {r.username}") # testuser
print(f"Password: {r.password}") # testpw
print(f"Host: {r.host}") # localhost
print(f"Port: {r.port}") # 1234
print(f"Host Location: {r.hostloc}") # localhost:1234
print(f"Paths: {r.paths}") # ['testdb']
print(f"Query: {r.query}") # appname=my_app
print(f"Fragment: {r.fragment}") # frag1
print(f"Get Query Param 'appname': {r.get_query('appname')}") # my_app
# Example 2: Parsing from environment variable
os.environ['MY_DB_DSN'] = os.environ.get('MY_DB_DSN', 'sqlite:///:memory:')
env_dsn = dsnparse.parse_environ('MY_DB_DSN')
print(f"Env DSN Scheme: {env_dsn.scheme}") # sqlite
print(f"Env DSN Path: {env_dsn.path}") # :memory:
# Example 3: Custom ParseResult class
class MyResult(dsnparse.ParseResult):
def configure(self):
# Expose 'scheme' as 'interface' for custom logic
self.interface = self.scheme
custom_dsn = "MyInterface://customuser:custompass@host:5432/path"
r_custom = dsnparse.parse(custom_dsn, parse_class=MyResult)
print(f"Custom DSN Type: {type(r_custom)}") # <class '__main__.MyResult'>
print(f"Custom DSN Interface: {r_custom.interface}") # MyInterface
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dsnparse'
The 'dsnparse' library has not been installed in your Python environment.
fixInstall the library using pip: `pip install dsnparse`
ValueError: Invalid port specified
When parsing certain DSNs (like `sqlite:///:memory:` in version 0.4.0), `dsnparse` might incorrectly attempt to interpret a path component as a 'host:port' segment, leading to a port validation error.
fixFor SQLite, use a file-based DSN format like `sqlite:///path/to/database.db` or upgrade `dsnparse` to a version that correctly handles in-memory SQLite DSNs (fixed in 0.4.2+).
AttributeError: 'ParseResult' object has no attribute 'hostname'
The `dsnparse.ParseResult` object uses the attribute `host` to store the hostname, not `hostname` as is common in `urllib.parse.urlparse`'s result.
fixAccess the host using the `host` attribute: `dsn = dsnparse.dsnparse('postgresql://user@localhost:5432/mydb'); print(dsn.host)` TypeError: dsnparse() missing 1 required positional argument: 'dsn'
The `dsnparse.dsnparse()` function was called without providing the mandatory DSN string argument.
fixProvide the DSN string to be parsed as the first argument: `dsnparse.dsnparse('postgresql://user:pass@host:port/database')` Upgrade
Version history
0.4.0latest on PyPI · released Oct 13, 2025
Audit
Dependencies
No dependency data recorded yet.