Python 导入模块不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45432471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:01:40  来源:igfitidea点击:

Importing module not working

pythondjangopython-3.xmodule

提问by Peni

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)

我有一个 django(但我认为它在这里也不相关)项目,我尝试在其中添加我以前做过的脚本。所以我把它放在我的项目的一个子目录中,我有这个结构(我知道它现在有点乱,但它不会保持原样)

enter image description here

enter image description here

From views.pyi 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_pageand though, every subdirectory

views.py我想导入main.py(特别是函数excelToXml)。在互联网上搜索后,我发现我在 views.py 中复制的代码。如果我undestood它的权利将其添加到变量$ PATH的目录的父first_page虽然,每一个子目录

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.

我还在ExcelToXML目录中创建了一个文件__init.py__,这个文件是空的。

However even I did all that i still get this error when i run the django server

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

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'

文件“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 didn't find any solution that I could understand on internet so I really don't know how to solve this

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

采纳答案by Cédric Julien

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.

因为ExcelToXML 在first_page 模块下,所以被视为first_page 的子模块。

回答by Rohith

Check this for details on what is init.py file What is __init__.py for?

检查此以获取有关什么是init.py 文件的详细信息__init__.py 的用途是什么?

The init.py file should be present in every directory and sub directories whose classes should be made visible for import. In your case, the I am suspecting that the parent directory does not have the init.py file. Add the file in the parent directory and import it as follows

初始化.py文件应该是存在于每个目录和子目录,其类进口应可见。在您的情况下,我怀疑父目录没有init.py 文件。在父目录中添加文件并导入如下

import first_page.ExcelToXML

导入 first_page.ExcelToXML

回答by Zcode

Okay hello, the solution you found is (I think) a mess, you should read the official documentation about it https://docs.python.org/3.6/tutorial/modules.html#packages.
In short just add from .idea.main import excelToXml. If it does not work, rename .ideafolder to idea(without the dot) and add in your views.pythis line :from idea.main import excelToXml

好的,您好,您找到的解决方案(我认为)一团糟,您应该阅读有关它的官方文档https://docs.python.org/3.6/tutorial/modules.html#packages
简而言之,只需添加from .idea.main import excelToXml. 如果它不起作用,请将.idea文件夹重命名为idea(不带点)并添加您的views.py这一行:from idea.main import excelToXml