本文主要针对的自己写的包无法正常import的情况,如果是第三方包的话正常来说没有问题。
第三方包
主要考虑没有安装对应的版本以及包名写错了等奇葩情况,具体可参考ModuleNotFoundError: No module named ‘xxx’可能的解决方案大全
自建的包
如果是自己的包,可按照下面顺序来检查:
- 检查
__init__.py
文件是否存在:如果这个包是你自己写的,不妨检查一下待导入的包的根目录是否存在一个__init__.py文件,没有的话建一个空的__init__.py文件即可 (注意:不光要检查待导入的包根目录是否存在__init__.py文件,还需要检查当前程序所在文件夹是否存在这个文件) - 自己建的包不在PYTHONPATH目录下:这样通用的解决方案就是将对应包的路径添加到对应目录即可。
sys.path.append({这里改成model_path})
#比如:sys.path.append('~/abcd/Modeling') 自己建的包在Modeling文件夹下
- 第二步的代码和import的顺序:第二步的代码的执行顺序必须要在import之前,一个容易忽视的情况就是在
if __name__ == '__main__'
中写了对应的sys.path.append
,但是由于import写在文件顶部的话会执行在语句之前,因为没有生效。
我的博客园:https://www.cnblogs.com/swx123
我的github(代码一般都放在这里):https://github.com/578223592
- ModuleNotFoundError 解决方案 方案 python modulemodulenotfounderror解决方案 方案module modulenotfounderror解决方案 方案python modulenotfounderror python3 python module modulenotfounderror encodings python module modulenotfounderror脚本python module modulenotfounderror经验python module modulenotfounderror python module flask modulenotfounderror htmltestrunner python module modulenotfounderror解决方案 方案 大全 modulenotfounderror模块 解决方案 错误