Python 导入自建包报错ModuleNotFoundError: No module named xxx

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 导入自建包报错ModuleNotFoundError: No module named xxx相关的知识,希望对你有一定的参考价值。

参考技术A 如图,三个模块的关系:模块A导入模块B,而模块B导入模块C
执行模块A时,会报错ModuleNotFoundError: No module named xxx,说找不到模块C。

于是就打开模块B查看,发现导入模块C是正常的,这就让人很头疼

模块B和模块C是同一个包,而模块A又是在另一个包。同个包内的模块B可以用相对路径找到模块C,不同包的模块A则不能。

方案一:项目根目录为起始路径导入包
在IDE中执行python程序,编译器会自动把当前项目的根目录加入到包查找路径中,可以理解为加到PYTHONPATH下,所以导入写法如下:

方案二:导包时,把目标模块的路径加入包查找路径中

推荐使用方案一,养成好习惯,从项目根目录开始导包。

eclipse中自建实体类存在但是导入的包报错cannot be resolved

摘自:https://blog.csdn.net/lazy_p/article/details/7452846 ‘Clean’ Your Eclipse Project: Go to Project > Clean in Eclipse [This seems to work for me] Refresh your project folder (right click on your project > refresh) Re-build your project Clean your builds (If using Ant or Maven – clean your builds) Recreate your project in Eclipse ‘Switch’ Workspace – then Switch back (Eg Change to Debug, then switch back to Java) ‘Switch’ Workspace – then Switch back (Eg Change to Debug, then switch back to Java) Remove and re-add your JRE: 1. Right Click on your project > properties 2. Click on the Libraries tab 3. Click on the JRE 4. Click remove, then OK 5. Repeat 1-3 again, but add the JRE again --------------------- 其中 project 中 clean功能解释如下: 由于eclipse的编译是基于时间戳的判断机制的。因此当你按build   all的时候有些eclipse认为时间戳没有改变的类不会被编译。因此你可以先clean一下再编译。这个时候eclipse会将所有的以前编译信息清除然后编译。   eclipse  --->project  ----->clean... 选项将工程中的.class文件删除,同时重新编译工程,类似于jbuild中的rebuild  --------------------- 本文来自 映月文章 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u010715393/article/details/42125505?utm_source=copy

以上是关于Python 导入自建包报错ModuleNotFoundError: No module named xxx的主要内容,如果未能解决你的问题,请参考以下文章

PyCharm导入tensorflow包报错的问题

Python 学习记录Pycharm导入第三方包报错问题……ValueError: check_hostname requires server_hostname

导入项目Java Resource包报错

loadrunner java 缺少必要的导入包报错

Android Studio项目导入aar包报错

导入包报错:Cannot resolve com.xxx