Python3.7 'No module named apt_pkg' error on Linux Mint 19

Anthony Simard picture Anthony Simard · Oct 19, 2018 · Viewed 13.8k times · Source

When I attempt to run some test code my professor gave me using Python3.7 on Linux Mint 19 I get the following error

RuntimeError: Bad code object in .pyc file
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
RuntimeError: Bad code object in .pyc file

At first, I only installed Python3.7 minimal and this error didn't occur, but I needed to install the full version of Python3.7 to use some modules that come with it, and that started this error. I already tried installing Python3.7 dev and it did nothing. I also tried

sudo apt-get remove --purge python3.7-apt

and it said it couldn't even find apt. So I tried

sudo apt-get install python3.7-apt

and it couldn't even find the package. I also tried installing python3-apt and it did nothing. I would uninstall python3-apt first but apparently that could harm the OS because Mint uses Python3.6 internally. Any help resolving this would be much appreciated!

Answer

user11249934 picture user11249934 · Jul 26, 2019

I had the same issue with python3.7
I literally just reinstalled python3-apt and the error disappeard

sudo apt remove python3-apt

then

sudo apt install python3-apt