The importing module does not work

I have a django project (but I think it is not a revelant here) where I am trying to add a script that I did before. So I put it in a subdirectory of my project and I have this structure (I know this is a bit of a mess at the moment, but it won't stay the same)

enter image description here

From views.py, I want to import main.py (especially the excelToXml function ). After searching on the internet, I found that the code I copied to views.py. If I fix it correctly, add the $ PATH variable to the parent first_page directory , and although each subdirectory

CURRENT = os.path.dirname(os.path.abspath(__file__))
PARENT = os.path.dirname(CURRENT)
sys.path.append(PARENT)
from ExcelToXML.main import excelToXml

      

I also created a __ init.py _ _ file in the ExcelToXML directory, this file is left blank.

However even I did everything so far I get this error when I start the django server

File "c: \ Users \ CRA \ AppData \ Local \ Programs \ Python \ Python36-32 \ Lib \ site-packages \ django \ bin \ DevisVersOpen \ DevisVersOpen \ urls.py", line 18, in the module

from the import views of the first page

File "C: \ Users \ CRA \ AppData \ Local \ Programs \ Python \ Python36-32 \ Lib \ site-packages \ Django \ Bin \ DevisVersOpen \ first_page \ views.py", line 13, in the module

from ExcelToXML.main import excelToXml

ModuleNotFoundError: No module named 'ExcelToXML'

I haven't found a solution that I could understand on the internet, so I really don't know how to solve this.

+3


source to share


3 answers


Your directory structure allows me to think that you should try to import like this:

from first_page.ExcelToXML.main import excelToXml

      



because ExcelToXML is under the first_page module, so it is treated as a submodule of first_page.

+4


source


Ok, the solution you found is (I think) a mess, you should read the official documentation about this https://docs.python.org/3.6/tutorial/modules.html#packages .
Add briefly from .idea.main import excelToXml

. If that doesn't work, rename the .idea

folder to idea

(no dot) and add to your views.py

this line:from idea.main import excelToXml



0


source


Check it out to see what the init .py file is. What is __init__.py for?

The init .py file must be present in every directory and subdirectory whose classes must be visible for import. In your case, I suspect the parent directory does not have an init .py file . Add the file to the parent directory and import it like this:

import first_page.ExcelToXML

0


source







All Articles