Including import causes script to fail

I have a script that I want to use with mqtt however if I include the line:
import paho.mqtt.client as mqtt
the script fails. If I comment out the import the script runs…but of course is useless without mqtt. The script is below but here are some notes:
(1) This is a cgi script.
(2) You can see that I have commented out nearly everything to eliminate other possible problems.
(3) I can run this script using Geany and no errors are reported and it ends nicely.
(4) If I leave the import in and submit to run the script I get a “Internal Server Error…blah blah blah”. No useful information except the error log says, “End of script output before headers: Opener.cgi”, which is all over the internet without any solution.
(5) I have another cgi script that doesn’t use mqtt. If I insert the uncommented import, it fails also.
(6) I have many python scripts that use mqtt but are not used for cgi and they work fine.

I have exhausted my idea bucket. Any ideas?

#!/usr/bin/env python 


 
import cgi, os.path, cgitb, time
#import paho.mqtt.client as mqtt



form=cgi.FieldStorage()
  

# def on_connect(client, userdata, rc):
    # print("Connected with result code "+str(rc))

def main(): 
    a=1
   # print('Content-type: text/html\n\n')
    # time.sleep(10)
    print ('StartingMain')
    # broker_address="192.168.1.115"
    # client = mqtt.Client("P3")
    # client.on_connect=on_connect('localhost',1883,60)
    # print("connecting to broker")
    # client.connect(broker_address,1883,60) #connect to broker
    # change=form.getvalue("ChangeOverheadDoor")
 # This pubs to a terminal window just fine   client.publish("GarageCommand", "ON")
 #   print(change,"<br>")


# def fileToStr(fileName): 
        # """Return a string containing the contents of the named file."""
        # fin = open(fileName); 
        # contents = fin.read();
        # yesOrNo=fin.readline()
        # forText=fin.readline()
        # fin.close() 
        # return contents

# def strToFile(text, filename):
    # """Write a file with the given name and the given text."""
    # output = open(filename,"w")
    # output.write(text)
    # output.close()

# def browseLocal(webpageText, filename='/var/www/calinski.us/public_html/opener.html'):
    # '''Start your webbrowser on a local file containing the text
    # with given filename.'''
    # import webbrowser, os.path
    # strToFile(webpageText, filename)
    # webbrowser.open("file:///" + os.path.abspath(filename)) #elaborated for Mac


try:   # NEW
    print("Content-type: text/html\n\n")   # say generating html
    main() 
except:
    cgi.print_exception()                 # catch and print errors

Have you tried using #!/usr/bin/env pyhton3 ?

First, note that the cgi module is going away in the near future
(Python 3.13), so if you have the luxury of rewriting that script
you may want to avoid using the cgi module in the long-term.

With that out of the way, you can get Python tracebacks returned in
the request output by importing the cgitb module and then calling
cgitb.enable() as described in the documentation:

https://docs.python.org/3/library/cgitb.html

Hope that helps your troubleshooting!

Rob,
Yep I have done that and variations. No luck.
I still can’t understand why the mear importing causes failures.

fungi,

I tried that a few different ways. I guess I’m not smart enough to get that to work.

I just found out that cgi is going away…after spending weeks creating scripts for cgi. I need to complete this project ASAP.

Maybe for the next project.

I still can’t understand why the mear importing causes failures.

Humm, very odd.

Your code runs just fine on my system: Debian based Linux running Python 3.6.9

Mind, that shebang (that I posted) does not work on my system (maybe it’s for Apple OS or MS Windows) I use #!/usr/bin/python3. That said, your code still runs on my system with #!/usr/bin/python2.

You say…

… but maybe you should let the guys here (most of whom are VERY knowledgeable, much more so than I) be the judge of that.

{content removed: I get so used to posting this, that it’s become 2nd nature}

Sorry I can’t be of more help.

edit: just to be clear, this is what I get on the output

Content-type: text/html


StartingMain

Yep, I get the same thing like I said in #3 of my first post. But the html file has a “submit” button that runs this script. That is what gives me the internal Server Error.

I have wasted too much time on this problem. I wanted the script to send a mqtt message to a device. I think I will have the script just put a note in a file and let another program that is already running find the note and send the message.
Dirty but leaving a message “under a rock” seems to move me forward.

Thanks for your time.

Ah, okay; my bad. I thought that the code that you posted did not produce what I was getting; nm.