ImportError: cannot import name 'variable' from 'module'












0















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









share|improve this question

























  • 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





    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
















0















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









share|improve this question

























  • 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





    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














0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 class Config instead of the variable config (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



















  • 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





    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

















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












2 Answers
2






active

oldest

votes


















0














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





share|improve this answer































    0














    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.






    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      0














      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





      share|improve this answer




























        0














        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





        share|improve this answer


























          0












          0








          0







          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





          share|improve this answer













          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 10:23









          Renato DamasRenato Damas

          9151822




          9151822

























              0














              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.






              share|improve this answer




























                0














                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.






                share|improve this answer


























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 10:50









                  MisterMiyagiMisterMiyagi

                  7,9862446




                  7,9862446






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python