From 1334e5fe9efa2886dade49edf8e65ffc5b8519b5 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Fri, 23 Apr 2021 13:44:07 -0400 Subject: [PATCH 1/2] Generate error message for installation when ConfigurationError is raised --- pymongo/uri_parser.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pymongo/uri_parser.py b/pymongo/uri_parser.py index a683b2fa6d..b5548e1838 100644 --- a/pymongo/uri_parser.py +++ b/pymongo/uri_parser.py @@ -16,6 +16,7 @@ """Tools to parse and validate a MongoDB URI.""" import re import warnings +import os from urllib.parse import unquote_plus @@ -416,8 +417,13 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, scheme_free = uri[SCHEME_LEN:] elif uri.startswith(SRV_SCHEME): if not _HAVE_DNSPYTHON: + python_path = os.getenv('_', "python") raise ConfigurationError('The "dnspython" module must be ' - 'installed to use mongodb+srv:// URIs') + 'installed to use mongodb+srv:// URIs. ' + 'To fix this error install pymongo ' + 'with the srv extra:\n ' + '%s -m pip install "pymongo[srv]"' + % (python_path)) is_srv = True scheme_free = uri[SRV_SCHEME_LEN:] else: From 7270b4c40332f1a334855a4c4d89eae09f8f3895 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Fri, 23 Apr 2021 23:27:51 -0400 Subject: [PATCH 2/2] Replace os with sys and format error message --- pymongo/uri_parser.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pymongo/uri_parser.py b/pymongo/uri_parser.py index b5548e1838..60d03ba497 100644 --- a/pymongo/uri_parser.py +++ b/pymongo/uri_parser.py @@ -16,7 +16,7 @@ """Tools to parse and validate a MongoDB URI.""" import re import warnings -import os +import sys from urllib.parse import unquote_plus @@ -417,13 +417,12 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False, scheme_free = uri[SCHEME_LEN:] elif uri.startswith(SRV_SCHEME): if not _HAVE_DNSPYTHON: - python_path = os.getenv('_', "python") - raise ConfigurationError('The "dnspython" module must be ' - 'installed to use mongodb+srv:// URIs. ' - 'To fix this error install pymongo ' - 'with the srv extra:\n ' - '%s -m pip install "pymongo[srv]"' - % (python_path)) + python_path = sys.executable or "python" + raise ConfigurationError( + 'The "dnspython" module must be ' + 'installed to use mongodb+srv:// URIs. ' + 'To fix this error install pymongo with the srv extra:\n ' + '%s -m pip install "pymongo[srv]"' % (python_path)) is_srv = True scheme_free = uri[SRV_SCHEME_LEN:] else: