Windows Full Python Integration

From Scribus Wiki
Jump to navigation Jump to search
This article is part of the Scripts series.

Please see at the bottom of this page how simple it can be to use a system version of Python with Scribus.

How to replace the Python Interpreter shipped by Scribus 1.5.x for Windows

The Python interpreter shipped with the Windows build of Scribus 1.5.x is limited by the Python at hand when the build is made by the Scribus developer. The Python interpreter runs off a dynamic linked library (DLL) that is shipped with Scribus. Unfortunately, with a DLL, you cannot expand it and integrate various Python packages like you may in a normal Python environment. The DLL is, in essence, frozen. You may want to have a Python Interpreter that can be expanded to include other packages, e.g. the PostgreSQL database package, which is not included in the DLL version. The way to accomplish this is to replace the DLL interpreter with a full Windows installation of Pyton. This gives you a more robust Python environment allowing you to add whatever packages you want.

You can replace the Python interpreter by following the steps below.

First, determine what version of Python has been included with your build of Scribus for Windows:

   Main Menu
   Scripter->Show Console

In top window of Script Console dialog type:

    import sys
    print(sys.version)

Click Script->Run or press F9. The lower window will produce something like:

    2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]

This is the version of Python that has been bundled with your Scribus. You want to know this so that you can download the full version and not introduce any compatibility issues between Scribus and the Python you're going to integrate.

At https://www.python.org/download/releases/2.7.8/ there are two choices for either the 32 bit or 64 bit platform. For instance, for the 64 bit platform, there are:

    Windows X86-64 MSI Installer (2.7.8) [1]
    Windows X86-64 MSI program database (2.7.8) [1]

You want the Installer. The "program database" is a larger distribution that is meant to include debugging routines. Download the appropriate installer, we'll run it later down below.

Open a DOS console and type "set" to obtain a listing of your default environmental variables. We are interested in the "Path" variable. For instance your Path may have two versions of Python already in it (the ellipsis represents other values not relevant to Python):

   Path=...C:\Python32\;C:\Python32\Scripts;...C:\Python27\;C:\Python27\Scripts;...

You do not want to destabilize your current PATH as you may have other software expecting to find whatever version of Python you have in your "Path" and not 2.7.8. So with the caveat in mind, we'll run their installer, but decline to have the installer modify the "Path" environmental variable.

Run the Python installer. Create new top level directory, e.g. C:\Python278 and make sure during the installation that you do *NOT* add python.exe to your path. This is because your system may already be set up for Python 3 or other versions of Python 2 by having their paths be first in your PATH variable. You do not want to destabilize your current system by altering values in your default "Path" value. Instead, we'll set up a special DOS environment which calls Python 2.7.8 when running Scribus.

Here is a batch script that you should copy and place in your Scribus home directory where the main executable Scribus.exe resides, e.g. C:\Program Files\Scribus 1.5.0. Name this batch script: scribus.bat

	@echo off
	::
	:: special run of Scribus that uses Python installed on the
	:: users system that matches the build of the Python interpreter
	:: shipped with Scribus rather than the Python Interpreter shipped
	:: with Scribus' build.
	::
	:: See: http://wiki.scribus.net/wiki/index.php?title=Windows_Full_Python_Integration&action=edit&redlink=1
	::
	echo.
	echo Running Scribus ***without*** its shipped Python Interpreter
	echo.
	::
	:: Hide the shipped version of the Python interpreter so Scribus will use
	:: the "same" version we have built on Windows 
	::
	if exist python (
		echo masking Scribus Python directory
		ren python python.SUSPEND
	)
	if exist python27.dll (
		echo masking Scribus library python27.dll
		ren python27.dll python27.dll.SUSPEND
	)
	echo.
	::
	:: point to our custom Python which should match the build of
	:: Python shipped with this version of Scribus
	:: See:
	::
	PATH=C:\Python278;.
	::
	::   run it!
	::
	echo. 
	echo Launching Scribus...
	scribus.exe
	echo.
	::
	:: undo our temporary masks so we don't run Scribus in the future
	:: forgetting that we have this special hack to replace the Python
	:: interpreter shipped with it
	::
	if exist python.SUSPEND (
		echo restoring Scribus Python directory
		ren python.SUSPEND python
	)
	if exist python27.dll.SUSPEND (
		echo restoring Scribus library python.dll
		ren python27.dll.SUSPEND python27.dll
	)
	echo.
	::
	:: remind our user that we're cleaning up
	::
	pause


A simple method for using the system Python in Windows

First of all, as the above section indicates, you must have the appropriate version of Python on your system (currently 2.7). Once you have installed Python, go to the directory C:\Program Files, and find your Scribus directory, for example Scribus 1.5.3.

Inside this Scribus 1.5.3 directory, you will see a directory named python. Right-click on this to bring up the context menu, and choose Rename, and rename the directory to anything else, like pythonold. Now when you start up Scribus, when it fails to find this directory named python, it will then look to the system for Python. That's all there is to it.

A major advantage of using the system Python is that you can easily add other packages that may not be included with Scribus, such as re or datetime.