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 <module>
    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 <module>
    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.
This commit is contained in:
Julian Oes 2020-02-13 15:37:09 +01:00 committed by Beat Küng
parent 7cafbc824e
commit 9a96ca14be
8 changed files with 38 additions and 38 deletions

View File

@ -16,7 +16,7 @@ from argparse import ArgumentParser
try: try:
from pymavlink import mavutil from pymavlink import mavutil
except ImportError as e: except ImportError as e:
print("Failed to import pymavlink: " + e) print("Failed to import pymavlink: " + str(e))
print("") print("")
print("You may need to install it with:") print("You may need to install it with:")
print(" pip3 install --user pymavlink") print(" pip3 install --user pymavlink")
@ -26,7 +26,7 @@ except ImportError as e:
try: try:
import serial import serial
except ImportError as e: except ImportError as e:
print("Failed to import pyserial: " + e) print("Failed to import pyserial: " + str(e))
print("") print("")
print("You may need to install it with:") print("You may need to install it with:")
print(" pip3 install --user pyserial") print(" pip3 install --user pyserial")

View File

@ -17,7 +17,7 @@ from argparse import ArgumentParser
try: try:
from pymavlink import mavutil from pymavlink import mavutil
except ImportError as e: except ImportError as e:
print("Failed to import pymavlink: " + e) print("Failed to import pymavlink: " + str(e))
print("") print("")
print("You may need to install it with:") print("You may need to install it with:")
print(" pip3 install --user pymavlink") print(" pip3 install --user pymavlink")

View File

@ -10,7 +10,7 @@ import sys
try: try:
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
except ImportError as e: except ImportError as e:
print("Failed to import jinja2: " + e) print("Failed to import jinja2: " + str(e))
print("") print("")
print("You may need to install it using:") print("You may need to install it using:")
print(" pip3 install --user jinja2") print(" pip3 install --user jinja2")
@ -20,7 +20,7 @@ except ImportError as e:
try: try:
import yaml import yaml
except ImportError as e: except ImportError as e:
print("Failed to import yaml: " + e) print("Failed to import yaml: " + str(e))
print("") print("")
print("You may need to install it using:") print("You may need to install it using:")
print(" pip3 install --user pyyaml") print(" pip3 install --user pyyaml")

View File

@ -17,7 +17,7 @@ import sys
try: try:
import requests import requests
except ImportError as e: except ImportError as e:
print("Failed to import requests: " + e) print("Failed to import requests: " + str(e))
print("") print("")
print("You may need to install it using:") print("You may need to install it using:")
print(" pip3 install --user requests") print(" pip3 install --user requests")

View File

@ -4,13 +4,12 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import os
import sys import sys
try: try:
import yaml import yaml
except ImportError as e: except ImportError as e:
print("Failed to import yaml: " + e) print("Failed to import yaml: " + str(e))
print("") print("")
print("You may need to install it using:") print("You may need to install it using:")
print(" pip3 install --user pyyaml") print(" pip3 install --user pyyaml")
@ -20,7 +19,7 @@ except ImportError as e:
try: try:
import cerberus import cerberus
except ImportError as e: except ImportError as e:
print("Failed to import cerberus: " + e) print("Failed to import cerberus: " + str(e))
print("") print("")
print("You may need to install it using:") print("You may need to install it using:")
print(" pip3 install --user cerberus") print(" pip3 install --user cerberus")

View File

@ -43,31 +43,26 @@ import shutil
import filecmp import filecmp
import argparse import argparse
import sys import sys
import errno
try: try:
import em import em
except ImportError as e: except ImportError as e:
print("Python import error: ", e) print("Failed to import em: " + str(e))
print(''' print("")
Required python package empy not installed. print("You may need to install it using:")
print(" pip3 install --user empy")
Please run: print("")
pip3 install --user empy sys.exit(1)
''')
exit(1)
try: try:
import genmsg.template_tools import genmsg.template_tools
except ImportError as e: except ImportError as e:
print("Python import error: ", e) print("Failed to import genmsg: " + str(e))
print(''' print("")
Required python package pyros-genmsg not installed. print("You may need to install it using:")
print(" pip3 install --user pyros-genmsg")
Please run: print("")
pip3 install --user pyros-genmsg sys.exit(1)
''')
exit(1)
__author__ = "Sergey Belash, Thomas Gubler, Beat Kueng" __author__ = "Sergey Belash, Thomas Gubler, Beat Kueng"

View File

@ -37,28 +37,34 @@ px_generate_mixers.py
Generates c/cpp header/source files for multirotor mixers Generates c/cpp header/source files for multirotor mixers
from geometry descriptions files (.toml format) from geometry descriptions files (.toml format)
""" """
import sys
try: try:
import toml 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 import numpy as np
except ImportError as e: except ImportError as e:
print("python import error: ", e) print("Failed to import numpy: " + str(e))
print(''' print("")
Required python3 packages not installed. print("You may need to install it using:")
print(" pip3 install --user numpy")
On a GNU/Linux or MacOS system please run: print("")
sudo pip3 install numpy toml sys.exit(1)
On Windows please run:
easy_install numpy toml
''')
exit(1)
__author__ = "Julien Lecoeur" __author__ = "Julien Lecoeur"
__copyright__ = "Copyright (C) 2013-2017 PX4 Development Team." __copyright__ = "Copyright (C) 2013-2017 PX4 Development Team."
__license__ = "BSD" __license__ = "BSD"
__email__ = "julien.lecoeur@gmail.com" __email__ = "julien.lecoeur@gmail.com"
def parse_geometry_toml(filename): def parse_geometry_toml(filename):
''' '''
Parses toml geometry file and returns a dictionary with curated list of rotors Parses toml geometry file and returns a dictionary with curated list of rotors

View File

@ -11,7 +11,7 @@ import sys
try: try:
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
except ImportError as e: except ImportError as e:
print("Failed to import jinja2: " + e) print("Failed to import jinja2: " + str(e))
print("") print("")
print("You may need to install it using:") print("You may need to install it using:")
print(" pip3 install --user jinja2") print(" pip3 install --user jinja2")