1

Python script (testbov.py) below extracts a sentence from the novel Madame Bovary.

'''

#!/usr/bin/env python       
from random import randint
import io
import codecs
from htmlcreator import HTMLDocument
import webbrowser

with codecs.open('./bovary_sc_no_susp.txt', 'r', encoding='utf8', errors='ignore') as source: 
    data=source.read() 
    splot_data=data.split(".") 
    has = randint(0,len(splot_data)) 
    prise = splot_data[has] 

document = HTMLDocument()
document.set_title('a slice of Bovary')
document.add_header('The slice')
document.add_text(prise)
document.write('my_document.html')  
webbrowser.open('./my_document.html')

'''

The script when run from the directory where it is (/var/www/html/test/cgi-bin) by the following command 'python testbov.py' does what he has to do and shows the extracted sentence in a browser page. (Python is python 3.7 installed by miniconda)

When script is launched from Geany using the run button it opens a terminal with the following error message :

Traceback (most recent call last):
  File "testbov .py", line 7, in <module>
    from htmlcreator import HTMLDocument
ImportError: No module named htmlcreator

------------------
(program exited with code: 1)
Press return to continue

When script is called through a browser pointing to localhost/test, index.html in test directory is below :

<html><head><title>minimal page</title></head>
<form action="http://localhost/test/cgi-bin/testbov.py" />
<input type="submit" value="Go !" />
</form></html>

I get error 500 with following message in /var/log/apache2/error.log :

Traceback (most recent call last):
  File "/var/www/html/test/cgi-bin/testbov.py", line 7, in <module>
    from htmlcreator import HTMLDocument
ImportError: No module named htmlcreator

So it seems that neither browser nor geany are able to find htmlcreator (installed through pip) while terminal is able to do so.

3 Answers3

0

the correct import is

from html_creator import Document
jv95
  • 681
  • 4
  • 18
0

I believe it runs in its own virtual environment. You need to install the module in that virtual env. You can check it in your script. Determine if Python is running inside virtualenv

import sys

def is_venv():
    return (hasattr(sys, 'real_prefix') or
            (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))

if is_venv():
    print('inside virtualenv or venv')
else:
    print('outside virtualenv or venv')
0

Please add this small debug code:

import os
print(os.environ['_'])

at the beginning of your script to verify which python is used for executing the script in both scenarios.

And then make sure they're either exactly the same or htmlcreator is available in both environments.