Internal Error when using PyInstaller to make an standalone app using Flask + FlaskWebGui - flask

Ok so I've been trying to make a standalone python app using Flask and FlaskWebGUI, it works just fine when I'm running it on VScode, print of an example program below:
Working just fine, ok. But when I use the PyInstaller command pyinstaller server.py to create the desktop app for some reason this following error occurs:
that's my server.py code:
from flask import Flask, render_template, request, redirect, url_for
#importa o Web GUI
from flaskwebgui import FlaskUI
app = Flask(__name__)
app.static_folder = 'static'
ui = FlaskUI(app, width=1150, height=700)
# A decorator used to tell the application
# which URL is associated function
#app.route('/', methods =["GET", "POST"])
def gfg():
if request.method == "POST":
# getting input with name = fname in HTML form
first_name = request.form.get("fname")
# getting input with name = lname in HTML form
last_name = request.form.get("lname")
return "Your name is "+first_name + last_name
return render_template("index.html")
# runs app
if __name__ == "__main__":
# app.run(debug=True)
# Default start flask
FlaskUI(
app=app,
server="flask",
width=1150,
height=700,
).run()
I have no idea why it isn't working.
In case it's useful, here's the PyInstaller log after running the command: https://pastebin.com/6dC6fTBs
==========
I also have tried using the following PyInstaller commands:
pyinstaller --name myapp --onefile server.py -> still not working
pyinstaller --name myapp --onefile --add-data "templates;." server.py -> also not working

It was a dependent files issue, PyInstaller you need to specify the dependency files to PyInstaller and I just wasn't managing to do it.
I then instead of typing the PyInstaller command by myself, i used it's GUI (by running "auto-py-to-exe" on a terminal in the script folder) then i used the GUI to select the dependency files (I did not had to select libraries manually, just files like .html, .png) and it worked just fine.
There's also a lot of other configs you can set using the GUI, but that's all i needed to make the program run properly.

Related

ModuleNotFound: No module named 'SpeechRecognition'

I'm using Python 3.7.8
ModuleNotFound: No module named 'SpeechRecognition' appears when I try to run my project.
ModuleNotFound: No module named 'SpeechRecognition'
But it works well when I try it with python -m speech_recognition:
enter image description here
How to start flask app. I recommend to start like this. main.py is flask start script file.
# set flask start script file
export FLASK_APP=main.py
# run flask
python3 -m flask run
# main.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'

I am new to flask tryin to run first application .. here is my code and what turns back in terminal .. any suggestions?

code
from flask import Flask, render_template, session, request
from flask_session import Session
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"]= "filesystem"
Session(app)
#app.route("/", methods=["GET", "POST"])
def index():
if session.get("comments") is None:
session["comments"] = []
if request.method == "POST":
comment = request.form.get("comment")
session["comments"].append(comment)
CMD
(env) E:\COURSES\CS50W\lec2\exersice>set FLASK_APP=application.py
(env) E:\COURSES\CS50W\lec2\exersice>python -m flask run
E:\COURSES\CS50W\lec2\exersice\env\Scripts\python.exe: No module named flask
Check this out.
If you are not sure whether you have installed flask or not, try running only flask in the cmd or any terminal.If you get any commands about flask then you can be sure that it is installed or you can install by using
pip install flask
If you are concerned about the versions of the libraries you use for your application, then follow the above mentioned link!

Cannot import module to begin basic Flask app

So I'm following a beginners tutorial on Flask and for whatever reason am getting an error on what is essentially the very first step.
I first created an "app" directory where I created a python file for "init.py" which contains the following code:
from flask import Flask
app = Flask(__name__)
from app import routes
I then created a "routes.py" python file in the same directory:
from app import app
#app.route('/')
#app.route('/index')
def index():
return "Hello, World!"
Finally (and this is where the problem stems from), I created a python file named "microblog.py" which is located in the same folder as the "app" directory:
from app import app
I then go to my virtual environment and run (using cmd windows):
set FLASK_APP=microblog.py
So far so good, however when I try to run the following code in cmd:
flask run
I get the following error:
ImportError: cannot import name 'app' from 'app' (C:\Users\Grae_\microblog\app\__init__.py)
If any further clarification is needed, here are my file locations:
C:\Users\Grae_\microblog
C:\Users\Grae_\microblog\app
C:\Users\Grae_\microblog\__init__.py
C:\Users\Grae_\microblog\routes.py
C:\Users\Grae_\microblog\venv
C:\Users\Grae_\microblog\microblog.py
Apologies if this is really obvious, I'm just obviously very new to Flask and have been stuck on this for a while.
Thanks
The issue here is on python package "app". The directory should have a file named __init__.py instead of init.py.
For example, you rename the file init.py to __init__.py and replace content with below code it should work
from flask import Flask
app = Flask(__name__)
def start():
from app import routes
start()
You can do something like this:-
test.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Index!"
#app.route("/hello")
def hello():
return "Hello World!"
#app.route("/members")
def members():
return "Members"
#app.route("/members/<string:name>/")
def getMember(name):
return name</string:name>
if __name__ == "__main__":
app.run()
In command prompt, run the command-
python test.py
Try the URLs in your browser:
http://127.0.0.1:5000/
http://127.0.0.1:5000/hello
http://127.0.0.1:5000/members
http://127.0.0.1:5000/members/Karan/

flask: code in app factory does not get executed

I am using the standard flask app factoty setup as stated here:
http://flask.pocoo.org/docs/1.0/tutorial/factory/
flaskr/init.py
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
print('Hello World')
...
return app
I run this app with:
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
All very standard. But why is the code print("hello world") never executed?
edit
After reboot of my dev sytem the issue is gone. I am sorry I posted this.
Looks like you named the file init.py instead of __init__.py.
I guess you are using an older version of flask (<1.0).
The newest versions (>1.0) allow to detect automatically the function create_app or make_app (source code) to launch the application from CLI flask command.
You can update the flask package, for example with pip :
pip install --upgrade Flask
or add the following lines at the end of the __init__ file to create the application in an explicit way:
if __name__ == "__main__":
app = create_app()
app.run()

Django Selenium LiveServerTestCase not loading the page in browser (error 500)

I have a test Django project called MyApp, running over WSGI on port 8083. When I go to http://myapp:8083, I see the standard Django "it's working" page. I wrote a functional test using selenium bindings in Django to launch a browser and load the above mentioned page. When I run the test, though, I get an error message "Address already in use". So I run the test using another port like this: python manage.py test --liveserver=myapp:8084
This opens the browser, but shows "Page not found" error instead of the default Django page. What am I doing wrong? Any ideas? Thank you!
The test.py file content:
class CoreSeleniumTestCase(LiveServerTestCase):
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.maximize_window()
super(CoreSeleniumTestCase, cls).setUpClass()
#classmethod
def tearDownClass(cls):
cls.driver.quit()
super(CoreSeleniumTestCase, cls).tearDownClass()
def testIndexShouldLoad(self):
self.driver.get('%s%s' % (self.live_server_url, '/'))
I finally found the problem. At some point, Django removed MEDIA_ROOT from the settings.py file by default. It turns out that this setting must be in the file for Selenium tests to work properly. Once I reintroduced the setting and assigned a directory to it, the Selenium tests started to work as expected.