Error in sys.excepthook: ModuleNotFoundError: No module named 'apt_pkg'

Bug #1631367 reported by graingert
80
This bug affects 15 people
Affects Status Importance Assigned to Milestone
python3.6 (Ubuntu)
Confirmed
Low
Unassigned

Bug Description

Errors that are supposed to be handled by the except hook fail due to incompatible apt_pkg module

$ python3.6
Python 3.6.0b1 (default, Sep 17 2016, 12:08:22)
[GCC 6.2.0 20160914] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> foop
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foop' is not defined
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 23, 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:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foop' is not defined
>>>

ProblemType: Bug
DistroRelease: Ubuntu 16.10
Package: python3.6 3.6.0~b1-1ubuntu2
ProcVersionSignature: Ubuntu 4.8.0-17.19-generic 4.8.0-rc7
Uname: Linux 4.8.0-17-generic x86_64
ApportVersion: 2.20.3-0ubuntu7
Architecture: amd64
CurrentDesktop: GNOME
Date: Fri Oct 7 12:08:38 2016
InstallationDate: Installed on 2016-01-05 (275 days ago)
InstallationMedia: Ubuntu 15.10 "Wily Werewolf" - Release amd64 (20151021)
SourcePackage: python3.6
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
graingert (tagrain) wrote :
Revision history for this message
Hans Joachim Desserud (hjd) wrote :

Thanks for taking your time to report this issue and help making Ubuntu better.

I get the same error when I follow your example on Ubuntu Yakkety. I don't get a stacktrace if I attempt the same with python3.5

Changed in python3.6 (Ubuntu):
status: New → Confirmed
Revision history for this message
Brian Murray (brian-murray) wrote :

What version of python3-apport and python3-apt do you have installed?

Changed in python3.6 (Ubuntu):
importance: Undecided → Low
Revision history for this message
Brian Murray (brian-murray) wrote :

It might be that the python3-apt package isn't setup for python3.6.

Revision history for this message
graingert (tagrain) wrote : Re: [Bug 1631367] Re: Error in sys.excepthook: ModuleNotFoundError: No module named 'apt_pkg'

Brian, this is straight up what it is...

On 10 Oct 2016 20:46, "Brian Murray" <email address hidden> wrote:

> It might be that the python3-apt package isn't setup for python3.6.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1631367
>
> Title:
> Error in sys.excepthook: ModuleNotFoundError: No module named
> 'apt_pkg'
>
> Status in python3.6 package in Ubuntu:
> Confirmed
>
> Bug description:
> Errors that are supposed to be handled by the except hook fail due to
> incompatible apt_pkg module
>
> $ python3.6
> Python 3.6.0b1 (default, Sep 17 2016, 12:08:22)
> [GCC 6.2.0 20160914] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> foop
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'foop' is not defined
> 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
> 23, 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:
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'foop' is not defined
> >>>
>
> ProblemType: Bug
> DistroRelease: Ubuntu 16.10
> Package: python3.6 3.6.0~b1-1ubuntu2
> ProcVersionSignature: Ubuntu 4.8.0-17.19-generic 4.8.0-rc7
> Uname: Linux 4.8.0-17-generic x86_64
> ApportVersion: 2.20.3-0ubuntu7
> Architecture: amd64
> CurrentDesktop: GNOME
> Date: Fri Oct 7 12:08:38 2016
> InstallationDate: Installed on 2016-01-05 (275 days ago)
> InstallationMedia: Ubuntu 15.10 "Wily Werewolf" - Release amd64
> (20151021)
> SourcePackage: python3.6
> UpgradeStatus: No upgrade log present (probably fresh install)
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ubuntu/+source/python3.6/+bug/
> 1631367/+subscriptions
>

Revision history for this message
Hans Joachim Desserud (hjd) wrote :

>What version of python3-apport and python3-apt do you have installed?

python3-apport: 2.20.3-0ubuntu8
python3-apt: 1.1.0~beta5

(I also suspect it isn't set up for python3.6)

Revision history for this message
Anthony Sottile (asottile) wrote :

I did some digging into this here: https://bitbucket.org/fkpackaging/deadsnakes-issues/issues/39/looking-for-a-new-maintainer#comment-33484057

The "default" python in zesty is python3.5, since python3.6 is a non-default python, it should install dist-packages into /usr/lib/python3.6/dist-packages instead of /usr/lib/python3/dist-packages (at least if I'm correct :D)

Revision history for this message
Frankie Robertson (frankie-q) wrote :

This shouldn't be "priority low". Apport breaks the Python 3.6 package. At least all desktop users trying to use Python 3.6 should be affected.

To summarise the problem, python3-apt has a C module which is only compiled for Python 3.5, but Python 3.6 needs it to run the apport.

Some possible possible workarounds:
* Get rid of the symlink: rm /usr/lib/python3.6/sitecustomize.py

* Change /etc/sitecustomize.py to not install the hook if apt_pkg is available

    # install the apport exception handler if available
    try:
        import apport_python_hook
        import apt_pkg # <-- Add this line
    except ImportError:
        pass
    else:
        apport_python_hook.install()

* Install the apt module

    $ sudo apt install python3.6-dev libapt-pkg-dev python3-distutils-extra # Get build requirements
    $ wget https://bootstrap.pypa.io/get-pip.py # Get pip
    $ python3.6 get-pip.py
    $ python3.6 /usr/bin/pip3 install http://launchpad.net/python-distutils-extra/trunk/2.28/+download/python-distutils-extra-2.28.tar.gz # install apt requirments
    $ python3.6 /usr/bin/pip3 install https://launchpad.net/ubuntu/+archive/primary/+files/python-apt_1.1.0~beta1build1.tar.xz # install apt package for Python 3.6

Revision history for this message
nikki (nikki-the-great) wrote :

I am using python3.6.2 on ubuntu 17

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'

Revision history for this message
Jeff Silverman (jeffsilverm) wrote :

I tried this and it worked. I didn't need to get pip because I already had it, but that's okay, I construe it as an "abundance of caution" step and I commend you.

BEFORE:
```
    >>> dir(rdatatype)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'rdatatype' is not defined
    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:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'rdatatype' is not defined
    >>>
```

After:
```
    >>> dir(rdatatype)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'rdatatype' is not defined
    >>>

```

IMHO, you have the correct fix for this issue. You probably already knew that.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.