PythonQt: How the heck do I actually install it?

642 Views Asked by At

I am trying to install PythonQt for a Qt 5.12 application that I am building on windows. Unfortunately, the documentation is super sparse, and I am not at all familiar with using external libraries in my code, or with building projects at all for that matter.

So, I think I'm close. I have a batch script that I think actually built PythonQt. This leaves me with some dlls, exp files, and lib files. Some variants are named PythonQt_QtAll-Qt5-Python38, based on the python version that I am building for, and some are named PythonQt-Qt5-Python38. I have no idea what the distinction is between these, as the documentation is not clear.

I'm not sure where to go from here, I tried following the instructions in this link: https://doc.qt.io/qtcreator/creator-project-qmake-libraries.html, but I don't seem to have any header files to include. I'm pretty sure that I shouldn't just go plopping random header files out of the PythonQt source and into my application.

Here is my batch file, which I think is working, but nothing is certain:

rem Take current dir
set "crt_dir=%~dp0"

rem Go up one level
for %%I in ("%crt_dir%\..") do set "root=%%~fI"

rem Construct python paths
set "pypath=%root%\Python38-32"
set "pylibpath=%pypath%\libs"

rem Set PYTHON_PATH environmental variable permanently
setx PYTHON_PATH %pypath%
setx PYTHON_LIB %pylibpath%
setx PYTHON_VERSION 38

rem Build PythonQt
rem Fairly sure this is the same command as vcvars32.bat
rem https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2019
call C:\"Program Files (x86)"\"Microsoft Visual Studio"\2017\Community\VC\Auxiliary\Build\vcvarsall.bat x86 && call c:\Qt\5.12.2\msvc2017\bin\qmake && call C:\"Program Files (x86)"\"Microsoft Visual Studio"\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\nmake /f Makefile || echo Build of PythonQt failed, Exit Code is %errorlevel% 

cmd /k

And here is my python.prf file:

# Change this variable to your python version (2.6, 2.7, 3.3, ...)
PYTHON_VERSION=$$(PYTHON_VERSION)
isEmpty( PYTHON_VERSION ) {
  win32:PYTHON_VERSION=38
  unix:PYTHON_VERSION=3.8
}


macx {
  # for macx you need to have the Python development kit installed as framework
  INCLUDEPATH += /System/Library/Frameworks/Python.framework/Headers
  LIBS += -F/System/Library/Frameworks -framework Python
} else:win32 {
  # for windows install a Python development kit or build Python yourself from the sources
  # Make sure that you set the environment variable PYTHON_PATH to point to your
  # python installation (or the python sources/header files when building from source).
  # Make sure that you set the environment variable PYTHON_LIB to point to
  # the directory where the python libs are located.
  #
  # When using the prebuild Python installer, this will be:
  # set PYTHON_PATH = c:\Python26
  # set PYTHON_LIB  = c:\Python26\libs
  #
  # When using the python sources, this will be something like:
  # set PYTHON_PATH = c:\yourDir\Python-2.6.1\
  # set PYTHON_LIB  = c:\yourDir\Python-2.6.1\PCbuild8\Win32

  # check if debug or release
  CONFIG(debug, debug|release) {
    DEBUG_EXT = #_d 
  } else {
    DEBUG_EXT = 
  }

  win32:INCLUDEPATH += $$(PYTHON_PATH)/PC $$(PYTHON_PATH)/include
  win32:LIBS += $$(PYTHON_LIB)/python$${PYTHON_VERSION}$${DEBUG_EXT}.lib
} else:unix {
  # on linux, python-config is used to autodetect Python.
  # make sure that you have installed a matching python-dev package.

  unix:LIBS += $$system(python$${PYTHON_VERSION}-config --ldflags)
  unix:QMAKE_CXXFLAGS += $$system(python$${PYTHON_VERSION}-config --includes)
}

And my common.prf:

# depending on your Qt configuration, you want to enable or disable
# one of the release/debug builds (if all three lines are commented,
# the default of your Qt installation will used)

# build with both debug and release mode
#CONFIG += debug_and_release build_all

# build with release mode only
#CONFIG += release

# build with debug mode only
#CONFIG += debug

# for all debug builds, add "_d" extension to target
CONFIG(debug, debug|release) {
  TARGET = $${TARGET}_d 
}

# Try files that are generated by the user:
PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp

!exists($$PYTHONQT_GENERATED_PATH) {
  # If no files are generated, try the checked-in wrappers:
  PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_$${QT_MAJOR_VERSION}$${QT_MINOR_VERSION}

  !exists($$PYTHONQT_GENERATED_PATH) {
    contains( QT_MAJOR_VERSION, 5 ) {
      contains( QT_MINOR_VERSION, 10 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_56
      }
      else:contains( QT_MINOR_VERSION, 11 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_511
      }
      else:contains( QT_MINOR_VERSION, 12 ) {
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_511
      }
      else:contains( QT_MINOR_VERSION, 1 ) {
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_50
      }
      else:contains( QT_MINOR_VERSION, 2 ) {
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_50
      }
      else:contains( QT_MINOR_VERSION, 3 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_53
      }
      else:contains( QT_MINOR_VERSION, 4 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_54
      }
      else:contains( QT_MINOR_VERSION, 5 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_54
      }
      else:contains( QT_MINOR_VERSION, 6 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_56
      }
      else:contains( QT_MINOR_VERSION, 7 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_56
      }
      else:contains( QT_MINOR_VERSION, 8 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_56
      }
      else:contains( QT_MINOR_VERSION, 9 ) { 
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_56
      }
      else {
        PYTHONQT_GENERATED_PATH = $$PWD/../generated_cpp_56
      }
    }
  }
}

VERSION = 3.2.0

# Suppress the automatic version number appended to the DLL file name on Windows.
win32: CONFIG += skip_target_version_ext

In summary, I believe that I've built PythonQt, and I have a bunch of executables for the PythonQt example projects, and a dll, exp, and lib file, for PythonQt-Qt5-Python38 and PythonQt_QtAll-Qt5-Python38, respectively. Where do I go from here?

1

There are 1 best solutions below

0
On

the answer was in the examples the whole time. Once PythonQt is built, you simple need to include all relevant .prf files from it in the .pro file for your project, e.g.:

include ( ./third_party/PythonQt/build/common.prf )  
include ( ./third_party/PythonQt/build/PythonQt.prf )  
include ( ./third_party/PythonQt/build/PythonQt_QtAll.prf )  

Still would welcome any answers about how to compile PythonQt into a library, but my original question has been resolved.