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
  Running script from remote to server invisiblemind 4 678 Mar-28-2025, 07:57 AM
Last Post: buran
  Insert command line in script lif 4 912 Mar-24-2025, 10:30 PM
Last Post: lif
  modifying a script mackconsult 1 536 Mar-17-2025, 04:13 PM
Last Post: snippsat
  help with a script that adds docstrings and type hints to other scripts rickbunk 2 1,218 Feb-24-2025, 05:12 AM
Last Post: from1991
  Detect if another copy of a script is running from within the script gw1500se 4 1,088 Jan-31-2025, 11:30 PM
Last Post: Skaperen
  pass arguments from bat file to pyhon script from application absolut 2 983 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 1,080 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,131 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,197 Oct-19-2024, 06:29 PM
Last Post: Winfried
  Help for Tiktok Script Jasson187512 0 735 Oct-09-2024, 08:42 AM
Last Post: Jasson187512

Forum Jump:

User Panel Messages

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