Reliably Detect Spyder IDE

112 views Asked by At

How do I reliably detect if a script/module is being run in the Spyder IDE?

I've hit an issue running ocrmypdf in the spyder IDE. It works on cmd and anaconda prompt. It errors out when run in the spyder IDE, on windows 7 & 10, various machines, various new/old anaconda setups. (See the stub and inline comments below for details on errors.) The developer of ocrmypdf suggested that it's due to multiprocessing not working in the spyder IDE (Python's multiprocessing doesn't work in Spyder IDE). I want to know if there's a reliable method of detecting whether ocrmypdf or any script/module is being run in the Spyder IDE.

Basically, this is a repeat of: Detect where Python code is running (e.g., in Spyder interpreter vs. IDLE vs. cmd)

I'm asking this question again because the question was originally asked in 2013 and the answer accepted - checking for environment variables which spyder sets in os.environment - is workable but has the risk of false positives.

If there's some cleverer way of resolving this please let me know!


import os, io
import ocrmypdf
from wand.image import Image as Img

try:
    from PIL import Image
except ImportError:
    import Image
    
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"


ocrmypdf_exitcodes = {0:'ok', 1:'bad_args', 2:'input file', 3:'missing_dependency', 
                      4:'invalid_output_pdf', 5:'file_access_error', 6:'already_done_ocr', 
                      7:'child_process_error', 8:'encrypted_pdf', 9:'invalid_config', 
                      10:'pdfa_conversion_failed', 15:'other_error', 130:'ctrl_c'}

path = "C:\Users\public\Documents"
tess_lang = "eng"

#Test files from https://github.com/jbarlow83/OCRmyPDF/tree/master/tests/resources

file = "skew.pdf" #works
file = "cardinal.pdf" #breaks at scanning contents section/it's been 20 minutes with no progress past first page
file = "c02-22.pdf" #Breaks at OCR section on first page - logs say 0.5 and then it stalls for 10+ minutes. Sometimes breaks by saying [Errno9] Bad File Descriptor instead.


pdf = os.path.join(path, file)
try:
    filename = pdf.rsplit('.', 1)[0]+'_new.pdf'
    ocrmypdf.ocr(input_file = pdf, output_file = filename, language = '+'.join(list(set([tess_lang, 'eng']))), rotate_pages=True, deskew=True, force_ocr = False)
except Exception as e:
    filename = pdf
    print('Error occurred when trying to process file {} error message is: {}'.format(pdf, repr(e) + " " + str(e)))
    print(repr(e))
    try:
        print(ocrmypdf_exitcodes[e.returncode])
    except:
        pass
0

There are 0 answers