简体   繁体   中英

Python not able to import module

I have quite a number of different file which I want to use to create an API. The files are:

  1. app.py -- main file
  2. utils.py -- utilities file
  3. recorder.py
  4. consts.py
  5. submitter.py

All of these files have a dependency on one another. However, when I try to import them in a particular file ( for ex - importing consts in recorder.py, I am getting error saying The module consts is not found. I am importing it using:

from .consts import consts 

Can someone tell me what am I doing wrong here, and how to solve this issue.

If i import it in the main app.py, it doesn't give any error. but it does when I want to access any consts in other files.

Thank you in advance

EDIT: Here is the folder structure:

在此处输入图像描述

When you say they are interdependent, that may be your issue. You cannot have two files that depend directly on one another with out some hacky solutions as youll run into the problem of importing one that tries to import the importer which tries to import the importee and so on forever.

Edit: i misunderstood how imports work, the problem comes from:

"If you import X from your main program, Python will load the code for X and execute it. When Python reaches the import Y statement, it loads the code for Y, and starts executing it instead.

At this time, Python has installed module objects for both X and Y in sys.modules. But X doesn't contain anything yet; the def spam statement hasn't been executed."

As stated in the link i included at the bottom of this response. The link also gives some possible solutions like changing when you import the different modules.

What we really need is to see the code for the files to understand how you are importing everything

this link should help you in your desire for circular dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM