ImportError: cannot import name 'variable' from 'module'
I am using Flask for my web framework. I am having an issue with imports. I am not understanding why can't I import my variable when I declare it within my my_app/__init__.py
:
from flask import Flask
from flask_login import LoginManager
from my_app.some_module.my_class.py import auth
app = Flask(__name__)
login_manager = LoginManager()
class Config:
def __init__(self):
pass
config = Config()
My conflictuous imports are present in my_app/some_module/my_class.py
:
from flask import Blueprint
from my_app import login_manager # this one works fine
from my_app import config
auth = Blueprint('auth', __name__)
I run the app with run.py
:
from my_app import app
app.run(debug=True)
I then get the error:
Traceback (most recent call last):
...
File ".../my_app/some_module/my_class.py", line 1, in <module>
from my_app import login_manager, config
ImportError: cannot import name 'config' from 'my_app' (.../my_app/__init__.py)
Project structure is:
my_app
+ __init__.py
some_module
+ __init__.py
+ my_class.py
+ run.py
python
|
show 11 more comments
I am using Flask for my web framework. I am having an issue with imports. I am not understanding why can't I import my variable when I declare it within my my_app/__init__.py
:
from flask import Flask
from flask_login import LoginManager
from my_app.some_module.my_class.py import auth
app = Flask(__name__)
login_manager = LoginManager()
class Config:
def __init__(self):
pass
config = Config()
My conflictuous imports are present in my_app/some_module/my_class.py
:
from flask import Blueprint
from my_app import login_manager # this one works fine
from my_app import config
auth = Blueprint('auth', __name__)
I run the app with run.py
:
from my_app import app
app.run(debug=True)
I then get the error:
Traceback (most recent call last):
...
File ".../my_app/some_module/my_class.py", line 1, in <module>
from my_app import login_manager, config
ImportError: cannot import name 'config' from 'my_app' (.../my_app/__init__.py)
Project structure is:
my_app
+ __init__.py
some_module
+ __init__.py
+ my_class.py
+ run.py
python
maybe you meant to import the classConfig
instead of the variableconfig
(note the capitalisation / lack of it)?
– Tilman B. aka Nerdyyy
Nov 15 '18 at 17:05
2
Is that all the contents of your__init__.py
?
– user2357112
Nov 15 '18 at 17:07
2
Show us the complete, exact error message, including full stack trace.
– user2357112
Nov 15 '18 at 17:07
1
What part of your code importsmy_module.my_class
?
– MisterMiyagi
Nov 15 '18 at 17:13
2
For the record, I just gave it a quick test with your example code and it worked for me with usingimport my_module.my_class
from a higher directory.
– Peter
Nov 15 '18 at 17:13
|
show 11 more comments
I am using Flask for my web framework. I am having an issue with imports. I am not understanding why can't I import my variable when I declare it within my my_app/__init__.py
:
from flask import Flask
from flask_login import LoginManager
from my_app.some_module.my_class.py import auth
app = Flask(__name__)
login_manager = LoginManager()
class Config:
def __init__(self):
pass
config = Config()
My conflictuous imports are present in my_app/some_module/my_class.py
:
from flask import Blueprint
from my_app import login_manager # this one works fine
from my_app import config
auth = Blueprint('auth', __name__)
I run the app with run.py
:
from my_app import app
app.run(debug=True)
I then get the error:
Traceback (most recent call last):
...
File ".../my_app/some_module/my_class.py", line 1, in <module>
from my_app import login_manager, config
ImportError: cannot import name 'config' from 'my_app' (.../my_app/__init__.py)
Project structure is:
my_app
+ __init__.py
some_module
+ __init__.py
+ my_class.py
+ run.py
python
I am using Flask for my web framework. I am having an issue with imports. I am not understanding why can't I import my variable when I declare it within my my_app/__init__.py
:
from flask import Flask
from flask_login import LoginManager
from my_app.some_module.my_class.py import auth
app = Flask(__name__)
login_manager = LoginManager()
class Config:
def __init__(self):
pass
config = Config()
My conflictuous imports are present in my_app/some_module/my_class.py
:
from flask import Blueprint
from my_app import login_manager # this one works fine
from my_app import config
auth = Blueprint('auth', __name__)
I run the app with run.py
:
from my_app import app
app.run(debug=True)
I then get the error:
Traceback (most recent call last):
...
File ".../my_app/some_module/my_class.py", line 1, in <module>
from my_app import login_manager, config
ImportError: cannot import name 'config' from 'my_app' (.../my_app/__init__.py)
Project structure is:
my_app
+ __init__.py
some_module
+ __init__.py
+ my_class.py
+ run.py
python
python
edited Nov 16 '18 at 10:17
Renato Damas
asked Nov 15 '18 at 17:01
Renato DamasRenato Damas
9151822
9151822
maybe you meant to import the classConfig
instead of the variableconfig
(note the capitalisation / lack of it)?
– Tilman B. aka Nerdyyy
Nov 15 '18 at 17:05
2
Is that all the contents of your__init__.py
?
– user2357112
Nov 15 '18 at 17:07
2
Show us the complete, exact error message, including full stack trace.
– user2357112
Nov 15 '18 at 17:07
1
What part of your code importsmy_module.my_class
?
– MisterMiyagi
Nov 15 '18 at 17:13
2
For the record, I just gave it a quick test with your example code and it worked for me with usingimport my_module.my_class
from a higher directory.
– Peter
Nov 15 '18 at 17:13
|
show 11 more comments
maybe you meant to import the classConfig
instead of the variableconfig
(note the capitalisation / lack of it)?
– Tilman B. aka Nerdyyy
Nov 15 '18 at 17:05
2
Is that all the contents of your__init__.py
?
– user2357112
Nov 15 '18 at 17:07
2
Show us the complete, exact error message, including full stack trace.
– user2357112
Nov 15 '18 at 17:07
1
What part of your code importsmy_module.my_class
?
– MisterMiyagi
Nov 15 '18 at 17:13
2
For the record, I just gave it a quick test with your example code and it worked for me with usingimport my_module.my_class
from a higher directory.
– Peter
Nov 15 '18 at 17:13
maybe you meant to import the class
Config
instead of the variable config
(note the capitalisation / lack of it)?– Tilman B. aka Nerdyyy
Nov 15 '18 at 17:05
maybe you meant to import the class
Config
instead of the variable config
(note the capitalisation / lack of it)?– Tilman B. aka Nerdyyy
Nov 15 '18 at 17:05
2
2
Is that all the contents of your
__init__.py
?– user2357112
Nov 15 '18 at 17:07
Is that all the contents of your
__init__.py
?– user2357112
Nov 15 '18 at 17:07
2
2
Show us the complete, exact error message, including full stack trace.
– user2357112
Nov 15 '18 at 17:07
Show us the complete, exact error message, including full stack trace.
– user2357112
Nov 15 '18 at 17:07
1
1
What part of your code imports
my_module.my_class
?– MisterMiyagi
Nov 15 '18 at 17:13
What part of your code imports
my_module.my_class
?– MisterMiyagi
Nov 15 '18 at 17:13
2
2
For the record, I just gave it a quick test with your example code and it worked for me with using
import my_module.my_class
from a higher directory.– Peter
Nov 15 '18 at 17:13
For the record, I just gave it a quick test with your example code and it worked for me with using
import my_module.my_class
from a higher directory.– Peter
Nov 15 '18 at 17:13
|
show 11 more comments
2 Answers
2
active
oldest
votes
The problem is that you have a cyclic dependency. By the time you import auth
from my_app.some_module.my_class.py
your config
is not set yet. Try moving that import to the end of the my_app/__init__.py
file like:
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
login_manager = LoginManager()
class Config:
pass
config = Config()
from my_app.some_module.my_class.py import auth
add a comment |
You have a cyclic import: my_app.some_module
-> my_app.some_module.my_class
-> my_app.some_module
.
You can fix this by moving both Config
and config
to a separate module my_app.some_module.config
.
# my_app.some_module.my_config
class Config:
pass
config = Config()
# my_app.some_module.my_class
from .my_config import config
# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass
This means that every import does not depend on previous imports:
my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
-> my_app.some_module.my_config
Doing imports this way instead of moving the import for .my_class
to the end of __init__.py
is more robust. You can freely reorder the imports of .my_class
and .my_config
at the top of files.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324497%2fimporterror-cannot-import-name-variable-from-module%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that you have a cyclic dependency. By the time you import auth
from my_app.some_module.my_class.py
your config
is not set yet. Try moving that import to the end of the my_app/__init__.py
file like:
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
login_manager = LoginManager()
class Config:
pass
config = Config()
from my_app.some_module.my_class.py import auth
add a comment |
The problem is that you have a cyclic dependency. By the time you import auth
from my_app.some_module.my_class.py
your config
is not set yet. Try moving that import to the end of the my_app/__init__.py
file like:
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
login_manager = LoginManager()
class Config:
pass
config = Config()
from my_app.some_module.my_class.py import auth
add a comment |
The problem is that you have a cyclic dependency. By the time you import auth
from my_app.some_module.my_class.py
your config
is not set yet. Try moving that import to the end of the my_app/__init__.py
file like:
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
login_manager = LoginManager()
class Config:
pass
config = Config()
from my_app.some_module.my_class.py import auth
The problem is that you have a cyclic dependency. By the time you import auth
from my_app.some_module.my_class.py
your config
is not set yet. Try moving that import to the end of the my_app/__init__.py
file like:
from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
login_manager = LoginManager()
class Config:
pass
config = Config()
from my_app.some_module.my_class.py import auth
answered Nov 16 '18 at 10:23
Renato DamasRenato Damas
9151822
9151822
add a comment |
add a comment |
You have a cyclic import: my_app.some_module
-> my_app.some_module.my_class
-> my_app.some_module
.
You can fix this by moving both Config
and config
to a separate module my_app.some_module.config
.
# my_app.some_module.my_config
class Config:
pass
config = Config()
# my_app.some_module.my_class
from .my_config import config
# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass
This means that every import does not depend on previous imports:
my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
-> my_app.some_module.my_config
Doing imports this way instead of moving the import for .my_class
to the end of __init__.py
is more robust. You can freely reorder the imports of .my_class
and .my_config
at the top of files.
add a comment |
You have a cyclic import: my_app.some_module
-> my_app.some_module.my_class
-> my_app.some_module
.
You can fix this by moving both Config
and config
to a separate module my_app.some_module.config
.
# my_app.some_module.my_config
class Config:
pass
config = Config()
# my_app.some_module.my_class
from .my_config import config
# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass
This means that every import does not depend on previous imports:
my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
-> my_app.some_module.my_config
Doing imports this way instead of moving the import for .my_class
to the end of __init__.py
is more robust. You can freely reorder the imports of .my_class
and .my_config
at the top of files.
add a comment |
You have a cyclic import: my_app.some_module
-> my_app.some_module.my_class
-> my_app.some_module
.
You can fix this by moving both Config
and config
to a separate module my_app.some_module.config
.
# my_app.some_module.my_config
class Config:
pass
config = Config()
# my_app.some_module.my_class
from .my_config import config
# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass
This means that every import does not depend on previous imports:
my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
-> my_app.some_module.my_config
Doing imports this way instead of moving the import for .my_class
to the end of __init__.py
is more robust. You can freely reorder the imports of .my_class
and .my_config
at the top of files.
You have a cyclic import: my_app.some_module
-> my_app.some_module.my_class
-> my_app.some_module
.
You can fix this by moving both Config
and config
to a separate module my_app.some_module.config
.
# my_app.some_module.my_config
class Config:
pass
config = Config()
# my_app.some_module.my_class
from .my_config import config
# my_app.some_module.__init__
from .my_config import config
from .my_class import MyClass
This means that every import does not depend on previous imports:
my_app.some_module
|-> my_app.some_module.my_class -> my_app.some_module.config
-> my_app.some_module.my_config
Doing imports this way instead of moving the import for .my_class
to the end of __init__.py
is more robust. You can freely reorder the imports of .my_class
and .my_config
at the top of files.
answered Nov 16 '18 at 10:50
MisterMiyagiMisterMiyagi
7,9862446
7,9862446
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324497%2fimporterror-cannot-import-name-variable-from-module%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
maybe you meant to import the class
Config
instead of the variableconfig
(note the capitalisation / lack of it)?– Tilman B. aka Nerdyyy
Nov 15 '18 at 17:05
2
Is that all the contents of your
__init__.py
?– user2357112
Nov 15 '18 at 17:07
2
Show us the complete, exact error message, including full stack trace.
– user2357112
Nov 15 '18 at 17:07
1
What part of your code imports
my_module.my_class
?– MisterMiyagi
Nov 15 '18 at 17:13
2
For the record, I just gave it a quick test with your example code and it worked for me with using
import my_module.my_class
from a higher directory.– Peter
Nov 15 '18 at 17:13