Importing matplotlib causes "int() argument must be a string" error

Question:

I’m new to Python. A few days ago I installed Anaconda and PyCharm (on D disk), and I am trying to use the matplotlib package to plot one picture. When I click "run", I get the following error:

Traceback (most recent call last):
  File "G:onedriveOneDrive - mail.dlut.edu.cnPyCharmshock waveP6.py", line 7, in <module>
    import matplotlib.pyplot as plt
  File "D:anaconda3libsite-packagesmatplotlibpyplot.py", line 2230, in <module>
    switch_backend(rcParams["backend"])
  File "D:anaconda3libsite-packagesmatplotlib__init__.py", line 672, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel)
  File "D:anaconda3libsite-packagesmatplotlibpyplot.py", line 247, in switch_backend
    switch_backend(candidate)
  File "D:anaconda3libsite-packagesmatplotlibpyplot.py", line 267, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "D:anaconda3libsite-packagesmatplotlibpyplot.py", line 268, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "D:anaconda3libimportlib__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:anaconda3libsite-packagesmatplotlibbackendsbackend_qtagg.py", line 12, in <module>
    from .backend_qt import (
  File "D:anaconda3libsite-packagesmatplotlibbackendsbackend_qt.py", line 73, in <module>
    _MODIFIER_KEYS = [
  File "D:anaconda3libsite-packagesmatplotlibbackendsbackend_qt.py", line 74, in <listcomp>
    (_to_int(getattr(_enum("QtCore.Qt.KeyboardModifier"), mod)),
TypeError: int() argument must be a string, a bytes-like object or a number, not 'KeyboardModifier'

Process finished with exit code 1
Asked By: loverchun

||

Answers:

It looks like this is a bug in pyside6 v6.3.0, one of the libraries matplotlib depends on for rendering plots; here’s the bug report. It’s a new bug, and it’s already been fixed, so it’s really bad luck that it got you!

Solution: the issue seems to be fixed in pyside version 6.4.0 (released 13 October), so one solution is to upgrade it, or you could downgrade, e.g. to version 6.2. Another solution is to try using another backend, because I think the problem only affects the Qt backend. (A backend is a rendering engine for matplotlibread all about them.) It’s easy to try this latter option, so let’s start there.

Use another backend

Try this at the top of your script:

import matplotlib
matplotlib.use('tkagg')

Or you can try others; see this page for help.

Up- or down-grade pyside

To handle this, you’ll need to deal with ‘virtual environments’. You may already be doing this. Environments let you have different versions of Python and different collections of packages for different projects you might be working on.

Fix the base environment…

When you install Anaconda, it made an environment called base that contains ‘everything’ in Anaconda (Python plus lots of libraries like matplotlib). You can upgrade the version of pyside in the base environment by opening an Anaconda prompt from the Start menu and typing this:

conda install -c conda-forge pyside==6.4.0

However, most programmers don’t use their base environment and prefer to manage an environment specific to their project. If you are doing this, or want to give it a try, read on.

…or make a new environment

Alternatively, to make a new environment, open an Anaconda prompt and type this but replace MYENV with a short suitable name for the environment:

conda create -n MYENV python=3.10 pyside=6.4.0 anaconda

Or you can replace anaconda with a list of the packages you want, like jupyter scipy networkx or whatever.

You would then start using this environment with conda activate MYENV and your script or notebook should run in there no problem.

Answered By: Matt Hall

Update
When Update pyside >=6.4.0, You alse need update matplotlib to 3.6.2

Reason:
Qt for Python Release: 6.4 The new Enum system

Sloution
update matplotlib to 3.6.2

Refer github Isue#24332

Answered By: Jon118
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.