0

I am not an expert in python. I am trying to create a sample web application and I could run it with flask. However, after packaging it and trying to install it using waitress I am getting the error -

No module named helloweb.py (that is my web application). I am not sure how to troubleshoot this issue. Any pointers would be great. All the other answers on stack overflow did not help me much.

Here is my web application -

helloweb.py -

import datetime
from flask import Flask
from flask_api import status
import json


app = Flask(__name__)



@app.route('/')
def hello_world():
    return 'Hello!'

@app.route('/xyz')
def display_status():
    healthstatus=json.dumps({'status': status.HTTP_200_OK})
    return healthstatus

I tried the following and it worked -

FLASK_APP=helloweb.py
FLASK_RUN_PORT=8080

flask run

But trying to port it on the WSGI production server waitress is not working for me. I did the following, created a setup.py file -

import setuptools
from setuptools import setup

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="mydomain-webapp-myusername", # Replace with your own username
    version="0.0.1",
    author="first.last",
    author_email="someone@gmail.com",
    description="A small flask package",
    long_description="This is a web app",
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

and I created a LICENSE, README.md file, init.py file,waitress_server.py file in the same folder as the source file (helloweb.py). I am not sure why I am seeing the following error when I run -

$ waitress-serve --call 'helloweb:app'

Error: Bad module 'helloweb'

Usage:

    waitress-serve [OPTS] MODULE:OBJECT
.........
........
..........
There was an exception (ImportError) importing your module.

It had these arguments: 
1. No module named flask_api

I am able to import flask_api in the python shell and also helloweb.py, so I am not sure how to fix this. How do I troubleshoot this error?

user4002112
  • 141
  • 1
  • 2
  • 18
  • What are you expecting `from flask_api` to do? – OneCricketeer Aug 27 '20 at 19:11
  • I am using that module to display HTTP status code. – user4002112 Aug 27 '20 at 19:12
  • Sure. According to the error, though, that module is not available – OneCricketeer Aug 27 '20 at 19:13
  • Yes, that's why I mentioned I am able to import the module in python shell in the same environment. See the output below - myprompt$ python Python 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import flask_api >>> – user4002112 Aug 27 '20 at 19:14
  • And `waitress-serve` uses the same Python environment as your shell? – OneCricketeer Aug 27 '20 at 19:15
  • What does `waitress_server.py` file look like? – Seyi Daniel Aug 27 '20 at 19:15
  • @OneCricketeer, good question, how do I make sure of that? I just tried setting current python interpreter to version 3.8 which is the latest python version I am using. – user4002112 Aug 27 '20 at 19:17
  • Hi @SeyiDaniel, This is the content of waitress-server.py - from waitress import serve import helloweb serve(helloweb.app, host='0.0.0.0', port=8080) – user4002112 Aug 27 '20 at 19:18
  • Not sure what you mean "setting". Are you using PyCharm/VsCode? If so, are you using a virtualenv? Where did you install waitress? And where is `flask_api` installed? – OneCricketeer Aug 27 '20 at 19:24
  • @OneCricketeer, here is the output I have - There was an exception (ImportError) importing your module. It had these arguments: 1. No module named Flask_API (hellowebapp) simmi-MBP:wm simmi$ pip list Package Version ------------ ------- click 7.1.2 Flask 1.1.2 Flask-API 2.0 itsdangerous 1.1.0 Jinja2 2.11.2 MarkupSafe 1.1.1 pip 20.2.2 setuptools 49.6.0 Werkzeug 1.0.1 wheel 0.35.1 – user4002112 Aug 27 '20 at 21:00
  • If `pip list` doesn't show `waitress`, I would re-install it with `pip install waitress` – OneCricketeer Aug 27 '20 at 21:14
  • 1
    @OneCricketeer, Thank you for the help, I solved both problems - Instead of using waitress, I am using gunicorn and for the flask API, I needed to change the PATH variable to point to python site-packages. – user4002112 Aug 27 '20 at 23:53
  • Hmm. Touching PATH shouldn't be necessary. – OneCricketeer Aug 27 '20 at 23:59
  • @OneCricketeer, do you have alternate suggestions? – user4002112 Aug 28 '20 at 05:40
  • Well, PATH would contain `/usr/local/bin/`, for example, and in there would be the python executable. Since the site-packages should only contain python executables, not OS executables, then I was simply saying that it shouldn't need to be on the path. – OneCricketeer Aug 28 '20 at 17:57

0 Answers0