From 9a96ca14be2ea18ad232861aa4491176157bb114 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 13 Feb 2020 15:37:09 +0100 Subject: [PATCH] Tools: make Python import error more readable The problem with printing the exception was that starting with Python 3.6 the ImportError is yet another (sub) exception called ModuleNotFoundError which can't be printed as a string and then triggers another exception: ``` Traceback (most recent call last): File "/home/julianoes/src/Firmware/Tools/serial/generate_config.py", line 11, in import jinja2 ModuleNotFoundError: No module named 'jinja2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/julianoes/src/Firmware/Tools/serial/generate_config.py", line 13, in print("Failed to import jinja2: " + e) TypeError: must be str, not ModuleNotFoundError ``` As per @bkueng's suggestion the easiest is to cast the exception to str and that way prevent the second exception. --- Tools/mavlink_shell.py | 4 +-- Tools/mavlink_ulog_streaming.py | 2 +- Tools/serial/generate_config.py | 4 +-- Tools/upload_log.py | 2 +- Tools/validate_yaml.py | 5 ++-- msg/tools/px_generate_uorb_topic_files.py | 29 ++++++++----------- .../geometries/tools/px_generate_mixers.py | 28 +++++++++++------- src/lib/parameters/px_generate_params.py | 2 +- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Tools/mavlink_shell.py b/Tools/mavlink_shell.py index c2954ea7cf..1bd603bba4 100755 --- a/Tools/mavlink_shell.py +++ b/Tools/mavlink_shell.py @@ -16,7 +16,7 @@ from argparse import ArgumentParser try: from pymavlink import mavutil except ImportError as e: - print("Failed to import pymavlink: " + e) + print("Failed to import pymavlink: " + str(e)) print("") print("You may need to install it with:") print(" pip3 install --user pymavlink") @@ -26,7 +26,7 @@ except ImportError as e: try: import serial except ImportError as e: - print("Failed to import pyserial: " + e) + print("Failed to import pyserial: " + str(e)) print("") print("You may need to install it with:") print(" pip3 install --user pyserial") diff --git a/Tools/mavlink_ulog_streaming.py b/Tools/mavlink_ulog_streaming.py index 3b3d151a32..ffd32f1c7f 100755 --- a/Tools/mavlink_ulog_streaming.py +++ b/Tools/mavlink_ulog_streaming.py @@ -17,7 +17,7 @@ from argparse import ArgumentParser try: from pymavlink import mavutil except ImportError as e: - print("Failed to import pymavlink: " + e) + print("Failed to import pymavlink: " + str(e)) print("") print("You may need to install it with:") print(" pip3 install --user pymavlink") diff --git a/Tools/serial/generate_config.py b/Tools/serial/generate_config.py index 247fb3245c..87144aec38 100755 --- a/Tools/serial/generate_config.py +++ b/Tools/serial/generate_config.py @@ -10,7 +10,7 @@ import sys try: from jinja2 import Environment, FileSystemLoader except ImportError as e: - print("Failed to import jinja2: " + e) + print("Failed to import jinja2: " + str(e)) print("") print("You may need to install it using:") print(" pip3 install --user jinja2") @@ -20,7 +20,7 @@ except ImportError as e: try: import yaml except ImportError as e: - print("Failed to import yaml: " + e) + print("Failed to import yaml: " + str(e)) print("") print("You may need to install it using:") print(" pip3 install --user pyyaml") diff --git a/Tools/upload_log.py b/Tools/upload_log.py index 1ccde34874..d39a792cea 100755 --- a/Tools/upload_log.py +++ b/Tools/upload_log.py @@ -17,7 +17,7 @@ import sys try: import requests except ImportError as e: - print("Failed to import requests: " + e) + print("Failed to import requests: " + str(e)) print("") print("You may need to install it using:") print(" pip3 install --user requests") diff --git a/Tools/validate_yaml.py b/Tools/validate_yaml.py index ee5a303c6f..3a2d022bfc 100755 --- a/Tools/validate_yaml.py +++ b/Tools/validate_yaml.py @@ -4,13 +4,12 @@ from __future__ import print_function import argparse -import os import sys try: import yaml except ImportError as e: - print("Failed to import yaml: " + e) + print("Failed to import yaml: " + str(e)) print("") print("You may need to install it using:") print(" pip3 install --user pyyaml") @@ -20,7 +19,7 @@ except ImportError as e: try: import cerberus except ImportError as e: - print("Failed to import cerberus: " + e) + print("Failed to import cerberus: " + str(e)) print("") print("You may need to install it using:") print(" pip3 install --user cerberus") diff --git a/msg/tools/px_generate_uorb_topic_files.py b/msg/tools/px_generate_uorb_topic_files.py index 707a5ff0e6..af5e1bceb4 100755 --- a/msg/tools/px_generate_uorb_topic_files.py +++ b/msg/tools/px_generate_uorb_topic_files.py @@ -43,31 +43,26 @@ import shutil import filecmp import argparse import sys -import errno try: import em except ImportError as e: - print("Python import error: ", e) - print(''' -Required python package empy not installed. - -Please run: - pip3 install --user empy -''') - exit(1) + print("Failed to import em: " + str(e)) + print("") + print("You may need to install it using:") + print(" pip3 install --user empy") + print("") + sys.exit(1) try: import genmsg.template_tools except ImportError as e: - print("Python import error: ", e) - print(''' -Required python package pyros-genmsg not installed. - -Please run: - pip3 install --user pyros-genmsg -''') - exit(1) + print("Failed to import genmsg: " + str(e)) + print("") + print("You may need to install it using:") + print(" pip3 install --user pyros-genmsg") + print("") + sys.exit(1) __author__ = "Sergey Belash, Thomas Gubler, Beat Kueng" diff --git a/src/lib/mixer/MultirotorMixer/geometries/tools/px_generate_mixers.py b/src/lib/mixer/MultirotorMixer/geometries/tools/px_generate_mixers.py index a0d3c1b222..932a265fb4 100755 --- a/src/lib/mixer/MultirotorMixer/geometries/tools/px_generate_mixers.py +++ b/src/lib/mixer/MultirotorMixer/geometries/tools/px_generate_mixers.py @@ -37,28 +37,34 @@ px_generate_mixers.py Generates c/cpp header/source files for multirotor mixers from geometry descriptions files (.toml format) """ +import sys try: import toml +except ImportError as e: + print("Failed to import toml: " + str(e)) + print("") + print("You may need to install it using:") + print(" pip3 install --user toml") + print("") + sys.exit(1) + +try: import numpy as np except ImportError as e: - print("python import error: ", e) - print(''' -Required python3 packages not installed. - -On a GNU/Linux or MacOS system please run: - sudo pip3 install numpy toml - -On Windows please run: - easy_install numpy toml -''') - exit(1) + print("Failed to import numpy: " + str(e)) + print("") + print("You may need to install it using:") + print(" pip3 install --user numpy") + print("") + sys.exit(1) __author__ = "Julien Lecoeur" __copyright__ = "Copyright (C) 2013-2017 PX4 Development Team." __license__ = "BSD" __email__ = "julien.lecoeur@gmail.com" + def parse_geometry_toml(filename): ''' Parses toml geometry file and returns a dictionary with curated list of rotors diff --git a/src/lib/parameters/px_generate_params.py b/src/lib/parameters/px_generate_params.py index ba59b74cd1..64440a0e94 100755 --- a/src/lib/parameters/px_generate_params.py +++ b/src/lib/parameters/px_generate_params.py @@ -11,7 +11,7 @@ import sys try: from jinja2 import Environment, FileSystemLoader except ImportError as e: - print("Failed to import jinja2: " + e) + print("Failed to import jinja2: " + str(e)) print("") print("You may need to install it using:") print(" pip3 install --user jinja2")