# Including import causes script to fail

**URL:** <https://discuss.python.org/t/including-import-causes-script-to-fail/16721>\
**Category:** Python Help\
**Created:** [June 21, 2022, 9:36pm UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721 "2022-06-21T21:36:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![petec](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@petec](https://discuss.python.org/u/petec)\
**Post date:** [June 21, 2022, 9:36pm UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/1 "2022-06-21T21:36:10Z")

</div>

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?

```auto
#!/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

```

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 21, 2022, 9:50pm UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/2 "2022-06-21T21:50:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [June 21, 2022, 10:04pm UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/3 "2022-06-21T22:04:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![petec](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@petec](https://discuss.python.org/u/petec)\
**Post date:** [June 22, 2022, 12:55am UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/4 "2022-06-22T00:55:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petec](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@petec](https://discuss.python.org/u/petec)\
**Post date:** [June 22, 2022, 12:59am UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/5 "2022-06-22T00:59:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 22, 2022, 1:09am UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/6 "2022-06-22T01:09:23Z")

</div>

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…

> [@petec](#):
>
> (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,

… 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

```auto
Content-type: text/html

StartingMain

```

---

<div class="post-metadata">

**Author:** ![petec](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@petec](https://discuss.python.org/u/petec)\
**Post date:** [June 22, 2022, 1:34am UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/7 "2022-06-22T01:34:40Z")

</div>

> [@](#):
>
> edit: just to be clear, this is what I get on the output
> 
> ```auto
> 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.

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 22, 2022, 1:37am UTC](https://discuss.python.org/t/including-import-causes-script-to-fail/16721/8 "2022-06-22T01:37:49Z")

</div>

> [@petec](#):
>
> 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.

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