NameError: name 'helloworld' is not defined - python

My files are as follows:
helloworld.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def __init__(self):
print 'Hello World!'
if __name__ == '__main__':
app.run()
application.wsgi
import os
import sys
sys.path.append('/srv/www/mysite.com/application')
os.environ['PYTHON_EGG_CACHE'] = '/srv/www/mysite.com/.python-egg'
import flaskr.helloworld
application = helloworld
When attempting to run this through my web browser, the module is loaded fine. I end up receiving a 500 error, with this in my error.log "NameError: name 'helloworld' is not defined"
Any ideas why?
Thank you in advance.

import flaskr.helloworld as helloworld
application = helloworld.app
Or alternatively:
import flaskr.helloworld
application = flaskr.helloworld.app

In application.wsgi, how about replacing the last line with
application = flaskr.helloworld
Or replace the import with
import flaskr.helloworld as helloworld

Related

In Apache2 Error.log: ModuleNotFoundError: No module named

Context:
Ubuntu 20
apache2
python 3.8
flask 2.0
venv
PyCharm
This works fine
init.py
from flask import Flask
app = Flask(__name__)
def func1():
return "test1"
#app.route("/")
def index():
f1 = func1()
return f1
if __name__ == "__main__":
app.run(debug=True)
works
This doesn't work
init.py
from flask import Flask
import mod
app = Flask(__name__)
#app.route("/")
def index():
f1 = mod.func1()
return f1
if __name__ == "__main__":
app.run(debug=True)
works not 1
mod.py
def func1():
return "test1"
works not 2
In apache2 error.log
apache2 error.log
In Browser
browser
Why?
The reason for the error is due to the file system in Linux.This code would work perfectly in windows, But in Linux you should write.
import .mod
or
from ubc3 import mod
or
import ubc3.mod

ImportError: cannot import name 'app' from 'FlaskApp' (unknown location)

I have been following this tutorial from digital ocean.
I got all the way to the last step when I get a 500 Internal Server Error.
My app is structured like the following:
|--------FlaskApp
|----------------FlaskApp
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi
My __init__.py contains the following:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello, I love Digital Ocean!"
if __name__ == "__main__":
app.run()
My flaskapp.wsgi contains the following:
import sys
import logging
import site
site.addsitedir('/var/www/FlaskApp/venv/lib/python3.7/site-packages')
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'key'
I had added the bit about importing site after reading this SO
When I look into /var/www/FlaskApp/venv/lib/python3.7/site-packages I can in fact see that packages I need and when I run python3, and import flask, it does load.
Thanks for your help in advance!

Flask ImportError: cannot import name routes

I'm converting a cli application to use a REST api and I've read up on flask and I thought I understood things but apparently not :-D. based on this: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I have a directory structure:
--myApp
myApp.py
--APIService
__init__.py
WebService.py
myApp.py:
from APIService import app
app.run(debug = True )
init:
from flask import Flask
app = Flask(__name__)
from app import routes
WebService.py:
from APIService import app
class WebService(object):
'''
classdocs
'''
def __init__(self,):
'''
Constructor
'''
#app.route('/')
#app.route('/index')
def index():
return "Hello, World!"
I've tried this a few different ways like renaming app to APIService but I keep circling back to the same error: APIService\__init__.py", line 5, in <module> from app import routes ImportError: No module named app
I just don't get what I'm doing wrong here. I did pip install flask so the module is there. I skipped the environment part but that's because I wasn't bothered with running globally for now. anyone have a clue as to what I messed up?
In the following line insideAPIService\__init__.py:
from app import routes
the keyword routes is referencing a separate Python Module inside the APIService folder that is named "routes.py" in the Flask Mega Tutorial. It seems like you have renamed the "routes.py" file to "WebService.py" so you can solve the import issue by changing the import line insideAPIService\__init__.pyto:
from app import WebService

Why can't WSGI configuration file find my Python variable?

I am trying to setup PythonAnywhere with a simple Python app but the WSGI config doesn't seem to be able to import properly, what am I doing wrong?
#!/usr/bin/python3.6
import web
import urllib
from xml.dom import minidom
... [code] ...
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
#app.wsgifunc()
and my WSGI is as follows
#!/usr/bin/python3.6
import sys
path = "/home/myUsername"
if path not in sys.path:
sys.path.append(path)
from myPythonFileNameInSameDir import app as application
application.wsgifunc()
PythonAnywhere dev here -- when you import your web.py app from the WSGI file, it won't run anything in the if __name__ == "__main__": section.
You need to do this in the app:
#!/usr/bin/python3.6
import web
import urllib
from xml.dom import minidom
... [code] ...
app = web.application(urls, globals())
if __name__ == "__main__":
app.run()
...and this in the WSGI file:
#!/usr/bin/python3.6
import sys
path = "/home/myUsername"
if path not in sys.path:
sys.path.append(path)
from myPythonFileNameInSameDir import app
application = app.wsgifunc()

Flask __init__.py import error

I can not import functions from other files to __init__.py in a flask. Importing something from a file gets an error 500.
__init__.py
from flask import Flask
from fel import fel
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True)
fel.py
def fel(a,b):
c = a+b
return (c)
If I delete the following line in the __init__.py file
from fel import fel
Everything is OK.
__init__.py and fel.py are in the same directory
I am working in Python 3.4
Where is the mistake?
edit:
structures
FlaskApp\
__init__.py
fel.py
use relative import
from .fel import fel
fel(something)
Explanation:
The problem of import fel is that you don't know whether its an
absolute import or a relative import. fel could a module in python's
path, or a package in the current module.
Source https://softwareengineering.stackexchange.com/questions/159503/whats-wrong-with-relative-imports-in-python
Your import should be:
from FlaskApp.fel import fel
And the parent directory of FlaskApp needs to be present in your sys.path somehow (for example, set the PYTHONPATH environment variable).
just
from flask import Flask
from .fel import fel
app = Flask(__name__)
#app.route('/')
def hello_world():
number = fel(4,6)
return (number)
if __name__ == '__main__':
app.run(debug=True)

Categories