title: Runtime error when calling a .exe made with PyInstaller including PyQt4
body: <p>So, I have two <em>.py</em> files, one generated by <a href="http://pyqt.sourceforge.net/Docs/PyQt4/designer.html" rel="nofollow noreferrer">QtDesigner</a> and another which basically implements the functionality of the GUI. Using, <a href="https://pyinstaller.readthedocs.io/en/stable/index.html" rel="nofollow noreferrer">pyinstaller</a>, I generated a <em>.exe</em> file to use it on systems without python and the associated libraries. </p>
  
  <p>The command: <code>pyinstaller my_script.py</code> runs fine without any errors. </p>
  
  <p>The problem occurs when I run the <em>.exe</em> file.</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Qt: Untested Windows version 10.0 detected!
    Traceback (most recent call last):
      File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py", line 41, in 
    ImportError: No module named 'PySide'</p>
    
    <p>During handling of the above exception, another exception occurred:</p>
    
    <p>Traceback (most recent call last):
      File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py", line 43, in 
      File "", line 2237, in _find_and_load
      File "", line 2226, in _find_and_load_unlocked
      File "", line 1191, in _load_unlocked
      File "", line 1161, in _load_backward_compatible
      File "C:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
        module = loader.load_module(fullname)</p>
    
    <p>RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class
    [11364] Failed to execute script pyi_rth_qt4plugins</p>
  </blockquote>
  
  <p>So I tried to find a solution to this. These are the solutions I tried:</p>
  
  <p><a href="https://stackoverflow.com/questions/30697089/how-to-force-pyqt5-use-for-qobject-class">How to force PyQt5 use for QObject class?</a> - simply make the <code>PyQt</code> import as the first statement doesn't resolve the issue.</p>
  
  <p><a href="https://github.com/tzutalin/labelImg/issues/268" rel="nofollow noreferrer">https://github.com/tzutalin/labelImg/issues/268</a> - Here it is recommended to remove <strong>PyQt4</strong> and <em>use only</em> <strong>PyQt5</strong>. I do have both of them on my system, some projects rely on <strong>PyQt5</strong> and some on <strong>PyQt4</strong> hence I don't want remove the latter. Also, <em>there has to be another solution</em>, is making me not do this.</p>
  
  <p><a href="https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000551170-PyQt4-and-PyQt5-collisions-in-PyCharm-2017-2-1-when-debugging-QGIS-application" rel="nofollow noreferrer">https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000551170-PyQt4-and-PyQt5-collisions-in-PyCharm-2017-2-1-when-debugging-QGIS-application</a> - This was a similar error, so I added:
  <code>
  matplotlib.rcParams['backend'] = 'Qt4Agg'
  matplotlib.rcParams['backend.qt4'] = 'PyQt4'</code></p>
  
  <p>to my imports, still didn't work.</p>
  
  <p><em>Note:</em> 
  I am using:</p>
  
  <p>PyCharm 2018.1 (Community Edition)</p>
  
  <p>Build #PC-181.4203.547, built on March 26, 2018</p>
  
  <p>JRE: 1.8.0_152-release-1136-b20 amd64</p>
  
  <p>JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o</p>
  
  <p>Windows 10 10.0</p>
  
  <p>and the code works fine in the IDE.</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>My imports are:</p>
  
  <pre><code>from PyQt4 import QtCore, QtGui
  from matplotlib.backends.backend_qt4agg import FigureCanvasQTagg as Canvas
  </code></pre>
  
  <p>I am not adding any other import statements related to Qt.</p>
  
  <p><strong>EDIT - 2</strong>:</p>
  
  <p>Trying <strong>cx_Freeze</strong> instead of <strong>PyInstaller</strong>, here is the setup file. </p>
  
  <pre><code>import sys
  from cx_Freeze import setup, Executable
  
  base = None
  if sys.platform == "win32":
      base = "Win32GUI"
  
  additional_mods = ['numpy.core._methods', 'numpy.lib.format', 
  'numpy._distributor_init']
  
  setup( name="ASCII2fig",
         version = "0.1",
         description = "GUI",
         options = {'build_exe': {'includes': additional_mods}},
         executables = [Executable("ASCII2figALL_main_edited.py", base=base)])
  </code></pre>
  
  <p>I added the <code>additional_mods</code> after executing the script once following <code>ImportError</code>, which are non-stop. Any ways to hack and find which libraries I should mention explicitly?</p>
  
  <p>Additionally, I also tried to check which <em>libraries</em> are actually being imported when I run my main script with Qt using:</p>
  
  <pre><code>from modulefinder import ModuleFinder
  
  filename = "ASCII2figALL_main_edited.py"
  finder = ModuleFinder()
  finder.run_script(filename)
  for name, mod in finder.modules.items():
      print(name)
  </code></pre>
  
  <p>and apparently, it is importing PyQt5 <em>internally</em>. As mentioned before, I have <strong>NO</strong> import statements mentioning PyQt5.</p>
  
  <p><strong>Edit - 3</strong></p>
  
  <p>So, I changed the code to <em>pure</em> <strong>PyQt5</strong>, updated the <em>pyinstaller</em> to the latest version - 3.4, and now there is a new issue where it doesn't find the Qt plugins. It is still somehow importing PyQt4 and I don't know where.</p>