serial_for_url: use importlib instead of __import__
diff --git a/serial/__init__.py b/serial/__init__.py
index e9cce1e..d7dcde3 100644
--- a/serial/__init__.py
+++ b/serial/__init__.py
@@ -8,6 +8,7 @@
VERSION = '3.0a'
+import importlib
import sys
if sys.platform == 'cli':
@@ -51,21 +52,22 @@
klass = Serial # 'native' implementation
# check port type and get class
try:
- url_nocase = url.lower()
+ url_lowercase = url.lower()
except AttributeError:
# it's not a string, use default
pass
else:
- if '://' in url_nocase:
- protocol = url_nocase.split('://', 1)[0]
+ if '://' in url_lowercase:
+ protocol = url_lowercase.split('://', 1)[0]
+ module_name = '.protocol_%s' % (protocol,)
for package_name in protocol_handler_packages:
- module_name = '%s.protocol_%s' % (package_name, protocol,)
+ package = importlib.import_module(package_name)
try:
- handler_module = __import__(module_name)
+ handler_module = importlib.import_module(module_name, package_name)
except ImportError:
pass
else:
- klass = sys.modules[module_name].Serial
+ klass = handler_module.Serial
break
else:
raise ValueError('invalid URL, protocol %r not known' % (protocol,))