Flask Temperamental While Loading Basic Web Application - python

I'm having trouble starting Flask for Python 2.7 via Eclipse & Chrome; it's so temperamental am having trouble clarifying the situations it works/doesn't:
Files are .project (Eclipse), init.py & pymongo1.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'index page'
app.run(debug=True)
I load Eclipse, hit Run and get:
pydev debugger: starting (pid: 5296)
Finding files... done.
Importing test modules ... * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
pydev debugger: starting (pid: 2312)
Finding files... done.
Importing test modules ...
which simply hands. In Chrome, http://127.0.0.1:5000/ or 127.0.0.1:5000/ won't load anything. Changing 'index page' to 'Index page' & hitting save gives
* Detected change in 'C:\\Users\\Eddie\\Desktop\\dev\\health\\pymongo1.py', reloading
127.0.0.1 - - [28/Sep/2015 14:57:49] "GET / HTTP/1.1" 200 -
pydev debugger: Start reloading module: "pymongo1" ...
pydev debugger: Start reloading module: "pymongo1" ...
..and keeps Chrome loading. Hitting Run on Eclipse then gives:
pydev debugger: Start reloading module: "pymongo1" ...
pydev debugger: Start reloading module: "pymongo1" ...
* Detected change in 'C:\\Users\\Eddie\\Desktop\\dev\\health\\pymongo1.py', reloading
pydev debugger: Start reloading module: "pymongo1" ...
* Detected change in 'C:\\Users\\Eddie\\Desktop\\dev\\health\\pymongo1.py', reloading
Traceback (most recent call last):
File "C:\Users\Eddie\Downloads\eclipse\plugins\org.python.pydev_4.0.0.201504132356\pysrc\pydev_runfiles.py", line 468, in __get_module_from_str
mod = __import__(modname)
File "C:\Users\Eddie\Downloads\eclipse\plugins\org.python.pydev_4.0.0.201504132356\pysrc\pydev_monkey_qt.py", line 71, in patched_import
return original_import(name, *args, **kwargs)
File "C:\Users\Eddie\Desktop\dev\health\pymongo1.py", line 115, in <module>
app.run(debug=True)
File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\app.py", line 772, in run
run_simple(host, port, self, **options)
File "C:\Python27\lib\site-packages\werkzeug-0.10.4-py2.7.egg\werkzeug\serving.py", line 623, in run_simple
reloader_type)
File "C:\Python27\lib\site-packages\werkzeug-0.10.4-py2.7.egg\werkzeug\_reloader.py", line 229, in run_with_reloader
reloader.run()
File "C:\Python27\lib\site-packages\werkzeug-0.10.4-py2.7.egg\werkzeug\_reloader.py", line 131, in run
self.trigger_reload(filename)
File "C:\Python27\lib\site-packages\werkzeug-0.10.4-py2.7.egg\werkzeug\_reloader.py", line 111, in trigger_reload
sys.exit(3)
SystemExit: 3
ERROR: Module: pymongo1 could not be imported (file: C:\Users\Eddie\Desktop\dev\health\pymongo1.py).
Tried shutting down other apps in case they run on the same port, like Google Photos. Any clues why it temperamentally loads content/seems to be getting stuck on the 'importing test module' part? Regards

Resolved by #riotburn and #Joe Doherty. The py file needed starting from Windows PowerShell.

Related

How to rename Flask WSGI App during development?

I would like to know how can I change the name of the Flask WSGI App during the development stage.
Using the Flask Mega-Tutorial as reference, I was able to successfully setup a "Hello World" app.
Digressions from the tutorial:
Use pipenv as my Python virtual environment manager (instead of venv)
Name of the app is astronomer.py.
Now, I want to build on top of the existing app and customize the code to my requirements; starting with the app name that I have defined in the .flaskenv file as FLASK_APP env var.
Accordingly, I have updated the name of the root level Python script from astronomer.py (in the tutorial) to galielo.py (for my use). After changing the corresponding value of FLASK_APP and restarting the flask server via $ pipenv run flask run, the app crashes with the following error:
$ pipenv run flask run [12:29:33]
* Serving Flask app "astronomer.py" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 302-012-958
127.0.0.1 - - [09/Oct/2019 12:29:57] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
ModuleNotFoundError: No module named 'astronomer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 338, in __call__
self._flush_bg_loading_exception()
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 326, in _flush_bg_loading_exception
reraise(*exc_info)
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 314, in _load_app
self._load_unlocked()
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 250, in locate_app
raise NoAppException('Could not import "{name}".'.format(name=module_name))
flask.cli.NoAppException: Could not import "astronomer".
Debugging
After logging into the virtual env and checking for the value of the env var FLASK_APP, I get the old value of astronomer.py. This explains why the application is not starting. However, I'm not able to understand why this is happening?
I even tried using "eager-loading" the app using: $ pipenv run flask run --eager-loading
Still, the app does not start with the same error message ofcourse.
I was able to solve this manually by unsetting the env var FLASK_APP from within the virtual env and restarting the flask server. I'm curious to know about why the app is not loading the file .flaskenv at initialization and if there is an automated way to do this?
With Pipenv, I think things are a little bit different when it comes to environment variables. As per the documentation, there is a builtin mechanism for loading a .env file:
If a .env file is present in your project, $ pipenv shell and $ pipenv run will automatically load it, for you
So I guess you should rename your file from .flaskenv to .env and then safely remove the python-dotenv dependency.

Django run manage.py generates OS error in OS X Yosemite

I'm having problem with running Django manage.py runserver on my MAC OS X Yosemite.
When I ran it soon after restarting, everything works fine. But after several minutes, the running process will stop, and when I re-rerun manage.py runserver, it will generate this error
Performing system checks...
System check identified no issues (0 silenced).
January 22, 2015 - 15:10:11
Django version 1.7.3, using settings 'mycustomapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 83, in handle
self.run(*args, **options)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 92, in run
autoreload.main(self.inner_run, args, options)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 322, in main
reloader(wrapped_main_func, args, kwargs)
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 288, in python_reloader
reloader_thread()
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 264, in reloader_thread
change = fn()
File "/Users/fendy/.virtualenvs/mycustomapp/lib/python2.7/site-packages/django/utils/autoreload.py", line 202, in code_changed
stat = os.stat(filename)
OSError: [Errno 2] No such file or directory: './manage.py'
Note that I am in the same directory as manage.py does. but for some reasons, it could not find it
here is what I found when I run ls:
manage.py requirements.txt mycustomapp
My django version is 1.7.3, OS YOSEMITE, I had all brew updated and upgraded
Any help would be appreciated
Thanks !
There's no need to upgrade or degrade your Django version for an OSError [Error123].
I got this error for weeks and I thought about the actual reason behind this error because my code and files were all perfect! But then, after weeks of observation of my code, I figured out the hell of that mistake!
ANSWER - Please create your APPS, STATIC-FOLDER, TEMPLATE-FOLDER, and further things within your PROJECT!!
DEMONSTRATION -> { '>>' indicates next step}
OPEN POWERSHELL >> mkdir DIRECTORY_NAME >> cd DIRECTORY_NAME >> code .
this will open visual studio code within the directory named (DIRECTORY_NAME)
OPEN TERMINAL >> install django >> django startproject PROJECT_NAME >>
cd PROJECT_NAME >> python manage.py runserver >> COME OUT OF THE SERVER >> django startapp APP_NAME >> CONTINUE YOUR WORK IN THE VS CODE AND ONLY COM TO TERMINAL TO RUN SERVER.
I've managed to resolve the issue. Turns out since in this Django version I would need to separate the wsgi file for development and production use. therefore wsgi_prod.py and wsgi.py . This action was not needed in previous versions of Django.

Error running django server from PyCharm with local settings file

I am trying to get my django server running from within PyCharm. I am able to start the server with no errors, but as soon as I browse to a page in the app I get the following error in the console:
pydev debugger: starting
Validating models...
0 errors found
Django version 1.4.10, using settings 'oneanddone.settings.local'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/bsilverberg/gitRepos/oneanddone/vendor/lib/python/django/contrib/staticfiles/handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "/Users/bsilverberg/gitRepos/oneanddone/vendor/lib/python/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/Users/bsilverberg/gitRepos/oneanddone/vendor/lib/python/django/core/handlers/base.py", line 82, in get_response
urlconf = settings.ROOT_URLCONF
File "/Users/bsilverberg/gitRepos/oneanddone/vendor/lib/python/django/utils/functional.py", line 185, in inner
return func(self._wrapped, *args)
AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'
[09/Dec/2013 09:00:41] "GET /en-US/ HTTP/1.1" 500 59
I have a settings/local.py file and a settings/base.py file, and when I start the server from the terminal everything works fine. When starting from PyCharm I get the above error. My local settings file is getting loaded, as I can inspect the value of settings at this point in the code and can see my local values, but it seems like the settings from base.py are not being picked up (as that is where ROOT_URLCONF is defined).
So this doesn't seem to be an issue with DJANGO_SETTINGS_MODULE - it is finding the local.py file.
I'm running PyCharm 2.7.3 and django 1.4.
The most frustrating thing about this is that I had this working with a similar application a few months ago, and cannot figure out why it's not working now.
I just figured it out. All I had to do was point to the settings folder, instead of a specific file in the folder, and it all works.

How do I publish a django web app to Google App Engine from Visual Studio 2012

I am trying to set up a django application in Visual Studio 2012 that will publish to Google App Engine. I have a simple hello World Application set up in Visual Studio using pytools that I can run successfully in the development environment.
I added an app.yaml file, downloaded the AppEngine SDK and imported the existing application to the Google App Engine Launcher application. If I start the application locally using the App Engine Launcher, it starts but the page is just an error. I get the following from the log:
2013-05-23 07:47:03 Running command: "['C:\\Python27\\pythonw.exe', 'C:\\Program Files\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8000', 'Z:\\gridtreecontrol\\GridTreeView\\GridTreeView']"
INFO 2013-05-23 07:47:09,496 devappserver2.py:522] Skipping SDK update check.
WARNING 2013-05-23 07:47:09,523 api_server.py:329] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO 2013-05-23 07:47:09,543 api_server.py:153] Starting API server at: http://localhost:61149
INFO 2013-05-23 07:47:09,552 dispatcher.py:164] Starting server "default" running at: http://localhost:8080
INFO 2013-05-23 07:47:09,558 admin_server.py:117] Starting admin server at: http://localhost:8000
ERROR 2013-05-23 12:47:32,108 wsgi.py:235]
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 223, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "C:\Program Files\Google\google_appengine\lib\django-1.4\django\core\handlers\wsgi.py", line 219, in __call__
self.load_middleware()
File "C:\Program Files\Google\google_appengine\lib\django-1.4\django\core\handlers\base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "C:\Program Files\Google\google_appengine\lib\django-1.4\django\utils\functional.py", line 184, in inner
self._setup()
File "C:\Program Files\Google\google_appengine\lib\django-1.4\django\conf\__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "C:\Program Files\Google\google_appengine\lib\django-1.4\django\conf\__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named settings
INFO 2013-05-23 07:47:32,128 server.py:585] default: "GET / HTTP/1.1" 500 -
It seems like it wants me to make some adjustments to sys.path, but I feel like if I did that, it won't propagate those changes to the production server on Google App Engine. I have tried publishing the application to the server, but get the same error there.
I do not get this error when running the application in Visual Studio.
You should not try to develop a standalone Django application and then try to convert it to GAE. There are many differences in running Django inside GAE, and it's best to start from there in the first place.
I don't know why it's important to you to be able to run your app "inside" VS. Again, you should use the GAE development server - you can continue to write your code in VS, and the devserver will even reload when it detects code changes. You might be able to configure VS to use the GAE devserver instead of the Django one, but it sounds like a lot of unnecessary fiddling.

ImproperlyConfigured: Error importing middleware pp.middleware: "No module named pp.middleware"

im new to django and python , im trying to do runserver on a piece of django code and im running into the following problems,
Kinnovates-MacBook-Pro:platformsite Kinnovate$ python manage.py runserver
Running in development mode.
Running in development mode.
Running in development mode.
Running in development mode.
Validating models...
0 errors found
Django version 1.4b1, using settings 'platformsite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware pp.middleware: "No module named pp.middleware"
im running python2.7 1.4b Django on a macOSX10.7
how do i fix this?
You have a middleware class specified in settings.MIDDLEWARE_CLASSES that Django is trying to import, yet it isn't available on your Python path.
You should make sure that you have obtained the middleware sources and that your Python path is properly configured.

Categories

Resources