59

I'm using Virtualenv with profit on my development environment with web.py, simplejson and other web oriented packages.
I'm going to develop a simple python client using Qt to reuse some Api developed with web.py.

Does anybody here had succesfully installed PyQt4 with Virtualenv?
Is it possible?

I've downloaded all the binaries and have PyQt4 installed globally on my python2.6 directory.
If I don't use --no-site--packages option, Virtualenv correctly includes PyQt4 in my new sandbox but, obviously, with all the global packages that I don't need.

Is there a clean way to prepare a new sandbox with --no-site--packages option and then add PyQt4 or PySide using pip, easy_install or some other magic trick?

systempuntoout
  • 71,966
  • 47
  • 171
  • 241

13 Answers13

52

It should be enough to create an empty virtualenv and then copy the contents of the .../site-packages/PyQt4 directories into it.

I suggest to install PyQt4 once globally, make a copy of the directory, uninstall it and then use this trick to create VEs.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    Thanks for your help. Copying the directory i can import PyQt4 from command line without error but i'm missing sip (ImportError: No module named sip ). Do i need to copy that too?Do you think pyuic4 will work? – systempuntoout Dec 25 '09 at 23:48
  • 5
    Yes, that should fix it. I missed sip because it's in site-packages (no subdirectory). As for pyuic4, it should work. – Aaron Digulla Dec 26 '09 at 19:08
  • 1
    I copied sip.so inside site-packages under sandbox python directory and it worked. – systempuntoout Dec 27 '09 at 06:29
  • Why wouldn't running standard installation procedure from wihtin virtualenv work? – Piotr Dobrogost Jan 09 '11 at 10:56
  • 3
    @Piotr: Because the standard installation procedure on Windows opens a UI installer that doesn't care about virtualenv :-( On Linux, it depends whether you install it yourself or from a package. Packages also ignore virtualenv. I suggest that you try it and post an answer how to do it with `easy_install`. – Aaron Digulla Jan 10 '11 at 10:03
  • My easy_install fails with: "UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 594: ordinal not in range(128)" when using virtualenv on win7 :( – tylerthemiler Mar 29 '12 at 19:04
  • Make sure your path only has US-ASCII characters in it (no umlauts) and open a bug report against `easy_install` (with the complete stack trace) – Aaron Digulla Mar 30 '12 at 09:15
  • 2
    You don't say if you're using Windows or Unix. If you're on Windows and have a bdist_wininst installer (not MSI) you can use the wheel project's "wheel convert" command to convert the exe into a wheel. You can then use "wheel install" to install the wheel into a virtualenv. The convert step is a one-off so you can retain the generated wheel for all your future virtualenvs. – Paul Moore Jul 12 '13 at 13:04
46

I have the same problem. I use virtualenvwrapper, so I wrote this script to create a link to PyQt in every new virtual environment. Maybe is useful for someone else:

#!/bin/bash
# This hook is run after a new virtualenv is activated.
# ~/.virtualenvs/postmkvirtualenv

LIBS=( PyQt4 sip.so )

PYTHON_VERSION=python$(python -c "import sys; print (str(sys.version_info[0])+'.'+str(sys.version_info[1]))")
VAR=( $(which -a $PYTHON_VERSION) )

GET_PYTHON_LIB_CMD="from distutils.sysconfig import get_python_lib; print (get_python_lib())"
LIB_VIRTUALENV_PATH=$(python -c "$GET_PYTHON_LIB_CMD")
LIB_SYSTEM_PATH=$(${VAR[-1]} -c "$GET_PYTHON_LIB_CMD")

for LIB in ${LIBS[@]}
do
    ln -s $LIB_SYSTEM_PATH/$LIB $LIB_VIRTUALENV_PATH/$LIB 
done

link to gist

José Luis
  • 3,713
  • 4
  • 33
  • 37
  • Hi, I found your answer via google, via other's comment, I reckon your script would work. I know very very little about bash(and I am using zsh), could you tell me how to run this script, please? – Sean Dec 01 '12 at 06:48
  • @Sean The script should be run by virtualenvwrapper automatically when you create a new virtualenv. If you have only zsh installed, you can replace the first line of the script `#!/bin/bash` with the path to your zsh executable, probably `#!/bin/zsh`, you can get this path with `echo $PATH`. This first line is the shebang, look here for an explanation: [shebang wikipedia](http://en.wikipedia.org/wiki/Shebang_(Unix)) I don't use zsh, but probably the script works also with zsh – José Luis Dec 01 '12 at 11:05
  • 1
    If you get `VAR: bad array subscript` when using this, you are probably using an old version of `bash` (Apple still ships 3.2 with 10.8). It's easy to `brew install bash` and use an upgraded version though. – dmnd Jun 30 '13 at 20:24
  • 1
    This doesn't work. Trying to do 'from PyQt4 import pyqtconfig' (which is what matplotlib does to detect PyQt4) results in 'No module named sipconfig' – Thomas Johnson Sep 24 '13 at 03:38
  • 2
    Hi, I love your script and have put it in a [gist of my own](https://gist.github.com/Midnighter/5115f13d34f0b38197df). My version should be a bit more robust and also include `sipconfig.py` which makes matplotlib work just fine. Cheers. – Midnighter Jan 11 '15 at 20:36
29

Linux debian, python 2.7:

  • Install python-qt4 globaly: sudo apt-get install python-qt4
  • Create symbolic link of PyQt4 to your virtual env ln -s /usr/lib/python2.7/dist-packages/PyQt4/ ~/.virtualenvs/myEnv/lib/python2.7/site-packages/
  • Create symbolic link of sip.so to your virtual envln -s /usr/lib/python2.7/dist-packages/sip.so ~/.virtualenvs/myEnv/lib/python2.7/site-packages/
Simanas
  • 2,793
  • 22
  • 20
8

For those who want to use PyQt4 in a Python 3 virtualenv (on OSX) you first install PyQt4 and SIP (I will use homebrew)

$ brew install python3
$ brew install sip --with-python3
$ brew install pyqt --with-python3

Then create your virtual environment

$ virtualenv ...

Finally symlink (change the versions of SIP, PyQt4 and Python for those installed on your machine)

$ ln -s /usr/local/Cellar/sip/4.15.5/lib/python3.4/site-packages/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.4/site-packages/
$ ln -s /usr/local/Cellar/pyqt/4.10.4/lib/python3.4/site-packages/PyQt4/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.4/site-packages/PyQt4
jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
7

I asked if it's possible to install PySide from within virtualenv on irc.freenode.net #pyside channel and got positive answer from hugopl. So I followed instructions from PySide Binaries for Microsoft Windows and it worked. The output is below.

Z:\virtualenv\pyside>Scripts\activate.bat

(pyside) Z:\virtualenv\pyside>where python
Z:\virtualenv\pyside\Scripts\python.exe

(pyside) Z:\virtualenv\pyside>easy_install PySide
install_dir Z:\virtualenv\pyside\Lib\site-packages\
Searching for PySide
Reading http://pypi.python.org/simple/PySide/
Reading http://www.pyside.org
Reading http://www.pyside.org/files/pkg/
Best match: PySide 1.0.0beta1qt471
Downloading http://www.pyside.org/files/pkg/PySide-1.0.0beta1qt471.win32-py2.6.exe
Processing PySide-1.0.0beta1qt471.win32-py2.6.exe
Deleting c:\users\piotr\appdata\local\temp\easy_install-fvfa7e\PySide-1.0.0beta1qt471-py2.6-win32.egg.tmp\EGG-INFO\scripts\py
ide-uic-script.py
Deleting c:\users\piotr\appdata\local\temp\easy_install-fvfa7e\PySide-1.0.0beta1qt471-py2.6-win32.egg.tmp\EGG-INFO\scripts\py
ide-uic.exe
creating 'c:\users\piotr\appdata\local\temp\easy_install-fvfa7e\PySide-1.0.0beta1qt471-py2.6-win32.egg' and adding 'c:\users\
iotr\appdata\local\temp\easy_install-fvfa7e\PySide-1.0.0beta1qt471-py2.6-win32.egg.tmp' to it
creating z:\virtualenv\pyside\lib\site-packages\PySide-1.0.0beta1qt471-py2.6-win32.egg
Extracting PySide-1.0.0beta1qt471-py2.6-win32.egg to z:\virtualenv\pyside\lib\site-packages
Adding PySide 1.0.0beta1qt471 to easy-install.pth file
Installing pyside-uic-script.pyc script to Z:\virtualenv\pyside\Scripts
Installing pyside_postinstall.py script to Z:\virtualenv\pyside\Scripts
Installing pyside_postinstall.pyc script to Z:\virtualenv\pyside\Scripts
Installing pyside-uic-script.py script to Z:\virtualenv\pyside\Scripts
Installing pyside-uic.exe script to Z:\virtualenv\pyside\Scripts

Installed z:\virtualenv\pyside\lib\site-packages\pyside-1.0.0beta1qt471-py2.6-win32.egg
Processing dependencies for PySide
Finished processing dependencies for PySide

(pyside) Z:\virtualenv\pyside>python Scripts\pyside_postinstall.py -install
Generating file Z:\virtualenv\pyside\Scripts\qt.conf...
The PySide extensions were successfully installed.
David Fraser
  • 6,475
  • 1
  • 40
  • 56
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
  • This fails with "UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 594: ordinal not in range(128)" for me. – tylerthemiler Mar 29 '12 at 19:06
  • Note that this _only_ works on Windows. http://bugs.pyside.org/show_bug.cgi?id=943 – James Jul 10 '12 at 17:42
6

Let's assume your virtualenv is named myProject and you're using virtualenvwrapper. A Unix platform is also assumed.

$ workon myProject
$ pip install --no-install SIP
$ pip install --no-install PyQt
$ cd ~/.virtualenvs/myProject/build/SIP
$ python configure.py
$ make
$ make install
$ cd ~/.virtualenvs/myProject/build/PyQt
$ python configure.py
$ make
$ make install
$ cd && rm -rf ~/.virtualenvs/myProject/build # Optional.
  • On python3 venvs with recent pip, it requires additional flags to pip - something like this: pip install --no-install --allow-external SIP --allow-unverified SIP SIP, but the generated Makefiles are still a little broken. – RobotHumans Jul 02 '14 at 08:42
6

Easiest way is to install this : vext.pyqt4

This will add the single system PyQt4 package to your virtualenv.

Ubuntu 16.04 usage:

sudo apt install python3-pyqt4
mkvirtualenv --python=python3.5 venv
pip install --no-use-wheel vext.pyqt4
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Arun
  • 836
  • 1
  • 12
  • 19
  • This seems to be the 'correct' answer, in that it allows you to use existing package management infrastructure (pip or pipenv) within the virtualenv without manually copying or symlinking files. It's a shame it's not the accepted answer. FWIW, this works for qt5 via vext.pyqt5 as well, and works via pipenv install (with a warning, but functionality seems OK) – bjarchi May 23 '18 at 00:49
4

For PySide 1.2.1 and Ubuntu 12.4

Install compilers, Qt related, python sources

sudo apt-get install cmake qt4-qmake qt-sdk python-dev

Create virt env withvirtualenvwrapper

Be ready for compiling Qt (~30 min)

$ mkvirtualenv ve_name
(ve_name)$ pip install PySide

Test

$ python -c "from PyQt4 import QtCore; print QtCore.PYQT_VERSION_STR"
4.9.1
b1_
  • 2,069
  • 1
  • 27
  • 39
4

If you installed pyqt via brew on a Mac (specifically I'm on Mavericks):

ln -s /usr/local/Cellar/sip/4.15.2/lib/python2.7/site-packages/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python2.7/site-packages
ln -s /usr/local/Cellar/pyqt/4.10.3/lib/python2.7/site-packages/PyQt4/ ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python2.7/site-packages/PyQt4
pip install pygments pyzmq 

Of course those version folder numbers may change over time.

James Errico
  • 5,876
  • 1
  • 20
  • 16
3

What worked for me was copying /PyQt4/ and sip.x86_64-linux-gnu.so from /usr/lib/python2.7/dist-packages to <VIRTUALENV>/lib/python2.7/site-packages and changing the ownership permissions on these copied files (since the copies were owned by root).

heisen
  • 51
  • 3
2

Symbolic links may work.

I use Linux (Debian/sid), python 3.4, PySide, virtualenv, and PyCharm (IDE), but these same instructions will work for any package and development environment.

Symbolic links between /usr/lib/python3/dist-packages/PySide/ $VIRTUAL_ENV/lib/python3.4/site-packages/PySide/ work for me.

cd $VIRTUAL_ENV/lib/python3.4/site-packages/
mkdir PySide
cd PySide`
for x in /usr/lib/python3/dist-packages/PySide/* ; do ln -s $x ; done

Interestingly, if I symbolic link only the PySide directory, it does not work. I need to symlink each file within the directory. That may be a limitation of PyCharm (my IDE) -- I don't know.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
2

Expanding on Aaron Digulla's answer, using git to get the file list right can be really handy. I usually do something like this (from an msysGit shell):

# Create temp git repo for the pristine Python installation
$ cd /c/Python27
$ git init -q
$ git add .
$ git commit -qm "Initial commit"

Then run the installer for PyQt4 (or whatever). After that, make a tarball of the files that the installer added and delete the temp git repo, as follows:

# Stage the PyQt4-installed files and feed a list of their names to tar
# (note that there's no need to actually commit them)
$ git add --all
$ git diff --cached --name-only | tar -jcf pyqt4.tar.bz2 --files-from=-
$ rm -rf .git

Then you can run PyQt4's uninstaller (if you don't want to clutter up your system Python), and simply untar pyqt4.tar.bz2 into your virtualenv folder. If you're already comfortable using git, this is a great way to ensure you get all the PyQt4-installed files.

NOTE: Installing PyQt4 using the packaged installer also installs SIP. If you actually want to use this SIP to create bindings for your own C/C++ code inside your virtualenv, you'll want to modify the paths in the sipconfig.py file after you copy it over. Otherwise, SIP's build system will still be pointing at the system Python folder (e.g., C:\Python32 or whatever), which won't work if you delete all the PyQt4-installed files from there. (If you don't have any intention of using SIP yourself, you can probably skip this.)

matt wilkie
  • 17,268
  • 24
  • 80
  • 115
evadeflow
  • 4,704
  • 38
  • 51
0

Try this one: pip install python-qt5

Andrey Nikishaev
  • 3,759
  • 5
  • 40
  • 55