ImportError: cannot import name routes - python-2.7

I am doing Miguel Grinberg's tutorial on Flask. I am having a strange issue: before, this wasn't happening, but now it is. When I try to run flask shell or simply run my application, I receive the following error:
NoAppException: While importing "app.microblog", an ImportError was raised:
Traceback (most recent call last):
File "c:\projects\blog\virtualenv\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "c:\Projects\Blog\app\__init__.py", line 14, in <module>
from app import routes, models
File "app.py", line 11, in <module>
ImportError: cannot import name routes
I thought it might be a circular dependency problem, but that didn't seem to be the case. I've tried searching all over for the answre but can't seem to figure it out.
Thanks for your help.

It seems you are importing the routes from app module within app module.. If you can share some more snippet, that would be helpful in understanding the problem.
But it seems,
from app import routes, models
File "app.py", line 11, in <module>
ImportError: cannot import name routes
In these lines, it says the exception is occuring in from app import routes, models line which is line 11 of app.py file.
So you are just importing the app module within app module.

I think you shouldn't import like the way you do already. Instead use the following:
import app
...
...
...
#app.routes('/something', methods=['DESIRED_METHODS']
def your_function():
pass

Related

Cannot use module aioflask(Python). ImportError: cannot import name '_app_ctx_stack' from 'flask.ctx'

I need to use aioflask for seting webhooks for my telegram-bot. Here my code, where I set webhook:
from aioflask import Flask, request
...
app = Flask(__name__)
...
#app.route('/')
async def webhook():
await bot.delete_webhook()
await bot.set_webhook(url=APP_URL)
return '!', 200
...
But, when I run app, it give me this error:
Traceback (most recent call last):
File "D:/Python_Projects/FilmMarketBot/check.py", line 1, in <module>
from aioflask import Flask, request
File "D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\aioflask\__init__.py", line 2, in <module>
from .app import Flask
File "D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\aioflask\app.py", line 14, in <module>
from .ctx import AppContext, RequestContext
File "D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\aioflask\ctx.py", line 4, in <module>
from flask.ctx import AppContext as OriginalAppContext, \
ImportError: cannot import name '_app_ctx_stack' from 'flask.ctx' (D:\Python_Projects\FilmMarketBot\venv\lib\site-packages\flask\ctx.py)
Please, tell how can I fix it. ...Why always me?
There seems to be a breaking change in Flask 2.2.0, which causes this incompatibility. As a workaround you can downgrade your Flask package to 2.1.3. This change solved the issue for me.
Reported the issue on GitHub: https://github.com/miguelgrinberg/aioflask/issues/10

Django on Pycharm: ImproperlyConfigured with DJANGO_SETTINGS_MODULE

I am trying to use Pycharm Community Edition to improve on my code in my Django application, but I cannot run all of my Django code that I'd like. I keep getting this traceback...
Traceback (most recent call last):
File "C:/Users/Jaysp_000/firstSite/PROJECTone/blog_static/views.py", line 1, in <module>
from django.views.decorators.csrf import csrf_exempt
File "C:\Python34\lib\site-packages\django\views\decorators\csrf.py", line 3, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in <module>
from django.core.cache import caches
File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This error seems to involve the django.views.decortors.csrf.csrf_exempt that I imported to my views.py file. I've tried other files, and they have given me no issues. There is something in particular about this import, but I don't know what.
from django.views.decortors.csrf import csrf_exempt
#csrf_exempt
def handle_hook(request):
from django.http import HttpResponse
from django.core.management import call_command
result = call_command('update_blog', verbosity = 0)
return HttpResponse(result)
The same kind of issue shows up when I am trying to run the code on the python shell (I use 3.4) and when I import django.http.request as request. I type in handle_hook(request), and I get the same kind of error.
Im being told that I must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings, but I haven't a clue on how to do that. I've looked around and I am not certain if those methods specifically speak to my issue. Any clues?
Go to the Run Menu, select Edit Configurations..., then select the run configuration for you tests.
Select the environment variables button. You'll see one existing variable, which is PYTHONUNBUFFERED
Under that add (for example) DJANGO_SETTINGS_MODULE=mysitename.settings

ImportError: no module named contact.forms

according to Djangobook.com chapter7 I'm trying to import ContactForm from contact.forms but I faced this:
from contact.forms import ContactForm
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named contact.forms
as much as I struggled and searched find nothing. I would appreciate your advise
I had a similar problem with the relative paths in Django Book when I went through. Normally just check that the forms file is in the same folder as the folder you are working on. Otherwise try appname.contact.forms or just forms. Something like that. If you could post the file branching of your project that would help too.

weird django error: cannot import name get_path_info

I suddenly started getting this error when I try to start the django development server, and I cannot figure out where it is coming from. I have tried updating django.
File "/virtualEnv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 6, in <module>
from django.contrib.staticfiles.handlers import StaticFilesHandler
File "/virtualEnv/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 9, in <module>
from django.core.handlers.base import get_path_info
ImportError: cannot import name get_path_info
Has anyone seen any error like this before? Definitely trying my best to figure this out...
For me, the problem was that dj_static 0.0.5 didn't seem to be playing nicely with Django 1.7. I upgraded to dj_static 0.0.6 and it fixed it.
I had a similar issue.
Resolved for Django 1.7 by changing the import path for get_path_info:
from django.core.handlers.base import get_path_info
became:
from django.core.handlers.wsgi import get_path_info

Django 1.4 not recognizing my app

I am using django 1.4, and the app.module name does not seem to be working as expected for me. My settings.py has the app included as "survey.surveys".
Here is the django shell dump for an import I attempted through a preexisting file. The error says No module named ....
from survey.surveys.views.db import mobile_survey1
Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/pjain/dev/survey/survey/surveys/views/db.py", line 6, in <module>
from survey.surveys.models import Survey, Section, Question, Option
File "/home/pjain/dev/survey/survey/surveys/views/survey.py", line 7, in <module>
from survey.surveys.models import Survey, Section, Question, Option
ImportError: No module named surveys.models
However, when I copied and pasted the exact import which breaks, it imports fine in the shell.
from survey.surveys.models import Survey, Section, Question, Option
How can I fix this import in my project?