Reputation: 1833
I have two files app.py
and mod_login.py
app.py
from flask import Flask
from mod_login import mod_login
app = Flask(__name__)
app.config.update(
USERNAME='admin',
PASSWORD='default'
)
mod_login.py
# coding: utf8
from flask import Blueprint, render_template, redirect, session, url_for, request
from functools import wraps
from app import app
mod_login = Blueprint('mod_login', __name__, template_folder='templates')
And python return this error:
Traceback (most recent call last):
File "app.py", line 2, in <module>
from mod_login import mod_login
File "mod_login.py", line 5, in <module>
from app import app
File "app.py", line 2, in <module>
from mod_login import mod_login
ImportError: cannot import name mod_login
If I delete from app import app
, code will be work, but how I can get access to app.config
?
Upvotes: 37
Views: 305682
Reputation: 333
In mycase, the imported module name collided with a local variable of the same name
# imported module
from expiry_type import get_expiry_type
# local variable with the same name as the imported module name
expiry_type
Upvotes: 0
Reputation: 1
Probably as i guess, you didnt remove the comment section in the views.py folder, right under the first defualt django code, which is " from django.shortcuts import render " ,under that you will find a comment just delete it and it will work fine.
that is the defualt comment made by the django app " # Create your views here. " ,delete it because it might interfer in the app doing the functions as it is in the second line, after that you can use it, after the app runs correctly, after identifying the path
Upvotes: 0
Reputation: 1547
In my Django project I have multiple apps and backend scripts, modules, packages that use the name utils.py
for their purposes. Initially, the problem seemed to be name collision among the python pakcages on name utils.py
. The collision also affected only one package. Renaming to package_utils.py
solved the problem.
However, the root cause was a missing __init__.py
file in one of the Django apps which also used a utils.py
. Adding the missing file solved the problem w/o renaming.
Upvotes: 0
Reputation: 11
I'm new to Python and just encountered this same error while trying to refactor my code. The cause of the error was a bit different in my case.
My code is written in a single file and I'm reorganizing it into modules. I got the error after grouping my code into modules and trying to run the flask server from run.py
. I tried the solutions given here but didn’t work for me.
Then I noticed that I had 2 different __pycache__
directories-one within my package(named src
in the attached snippet) and the other outside it. When I deleted the __pycache__
directory outside my package, the error cleared and I could run the flask server.
|- __pycache__/ #deleting this directory solved it for me
|- src
|- __pycache__/
|- __init__.py
|- static/
|- templates
…
Hope this is helpful.
Upvotes: 0
Reputation: 9
When this is in a python console if you update a module to be able to use it through the console does not help reset, you must use a
import importlib
and
importlib.reload (*module*)
likely to solve your problem
Upvotes: 0
Reputation: 23068
Instead of using local imports, you may import the entire module instead of the particular object. Then, in your app
module, call mod_login.mod_login
app.py
from flask import Flask
import mod_login
# ...
do_stuff_with(mod_login.mod_login)
mod_login.py
from app import app
mod_login = something
Upvotes: 6
Reputation: 1679
This can also happen if you've been working on your scripts and functions and have been moving them around (i.e. changed the location of the definition) which could have accidentally created a looping reference.
You may find that the situation is solved if you just reset the iPython kernal to clear any old assignments:
%reset
or menu->restart terminal
Upvotes: 30
Reputation: 10667
The problem is that you have a circular import: in app.py
from mod_login import mod_login
in mod_login.py
from app import app
This is not permitted in Python. See Circular import dependency in Python for more info. In short, the solution are
Upvotes: 65