WesR
WesR

Reputation: 1512

Difficulty installing/importing pyqt5 on ubuntu 18.04

Goal: To import pyqt5 into python3 under Ubuntu 18.04

I know there are many answers to similar questions here and elsewhere. I have tried a lot of them, but I don't see anything that exactly matches my situation.

Starting with a clean installation of Ubuntu 18.04, I have tried many ways to be able to have the statement import pyqt5 succeed in Python 3.6.9, which is the default python3 on this distribution.

None of them have succeeded.

In several attempts, reinstalling Ubuntu anew several times, I have had pip3 either experience a 404 downloading https://pypi.org/simple/python3-pyqt5/ and confirmed the error by pasting the URL into Firefox and also getting a 404. (I also checked the Firefox was working fine accessing the Internet)

But usually I run through a process that seems to indicate that pyqt was loaded successfully but was still unable to import it in Python3.

Among the things I've tried from various web sites are:

A more complete list of what I did starting with the reinstall is shown below.

Any suggestions for solutions or what to look at on my system?

I have a bootable memory stick of Ubuntu 18.04.4 that I created last week by downloading the executable from Ubuntu.com. Everything that's packaged with the release seems to work fine, including Python 3.6.9 in other situations. I do virtually nothing except the following steps.

sudo apt install python3-pip

pip3 install humanfriendly   (just to ensure that pip3 works for a simply pypi module)
pip3 install --upgrade pip
pip3 install python3-pyqt5
sudo apt-get install pyqt5-dev-tools  (no complaints from pip3 seems to work okay)
sudo apt-get install qttools5-dev-tools   (seems to work)
in python3 
    >>>import pyqt5
    Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        ModuleNotFoundError: No module named 'pyqt5'

pip3 uninstall pyqt5        (Successfully uninstalled PyQt5-5.14.0)
pip3 install pyqt5 --user   (Successfully installed pyqt5-5.14.1
Try to import in python3 again, same result: "No module named 'pyqt5'"
cd ~/.local/bin
ls 
humanfriendly  pip  pip3  pip3.6  pylupdate5  pyrcc5  pyuic5
(there seems not to be an entry for pyqt5 although there is for some of its stand-alone componentes)

pip3 uninstall pyqt5
ls
humanfriendly  pip  pip3  pip3.6
(what was there went away)

pip3 uninstall pyqt5  (Successfully uninstalled PyQt5-5.14.1)
pip3 install pyqt5 --user (seems to succeed)
try to import in python3 (same error)
pip3 uninstall pyqt5  (Successfully uninstalled PyQt5-5.14.1)

next I tried sudo pip3 uninstall pyqt5
seems to work; not importable in python3; sudo pip3 uninstall

next I created a virtual env, ~/.vents/tevs
activate the venv

pip3 install pyqt5==5.14.0
    Installing collected packages: PyQt5-sip, pyqt5
attempt to import pyqt5 in python3:  fails as before

pip3 uninstall pyqt5
    (Successfully uninstalled PyQt5-5.14.0 after listing thousands of files
     in .venvs/tevs/lib/python3.6/site-packages/PyQt5)

pip3 install python3-pyqt5
    (fails with HTTP Error: 404 Client Error: 
    Not Found for url: https://pypi.org/simple/python3-pyqt5/)

Upvotes: 1

Views: 3883

Answers (2)

tailor_dimpleson
tailor_dimpleson

Reputation: 11

I recently encountered this problem on Ubuntu 20.04 and here is what worked for me.

Firstly, after trying much of what you described I finally decided to run python --version from terminal and the output told me that python 2.7 was the default version on my system. This was the problem.

Secondly, after thinking "sheeeee-*t partner", I checked for any python2 dependencies using apt rdepends python --installed.

Lastly, after confirming that I had no python2 dependencies, I ran sudo apt-get install python-is-python3 in order to make python3 the default version.

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 243927

Several problems should be noted in what the OP notes:

  1. The name of the library does not necessarily match the name of how to import the libraries, on the other hand pip accepts names like pyqt5 and PyQt5 as equivalent, but when you import it you should use PyQt5, not pyqt5, so to test that you have installed you can do the following:

    • Run on the console:

      $ python3 -c "from PyQt5.Qt import PYQT_VERSION_STR; print(PYQT_VERSION_STR)"
      5.14.1
      
    • Run on python console:

      Python 3.8.1 (default, Jan 22 2020, 06:38:00) 
      [GCC 9.2.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from PyQt5.Qt import PYQT_VERSION_STR
      >>> print(PYQT_VERSION_STR)
      5.14.1
      
  2. python3-pyqt5 is the name of the package that offers ubuntu repositories for the version of pyqt5 that your community compiles which is generally a non-current version. So to install pyqt5 there are several methods:

    • Using ubuntu repository: sudo apt-get install python3-pyqt5
    • Using pip: python3 -m pip pyqt5 (You must use sudo if required) or python3 -m pip install pyqt5 --user (This method also applies to virtualenv)

So I can conclude that the OP has successfully installed PyQt5 but has failed to verify if the installation has been correct so I recommend using what is indicated in (1)

Upvotes: 2

Related Questions