34

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?

Robert
  • 9,485
  • 14
  • 62
  • 110
Patrick Burns
  • 1,663
  • 6
  • 20
  • 32
  • 1
    What if you just try `from mod_login import *` and `from app import *` –  Jul 24 '13 at 21:53
  • 2
    Possible duplicate of [ImportError: Cannot import name X](http://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x) – Baum mit Augen May 18 '16 at 16:33
  • @Sebastian Thanks - that was a saver! I dont see a circular reference but was still getting the above error. Using `*` helped. I wonder whats the logic though. – Anupam Feb 01 '18 at 09:25
  • 1
    @Sebastian I spoke too soon. doesnt help. When I try to use the class it doesnt find it and gives the import error. I know there is no circular import and have also tried deleting all *.pyc files. Dont know what is missing – Anupam Feb 01 '18 at 10:04

5 Answers5

64

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

  • either gather everything in one big file
  • delay one of the import using local import
Community
  • 1
  • 1
hivert
  • 10,396
  • 3
  • 29
  • 55
28

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

mjp
  • 1,484
  • 2
  • 21
  • 34
  • 2
    thanks man. at my company we build python but it also just works without building except when i renamed all my files just like you said. so this made me think to actually rebuild, which solved the problem. woohoo! – Alexander Taylor May 26 '16 at 06:49
  • 4
    In my case, I refactored a single python script into different modules, leaving some old .py and .pyc files around, and stumbled on the "cannot import name" error. After a useless search of circular references, your answer put me on the right way - deleting old files did the trick. thanks – dipanda Oct 13 '16 at 16:06
  • 3
    Wanted to point out that this was a lifesaver. I did not have a circular reference, and was thoroughly confused as to why this was happening. – ste_kwr Jan 29 '17 at 19:12
5

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
Jivan
  • 19,150
  • 13
  • 65
  • 110
0

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

0

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.

Ziimm_
  • 11
  • 3