导入模块无法正常工作(Importing module not working)

编程入门 行业动态 更新时间:2024-10-22 12:35:39
导入模块无法正常工作(Importing module not working)

我有一个django(但我觉得这里也不虔诚)项目,我尝试添加我之前做过的脚本。 所以我把它放在我的项目的一个子目录中,我有这个结构(我知道它现在有点乱,但它不会完全像那样)

views.py我想导入main.py (特别是函数excelToXml )。 在互联网上搜索后,我发现我在views.py中复制的代码。 如果我正确地将它取消,它会将变量$ PATH添加到first_page的目录parent,尽管每个子目录

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

我还在ExcelToXML目录中创建了一个__init.py_ _文件,此文件为空。

但是,即使我做了所有这些,当我运行django服务器时仍然会出现此错误

文件“c:\ Users \ CRA \ AppData \ Local \ Programs \ Python \ Python36-32 \ Lib \ site-packages \ django \ bin \ DevisVersOpen \ DevisVersOpen \ urls.py”,第18行,在模块中

来自first_page导入视图

文件“c:\ Users \ CRA \ AppData \ Local \ Programs \ Python \ Python36-32 \ Lib \ site-packages \ django \ bin \ DevisVersOpen \ first_page \ views.py”,第13行,在模块中

从ExcelToXML.main导入excelToXml

ModuleNotFoundError:没有名为'ExcelToXML'的模块

我没有找到任何我能在互联网上理解的解决方案,所以我真的不知道如何解决这个问题

I have a django (but i think it's nor revelant here) project where I try to add a script i did before. So I put it in a subdirectory of my project, and i have this structure (I know it's a little bit of a mess at the moment but it won't stay exactly like that)

From views.py i want to import main.py (Especially the function excelToXml) . After searches on internet i found that code that i copied in views.py . If I undestood it right it add to the variable $PATH the directory parent of first_page and though, every 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 file __init.py__ in the directory ExcelToXML, this file is left empty.

However even I did all that i still get this error when i run 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 module

from first_page import views

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

from ExcelToXML.main import excelToXml

ModuleNotFoundError: No module named 'ExcelToXML'

I didn't find any solution that I could understand on internet so I really don't know how to solve this

最满意答案

你的目录结构让我觉得你应该尝试像这样导入:

from first_page.ExcelToXML.main import excelToXml

因为ExcelToXML位于first_page模块下,所以它被视为first_page的子模块。

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

from first_page.ExcelToXML.main import excelToXml

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

更多推荐

本文发布于:2023-07-24 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   模块   工作   working   module

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!