Why is my PyQt5 and PyQt5-tools not running on my windows terminal/python file? PyQt5 Module Not Working Python Error Solved

 

'PyQt5 Module in python file gives error when run on my windows terminal' - WHY? What is the solution? One Solution that worked for me -

Checking the version of your python and pip and the version of PyQt5 you are installing! Even if it is said or the error states that it might not be a fault of pip or python, they are up to date still. Solutions below :

Current Python Version is Python 3.11 while 3.10 is the stable version (3.10.7) which may be many of you are using currently. Pip is of version 21 or 22 if updated. Nothing to do with pip actually, its on your Python Version

To Install and Import PyQt5 module in your python program :

    pip install PyQt5

    import PyQt5

        or

    import Qt5Designer (pip install first)

WHAT ARE PyQt5 and PyQt5-tools?

PyQt5 is the latest version of a GUI widgets toolkit developed by Riverbank Computing. It is a Python interface for Qt, one of the most powerful, and popular cross-platform GUI library. PyQt5 is a blend of Python programming language and the Qt library. This introductory tutorial will assist you in creating graphical applications with the help of PyQt.

The PyQt5 wheels do not provide tools such as Qt Designer that were included in the old binary installers. This package aims to provide those in a separate package which is useful for developers while the official PyQt5 wheels stay focused on fulfilling the dependencies of PyQt5 applications.

You can install pip on your computer by typing the following pip install command in your command prompt (cmd)/windows terminal/powershell :
yourenv/Scripts/pip.exe install pyqt5-tools~=5.15

You will generally install pyqt5-tools using pip install. In most cases you should be using virtualenv or venv to create isolated environments to install your dependencies in. The above command assumes an env in the directory yourenv. The ~=5.15 specifies a release compatible with 5.15 which will be the latest version of pyqt5-tools built for PyQt5 5.15. If you are using a different PyQt5 version, specify it instead of 5.15. PyPI keeps a list of all available versions.

A program is provided available as Scripts/pyqt5-tools.exe. There are subcommands provided for each of Designer, QML Scene, and the QML Test Runner. These wrapper commands provide additional functionality related to launching the underlying programs. A larger set of Qt application are available as subcommands of the Scripts/qt5-tools.exe program. In both cases, passing --help will list the available subcommands.

Additionally, each pyqt5-tools subcommand listed below includes a parameter to run a basic example which can be used to see if the plugins are working. These examples are not intended to be used as examples of good code.

Each subcommand searches up the filesystem tree from your current working directory to find a .env file and loads it if found. If found, the environment variable DOT_ENV_DIRECTORY will be set to the directory containing the .env file. With this extra variable you can specify paths relative to the .env location.

PYQTDESIGNERPATH=${PYQTDESIGNERPATH};${DOT_ENV_DIRECTORY}/path/to/my/widgets

Designer

There is a Scripts/pyqt5-tools.exe designer.exe entry point that will help fill out PYQTDESIGNERPATH from either command line arguments or a .env file. Unknown arguments are passed through to the original Qt Designer program.

Usage: pyqt5-tools designer [OPTIONS]

Options:
  -p, --widget-path DIRECTORY     Paths to be combined with PYQTDESIGNERPATH
  --example-widget-path           Include the path for the pyqt5-tools example
                                  button (c:\users\sda\testenv\lib\site-
                                  packages\pyqt5_plugins)

  --designer-help                 Pass through to get Designer's --help
  --test-exception-dialog         Raise an exception to check the exception
                                  dialog functionality.

  --qt-debug-plugins / --no-qt-debug-plugins
                                  Set QT_DEBUG_PLUGINS=1
  --help                          Show this message and exit.

If you want to view the generated code from within Designer, you can run Scripts/pyqt5-tools.exe installuic and it will copy pyuic5.exe such that Designer will use it and show you generated Python code. 


YOU CAN THEREFORE, CHANGE A GUI FILE (.ui file) which you have built in your QtDesigner TO A PYTHON FILE (.py file) with pyuic5 FROM YOUR PyQt5-tools.


My Solution which worked for me :

If you see the documentation of PyQt5-tools and PyQt5 in PyPi's official website, you will find PyQt5 and PyQt5-tools are only available for the following Python Versions :

    Python 3.7, Python 3.8 and Python 3.9

PyPI version supported Python versions source on GitHub

and when I looked at my Python Version, that I was using in my file, I found it was Python 3.10.7 Stable

WHAT TO DO NOW? How to change Python Version?
To run PyQt5 you will need to install Python 3.9.7 or any lower version which is available in the official python org website, I used Python 3.9.7 Version, 64bits, I downloaded it, ran the setup, installed it, selected the python interpretr as Python 3.9.7 and used it. You will have to downgrade your python version. It is easy :

From the official python distribution, download python 3.9.7, install and run it.
Change the current version in your IDE by selecting interpreter
Open Command Prompt and check the python version you have, by typing :

    python --version

Your python 3.10 will still be there and you can use it anytime you want.

SECOND STEP IN OUR SOLUTION :

NOW, INSTALL  ALL YOUR MODULES YOU HAD AND ALSO YOU CAN INSTALL PyQt-5 and PyQt5-tools, you will just need another module - Click, version 7.1.2

You can do this by : 

    In your CMD, Type :
        pip install click==7.1.2

AFTER INSTALLING TRY INSTALLING PyQt-5

It should be working now!. This two are the main reasons/problems why PyQt5 and other modules sometimes doesn't work.

This error is thus either because of your unsupported Python Version or Unsupported Click Module Version as a pre-requisite to any other module, which both them, you need to downgrade their versions.

What is a Python Module?

Nowadays, the Python programming language becomes one of the most popular languages. When we write the codes for Production level Data Science Projects, what happens is that our Python code grows in size, and as a result most probably it becomes unorganized over time. So, keeping your code in the same file as it grows makes your code difficult to maintain and debug.

In Python, Modules are simply files with the “.py” extension containing Python code that can be imported inside another Python Program.

In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.

With the help of modules, we can organize related functions, classes, or any code block in the same file. So, It is considered a best practice while writing bigger codes for production-level projects in Data Science is to split the large Python code blocks into modules containing up to 300–400 lines of code.

The module contains the following components:

  • Definitions and implementation of classes,
  • Variables, and
  • Functions that can be used inside another program.

How to create Python Modules?

To create a module, we have to save the code that we wish in a file with the file extension “.py”. Then, the name of the Python file becomes the name of the module.

For Example,

In this program, a function is created with the name “welcome” and save this file with the name mymodule.py i.e. name of the file, and with the extension “.py”.

We saved the following code in a file named mymodule.py

def welcome(name):
  print("Hello, " + name +" to NeuzFyunda")

How to use Python Modules?

To incorporate the module into our program, we will use the import keyword, and to get only a few or specific methods or functions from a modulewe use the from keyword.

NOTE: When we are using a function from a module, then we use the following syntax:

module_name.function_name

Now to use the module which we have just created, we are using the import statement:

For Example,

In this example, we will Import the module named mymodule, and then call the welcome function with a given argument:

import mymodule
mymodule.welcome("NeuzFyunda")

Output:

Hello, NeuzFyunda to NeuzFyunda

 

Post a Comment

Previous Post Next Post