2

I have virtual environment created for python 3.6. And I have install python3-pyqt5.qtwebkit package with apt-get. The package have installed in global python3.5 and its working, but if I try to import it from venv python3.6 there is import error... How can I install this package in my virtual environment? Or if this is not possible how can I use global package installed in 3.5 in my virtualenv 3.6 python.

Running with global python3.5

(virtualenv) 
~/vcs-ss/sst  master ✗                                                                                                                         5d ▴ ⚑ ✚ ◒  
▶ python3.5 -c 'import PyQt5.QtWebKit; print(PyQt5.QtWebKit.__file__)'
/usr/lib/python3/dist-packages/PyQt5/QtWebKit.cpython-35m-x86_64-linux-gnu.so

Running with python3.6 from venv

(virtualenv) 
~/vcs-ss/sst  master ✗                                                                                                                         5d ▴ ⚑ ✚ ◒  
▶ python3.6 test/e2e/browser.py     
Traceback (most recent call last):
  File "test/e2e/browser.py", line 8, in <module>
    from PyQt5.QtWebKit import *
ModuleNotFoundError: No module named 'PyQt5.QtWebKit'
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 "test/e2e/browser.py", line 8, in <module>
    from PyQt5.QtWebKit import *
ModuleNotFoundError: No module named 'PyQt5.QtWebKit'

2 Answers2

1

In order to use system packages inside a virtual environment, you have to use --system-site-packages option when creating virtual env:

virtualenv --python=python3.6 --system-site-packages env

The same switch is available in Python3's venv.

Much better option is (if applicable in your case) to install all packages inside your virtual env, using pip.

randomir
  • 17,989
  • 1
  • 40
  • 55
  • I have `virtualenv -p python3.6 --distribute --system-site-packages venv` but still it seems that it recognize system site packages from global 3.6 python. –  Jul 10 '17 at 13:01
  • From your updated question, looks like `python-apt` package is missing. Try: `apt-get install python-apt`. – randomir Jul 10 '17 at 13:13
  • Have you tried `apt-get remove --purge python-apt` and then reinstall? Like this [answer (and comments)](https://stackoverflow.com/a/13717755/404556) suggest. – randomir Jul 10 '17 at 13:16
0

If you look at the contents of the package (using dpkg --listfiles python3-pyqt5.qtwebkit) you'll probably see something like this:

/.
/usr
/usr/lib
/usr/lib/python3
/usr/lib/python3/dist-packages
/usr/lib/python3/dist-packages/PyQt5
/usr/lib/python3/dist-packages/PyQt5/QtWebKit.cpython-35m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/PyQt5/QtWebKit.pyi
/usr/lib/python3/dist-packages/PyQt5/QtWebKitWidgets.cpython-35m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/PyQt5/QtWebKitWidgets.pyi
/usr/share
/usr/share/doc
/usr/share/doc/python3-pyqt5.qtwebkit
/usr/share/doc/python3-pyqt5.qtwebkit/copyright
/usr/share/doc/python3-pyqt5.qtwebkit/changelog.Debian.gz

The package installs binary modules compiled for a specific python version (in this case: cpython-35m), you won't be able to use those from a python3.6 installation/venv directly because of the incompatible binary names.

For that to work you'd need to symlink all the .so files in the PyQt5 directory to your venv and there change cpython-35m-x86_64-linux-gnu to cpython-36m-x86_64-linux-gnu. Same for the sip libraries, as those are requiered by PyQt5. And that will only work if the ABI is compatible between the two versions.

A quick test with the versions mentioned above shows it works at least for simple scripts, but I can't give any guarantee.

mata
  • 67,110
  • 10
  • 163
  • 162
  • `from PyQt5.QtWebKit import * ImportError: /usr/lib/x86_64-linux-gnu/libQt5Network.so.5: symbol _Z15qIsEffectiveTLDRK7QString, version Qt_5 not defined in file libQt5Core.so.5 with link time reference` –  Jul 10 '17 at 14:26