[PyQt] Detecting which Python version is used by SIP and PyQt

Hans-Peter Jansen hpj at urpla.net
Sat Oct 23 12:25:39 BST 2010


On Saturday 23 October 2010, 02:54:40 Xavion wrote:
> Arch Linux has recently switched its 'python' package to the Python
> v3 branch.  SIP and PyQt still require the 'python2' package,
> probably because the unpatched D-Bus Python bindings currently won't
> work with Python v3.
>
> I'm guessing that some other Linux distributions will continue using
> Python v2 for a while yet.  Due to this uncertainty, I'm thinking
> that the Makefiles of my applications should detect whether PyQt is
> attached to Python v2 or v3.

How about:

python -c 'import sys, sipconfig; sys.stdout.write("%s\n" % (sipconfig._pkg_config['py_version'] >> 16))'

but hold on a second.

Phil, there's an issue in this area:

$ python -c 'import sys, sipconfig; sys.stdout.write("%s\n" % (sipconfig._pkg_config['py_version'] >> 16))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'py_version' is not defined
$ python
Python 2.6 (r26:66714, Mar 30 2010, 00:30:21) 
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, sipconfig; sys.stdout.write("%s\n" % (sipconfig._pkg_config['py_version'] >> 16))
2
$ pyqtver.py
python: 2.6
sip: 4.11.2
qt4: 4.6.3
pyqt4: 4.8

> Doing so will save me from having to hard-code something like
> "#!/usr/bin/env python2" into the main executable file, only to be
> disappointed after finding out that some Linux distributions have
> already built PyQt on Python v3.

For a transition phase of a couple of years, I would do it the other 
way around. Your distribution should have created a python3 symlink,
hence, if you code for python3, use #!/usr/bin/env python3, given it 
isn't compatible with python < 3, otherwise use #!/usr/bin/env python.

> I realise that I could just search for the "/site-packages/PyQt4/"
> directory, but I want a much cleaner solution that won't slow down
> the installation processes.  So, how should my Makefiles determine
> this necessary information?

You may find the attached script helpful. It was written with 
parsability in mind, but also needs to be called with python3 to check, 
if pyqt is available for that installation (too). See above.

Pete
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyqtver.py
Type: application/x-python
Size: 870 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101023/ca16b5e4/attachment.bin>


More information about the PyQt mailing list