0

I'm running the following code both remotely on a linux machine via ssh, and on the same linux machine as a Jupyter notebook accessed through a browser.

import cv2
import pdf2image

def minimalFun(pdf_filepath, make_me_suffer = False):
    print("Now I start.")
    images = pdf2image.convert_from_path(pdf_filepath)
    print("Pdf read.")
    
    if make_me_suffer:
        cv2.namedWindow('test',0)
        
    print("I finished!")
    
minimalFun('Test.pdf', make_me_suffer = True)

I'm confused on the behaviour of the difference of the behaviour of the Pyhton interpreter in Jupyter and when used on the command line.

In a Jupyter notebook

With the make_me_suffer = False setting the code will just print

Now I start.
Pdf read.
I finished!

meaning in particular that the function pdf2image.convert_from_path ran successfully. However, with the make_me_suffer set to True, the code will print just

Now I start.

and then report that the kernel has died and will be restarting. In particular, the kernel died already with the function pdf2image.convert_from_path.

On the command line

As expected, with the make_me_suffer = False setting the code will just print

Now I start.
Pdf read.
I finished!

but now when the flag is set to make_me_suffer = True, we get

Now I start.
Pdf read.
: cannot connect to X server

meaning that here the function pdf2image.convert_from_path again finished successfully.

The question:

Does the Jupyter interpreter 'look ahead' to see if there will be a command later on requiring an x-windowing system and altering the interpretation of current stuff based on the information. If so, why? Is this common? Does it happen with functions loaded from other files? What is going on?

The reason why I'm asking is, that this took me a lot of time to troubleshoot and pinpoint in a more complex function. This conserns me as I have no idea how to avoid this in the future, other than having from now on a fobia on anything graphical.

Rami Luisto
  • 229
  • 1
  • 9

1 Answers1

1

Does the Jupyter interpreter 'look ahead' to see if there will be a command later on requiring an x-windowing system and altering the interpretation of current stuff based on the information.

No, it does not.

As you know, you can run cells in any arbitrary order or modify them after you've run them once. This makes notebooks very brittle unless used properly.

You could, however, move your common code (e.g. stuff that initializes a window that you know you'll need) into a regular .py module in the notebook directory and import and use stuff from there.

AKX
  • 152,115
  • 15
  • 115
  • 172