Forums

Programy Chatbot

I'll start by saying I'm a python novice. I'm trying to run the programy chatbot webchat client on pythonanywhere.

On windows the bot is started by a shell script in the venv. I tried this and got the following error:

 Initiating WebChat Client...                                                                                                                                                     
No bot root argument set, defaulting to [../../config/xnix]                                                                                                                      
WebChat Client running on http://quiraang.eu.pythonanywhere.com:8090                                                                                                             
Healthcheck now running as part of REST Service...                                                                                                                               
WebChat Client running in http mode, careful now !                                                                                                                               
 * Serving Flask app "client" (lazy loading)                                                                                                                                     
 * Environment: production                                                                                                                                                       
   WARNING: This is a development server. Do not use it in a production deployment.                                                                                              
   Use a production WSGI server instead.                                                                                                                                         
 * Debug mode: off                                                                                                                                                               
Traceback (most recent call last):                                                                                                                                               
  File "/usr/lib/python3.8/runpy.py", line 192, in _run_module_as_main                                                                                                           
    return _run_code(code, main_globals, None,                                                                                                                                   
  File "/usr/lib/python3.8/runpy.py", line 85, in _run_code                                                                                                                      
exec(code, run_globals)                                                                                                                                                      
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/programy/clients/restful/flask/webchat/client.py", line 161, in <module>                                  
    WEB_CLIENT.run(APP)                                                                                                                                                          
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/programy/clients/restful/flask/client.py", line 77, in run                                                
app.run(host=self.configuration.client_configuration.host,                                                                                                                   
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/flask/app.py", line 990, in run                                                                           
run_simple(host, port, self, **options)                                                                                                                                      
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/werkzeug/serving.py", line 1052, in run_simple                                                            
inner()                                                                                                                                                                      
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/werkzeug/serving.py", line 996, in inner                                                                  
srv = make_server(                                                                                                                                                           
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/werkzeug/serving.py", line 847, in make_server                                                            
return ThreadedWSGIServer(                                                                                                                                                   
  File "/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/werkzeug/serving.py", line 740, in __init__                                                               
HTTPServer.__init__(self, server_address, handler)                                                                                                                           
  File "/usr/lib/python3.8/socketserver.py", line 452, in __init__                                                                                                               
self.server_bind()                                                                                                                                                           
  File "/usr/lib/python3.8/http/server.py", line 137, in server_bind                                                                                                             
socketserver.TCPServer.server_bind(self)                                                                                                                                     
  File "/usr/lib/python3.8/socketserver.py", line 466, in server_bind                                                                                                            
self.socket.bind(self.server_address)                                                                                                                                        
OSError: [Errno 99] Cannot assign requested address                                                                                                                              
(mickey) 12:34 ~/.virtualenvs/mickey/scripts/xnix $

So I have set up the WSGI file as follows:

  import sys
  # add your project directory to the sys.path
 project_home = '/home/quiraang/.virtualenvs/mickey/lib/python3.8/site- 
 packages/programy/clients/restful/flask/webchat/'
 if project_home not in sys.path:
 sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
# from y-bot-webchat import app as application  # noqa
from client.py import app as application  # noqa

This does not work i get the following in the error log:

Error running WSGI application
 ModuleNotFoundError: No module named 'client.py'; 'client' is not a package
 File "/var/www/quiraang_eu_pythonanywhere_com_wsgi.py", line 17, in <module>    
 from client.py import app as application  # noqa

I've probably made some basic rookie error or misunderstanding somewhere - If anyone can help I'd be very grateful

I've now changed the wsgi file to:

import sys

# add your project directory to the sys.path

project_home = '/home/quiraang/.virtualenvs/mickey/'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
# from y-bot-webchat import app as application  # noqa
from programy.clients.restful.flask.webchat.client import app as application  # noqa

and get a different error

2021-01-30 14:33:21,937: Error running WSGI application
2021-01-30 14:33:21,940: ImportError: cannot import name 'app' from 'programy.clients.restful.flask.webchat.client' (/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/programy/clients/restful/flask/webchat/client.py)
2021-01-30 14:33:21,940:   File "/var/www/quiraang_eu_pythonanywhere_com_wsgi.py", line 18, in <module>
2021-01-30 14:33:21,941:     from programy.clients.restful.flask.webchat.client import app as application  # noqa
2021-01-30 14:33:21,941: ***************************************************

Not sure if this is progress or not

Does the file /home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/programy/clients/restful/flask/webchat/client.py define a variable called app?

Normally when you write a Flask site, you will have a variable called app defined somewhere that links together all of the details of your website. That is the one that you need to import into the WSGI file.

It has:

if __name__ == '__main__':

outputLog(None, "Initiating WebChat Client...")

APP = Flask(__name__)

WEB_CLIENT = WebChatBotClient()

@APP.route('/')
def index():
    return current_app.send_static_file('webchat.html')

it has APP rather than app - I tried importing that but it returns the same error

2021-01-30 15:57:47,676: Error running WSGI application
2021-01-30 15:57:47,682: ImportError: cannot import name 'APP' from 'programy.clients.restful.flask.webchat.client' (/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/programy/clients/restful/flask/webchat/client.py)
2021-01-30 15:57:47,683:   File "/var/www/quiraang_eu_pythonanywhere_com_wsgi.py", line 18, in <module>
2021-01-30 15:57:47,683:     from programy.clients.restful.flask.webchat.client import APP as application  # noqa
2021-01-30 15:57:47,683: ***************************************************

Looking at your WSGI file again, the project_home looks wrong. If you have this:

project_home = '/home/quiraang/.virtualenvs/mickey/'

...and then this:

from programy.clients.restful.flask.webchat.client import APP as application

...then Python will look for a file called /home/quiraang/.virtualenvs/mickey/programy/clients/restful/flask/webchat/client.py, but the file you want it to look at is /home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/programy/clients/restful/flask/webchat/client.py

You could change the path so that it has the location of the file you're trying to load from, which is almost, but not quite, what you had originally:

project_home = '/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/'

However, that path would normally be on your system path already, as you have specified that your website is using that virtualenv.

So what happens if you remove the three lines that change your system path completely?

I commented out the three lines :::python import sys

# add your project directory to the sys.path

#project_home = '/home/quiraang/.virtualenvs/mickey/lib/python3.8/site-packages/'
#if project_home not in sys.path:
 #    sys.path = [project_home] + sys.path

 # import flask app but need to call it "application" for WSGI to work

from programy.clients.restful.flask.webchat.client import APP as application  # noqa

and I get the same error

2021-02-02 11:33:22,799: Error running WSGI application
2021-02-02 11:33:22,801: ImportError: cannot import name 'APP' from 
'programy.clients.restful.flask.webchat.client' (/home/quiraang/.virtualenvs/mickey/lib/python3.8/site- 
 packages/programy/clients/restful/flask/webchat/client.py)
 2021-02-02 11:33:22,802:   File "/var/www/quiraang_eu_pythonanywhere_com_wsgi.py", line 18, in <module>
 2021-02-02 11:33:22,802:     from programy.clients.restful.flask.webchat.client import APP as application  # 
 noqa

This is open source code , and although I'm an experienced coder, I'm new to python - does it matter that the variable is uppercase?

Python is case-sensitive, so the case of your import has to match the case of the definition that you're importing. Check the documentation for the package you're using to see what case it's supposed to be.

It's not clear from the code that you posted earlier (of the contents of client.py) what the indentation is. I suspect from looking at it that all the code is in the if statement, so none of it is run, because that if is only true when client.py is run directly by Python and not when it's imported. That would explain why APP is not available to import - the code to define it is never run.

Yes all the code is in the if statement. As you say, that explains why it is not run. I shall raise a query with the author of the package. Thank you for your assistance.