Click here to Skip to main content
15,946,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a report generation script (let's call this script A)that generates a PDF report. I want to import this in a new script (script B) and call the functions from within this new script.

The problem:

1. Script A crashes when called from Script B exactly at the creation of a QPixmap object. Python console restarts.
2. Script A runs fine when it is run on its own, but if I try to call it from Command Prompt python.exe crashes.

I will explain and display the code in the next section as the best I have managed to do is identify the problems.

What I have tried:

The following is Script B. All it does is it imports Script A, creates an instance of a class defined in A, and uses that to call a method of that object.

import Script A as et

a = "sample"
mdbPath = "C:\\Python27\\test.mdb"
directory = "C:\\Python27"

ui = et.HawkAutomationScript()

Script A contains the definition of a class called HawkAutomationScript. This object contains two functions to note: one Main function and one function to generate the report. These are the imported modules:

from random import*
import sys
import os
import subprocess
import time
from datetime import datetime
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QTextDocument, QPrinter, QApplication, QPainter
from string import Template
import shutil
import time
import pyodbc

This is the definition of the class and the Main function:

doc = QTextDocument()
myCursor = QtGui.QTextCursor(doc)

class HawkAutomationScript(object):
    def Main(self,mdb,directory,name):
            Ui_FormObject = HawkAutomationScript()
##          Filling in the Report         ##

def fnGenerateReport(self,directory,name):
        now =
        dateString = now.strftime("%d-%b-%Y") 							# For getting Current Date
        dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\"
        ResultName = "SelfTest_Results"
        testName = name+".pdf"
        subDirPath = dirPath + name
                        subDirPath = subDirPath + testName
                        subDirPath = subDirPath + testName
                subDirPath = subDirPath + testName
        pixmap = QtGui.QPixmap(doc.size().width(), doc.size().height()) ################
        printer = QPrinter()
        # Create a QPainter to draw our content    
        painter = QPainter(pixmap)
        painter.begin( printer )

This is all the relevant code.

If I run script B, A crashes at the QPixmap line marked in the code. But if I copy the lines from B and run A on its own, the PDF report is generated. But if I run A on its own from Command Prompt python.exe crashes.

Any help would be really appreciated. Thank you!!
Updated 31-Oct-17 0:02am
Richard MacCutchan 31-Oct-17 5:55am    
Not sure exactly what the problem is, but one obvious issue is that dirPath and subDirPath are both pointing to the same directory when you are calling mkdir.
TheLostJedi 31-Oct-17 6:00am    
Thank you for the reply! I hadn't noticed that. subDirPath was supposed to be different. But in this case, the directory exists. So though this might have caused an issue later, right now it comes out of the if block with no issues but crashes after the QPixmap statement. The fact that this happens only when calling from another script is what confuses me. Thanks anyways!
Richard MacCutchan 31-Oct-17 6:18am    
I cannot see anything else wrong, but I have no experience of using Qt from Python. I can only suggest using the debugger to try and capture more information.

1 solution

The following code looks wrong to me:
dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\"

ResultName = "SelfTest_Results"
testName = name+".pdf"
subDirPath = dirPath    # this is now the same as dirPath
        if(os.path.isdir(subDirPath)): # if both paths (actually only one) exists
                subDirPath = subDirPath + testName  # append testName, but see below
        else: // this can never happen
                subDirPath = subDirPath + testName
        os.mkdir(dirPath)  # dirPath did not exist, so create it
        os.mkdir(subDirPath) # now create it again ???
        subDirPath = subDirPath + testName # now append testName, but it's too late.

What it should be:
dirPath = "C:\\Users\\michael\\Downloads\\Ethernet\\scripts\\"

ResultName = "SelfTest_Results"
testName = name+".pdf"
subDirPath = dirPath + name  # append name, testName is the name of the PDF file
    os.mkdir(dirPath)  # dirPath did not exist, so create it
if(!os.path.isdir(subDirPath)): # subDirPath did not exist, so create it


Share this answer
TheLostJedi 31-Oct-17 6:04am    
I have made the necessary change, although this does not affect the code if the dirPath exists.
Richard MacCutchan 31-Oct-17 6:16am    
Whether it exists or not, in the above code dirPath and subDirPath will always be the same when you call mkdir.
TheLostJedi 31-Oct-17 6:20am    
I have made the change to the code above. But in my test setup, the directory already exists. So mkdir is never called. subDirPath just becomes subDirPath + testName .I have changed this in the main code.
Richard MacCutchan 31-Oct-17 6:32am    
See my update above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900