13

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!

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Anthony Simard
  • 131
  • 1
  • 1
  • 3
  • The package is called python3-apt – Matúš Bako Oct 19 '18 at 21:16
  • 1
    Installing python3-apt doesn't resolve the problem unfortunately. It just says it was already up to date – Anthony Simard Oct 19 '18 at 21:21
  • 1
    "run some test code" usually best done in a `virtualenv` to get specific python and package versions working together correctly. – chickity china chinese chicken Oct 19 '18 at 21:23
  • Also, what modules you need that come in Python3.7 that are not in 3.6? Reading http://docs.python.org/3/whatsnew/3.7.html seems unlikely 3.7 version would be needed. But hey, if you just *want to* use 3.7 - go for it. – chickity china chinese chicken Oct 19 '18 at 21:25
  • There's a lot of similar questions already: https://stackoverflow.com/questions/13708180/python-dev-installation-error-importerror-no-module-named-apt-pkg – Eir Nym Oct 19 '18 at 22:37
  • 2
    None of the answers I read to the similar threads did anything to solve it for me. The reason I need 3.7 not 3.6 is they changed the byte code from 3.6 to 3.7 so the test code my professor gives us doesn't work on anything but 3.7 beacause he encodes it so we can't easily read it. – Anthony Simard Oct 19 '18 at 22:59

1 Answers1

31

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
  • 4
    It solved the "apt-pkg not found" problem. But I cannot run add-apt-repository, I get "command not found" – gmag11 Apr 03 '20 at 05:43
  • I did this, but I am also getting "command not found". Even reinstalling the packages doesn't solve the issue. As I couldn't recover, ended deleting the environment and starting over to solve all the problems. – Eduardo Matsuoka Aug 05 '20 at 19:33