Python Forum
Script crashes when called from a second script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script crashes when called from a second script
#1


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.

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()
ui.Main(mdbPath,directory,a)
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         ##
            Ui_FormObject.fnGenerateReport(directory,name)
def fnGenerateReport(self,directory,name):
        now = datetime.now()
        dateString = now.strftime("%d-%b-%Y") 							# For getting Current Date
        dirPath = "C:\\"
        
        ResultName = "SelfTest_Results"
        testName = name+".pdf"
        subDirPath = dirPath 
        if(os.path.isdir(dirPath)):
                if(os.path.isdir(subDirPath)):
                        subDirPath = subDirPath + testName
                else:
                        os.mkdir(subDirPath)
                        subDirPath = subDirPath + testName
        else:
                os.mkdir(dirPath)
                os.mkdir(subDirPath)
                subDirPath = subDirPath + testName
        pixmap = QtGui.QPixmap(doc.size().width(), doc.size().height()) ################
        printer = QPrinter()
        
        printer.setOutputFileName(subDirPath)
        printer.setOutputFormat(QPrinter.PdfFormat)
        
        doc.print_(printer)
        # Create a QPainter to draw our content    
        painter = QPainter(pixmap)
        painter.begin( printer )
        doc.drawContents(painter)
        painter.end()
This is all the relevant code and it is runnable.

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!!
Reply
#2
You actually named the file "Script A", not, perhaps, "ScriptA" or "Script_A"?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
The actual script isn't called Script A, it's called Ethernet.py. Also I don't think this is an import issue, as I can verify that the functions are called, and that it is the QPixmap line that crashes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 409 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 534 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 491 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 319 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 546 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 656 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 515 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Combine console script + GUI (tkinter) dejot 2 432 Feb-27-2024, 04:38 PM
Last Post: deanhystad
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 353 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  OBS Script Troubleshooting Jotatochips 0 301 Feb-10-2024, 06:18 PM
Last Post: Jotatochips

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020