【ModuleNotFoundError & ImportError】之导入语句全列举

发布时间:2023-06-22 10:00

目录

  • 前言
  • 1 呈现所建项目
  • 2 导入语句列举
    • 2.1. Spyder 导入测试
    • 3.2. PyCharm 导入测试
  • 总结


前言

提示:这里可以添加本文要记录的大概内容:

本文是作为对之前发表的《【ModuleNotFoundError & ImportError】之失败的尝试》的补充。

采用之前总结的对 import statement、sys.path 等内容的总结,在此尝试列举了全部的导入语句,并在 Spyder 与 PyCharm 里逐一尝试。结果是:能在 Spyder 里运行的导入语句,也可以在 PyCharm 里运行。

系统环境:
Win7-64bit
Spyder 4.2.5 ( Python 3.7.10)
PyCharm 2021.2.4(Community Ed.,调用 conda 环境)


提示:以下是本篇文章正文内容,下面案例可供参考

1 呈现所建项目

\"【ModuleNotFoundError

2 导入语句列举

\"【ModuleNotFoundError

2.1. Spyder 导入测试

# package 测试
import sr
import 导入测试.sr    # ModuleNotFoundError: No module named \'导入测试\'

import pkg    # ModuleNotFoundError: No module named \'pkg\'
import sr.pkg
import 导入测试.sr.pkg    # ModuleNotFoundError: No module named \'导入测试\'

import pkg1    # ModuleNotFoundError: No module named \'pkg1\'
import pkg.pkg1    # ModuleNotFoundError: No module named \'pkg\'
import sr.pkg.pkg1
import 导入测试.sr.pkg.pkg1    # ModuleNotFoundError: No module named \'导入测试\'

import pkg2    # ModuleNotFoundError: No module named \'pkg2\'
import pkg1.pkg2    # ModuleNotFoundError: No module named \'pkg1\'
import pkg.pkg1.pkg2    # ModuleNotFoundError: No module named \'pkg\'
import sr.pkg.pkg1.pkg2
import 导入测试.sr.pkg.pkg1.pkg2    # ModuleNotFoundError: No module named \'导入测试\'

from 导入测试 import sr    # ModuleNotFoundError: No module named \'导入测试\'

from sr import pkg
from 导入测试.sr import pkg    # ModuleNotFoundError: No module named \'导入测试\'

from pkg import pkg1    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg import pkg1
from 导入测试.sr.pkg import pkg1    # ModuleNotFoundError: No module named \'导入测试\'

from pkg1 import pkg2    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1 import pkg2    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1 import pkg2
from 导入测试.sr.pkg.pkg1 import pkg2    # ModuleNotFoundError: No module named \'导入测试\'

# module 测试
import module    # ModuleNotFoundError: No module named \'module\'
import sr.module
import 导入测试.sr.module    # ModuleNotFoundError: No module named \'导入测试\'

import module0    # ModuleNotFoundError: No module named \'module0\'
import pkg.module0    # ModuleNotFoundError: No module named \'pkg\'
import sr.pkg.module0
import 导入测试.sr.pkg.module0    # ModuleNotFoundError: No module named \'导入测试\'

import module1    # ModuleNotFoundError: No module named \'module1\'
import pkg1.module1    # ModuleNotFoundError: No module named \'pkg1\'
import pkg.pkg1.module1    # ModuleNotFoundError: No module named \'pkg\'
import sr.pkg.pkg1.module1
import 导入测试.sr.pkg.pkg1.module1    # ModuleNotFoundError: No module named \'导入测试\'

import module2    # ModuleNotFoundError: No module named \'module2\'
import pkg2.module2    # ModuleNotFoundError: No module named \'pkg2\'
import pkg1.pkg2.module2    # ModuleNotFoundError: No module named \'pkg1\'
import pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named \'pkg\'
import sr.pkg.pkg1.pkg2.module2
import 导入测试.sr.pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named \'导入测试\'

from sr import module
from 导入测试.sr import module    # ModuleNotFoundError: No module named \'导入测试\'

from pkg import module0    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg import module0
from 导入测试.sr.pkg import module0    # ModuleNotFoundError: No module named \'导入测试\'

from pkg1 import module1    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1 import module1
from 导入测试.sr.pkg.pkg1 import module1    # ModuleNotFoundError: No module named \'导入测试\'

from pkg2 import module2    # ModuleNotFoundError: No module named \'pkg2\'
from pkg1.pkg2 import module2    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1.pkg2 import module2
from 导入测试.sr.pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named \'导入测试\'

# class 测试
from sr.module import Class
from 导入测试.sr.module import Class    # ModuleNotFoundError: No module named \'导入测试\'

from pkg.module0 import Class    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.module0 import Class
from 导入测试.sr.pkg.module0 import Class    # ModuleNotFoundError: No module named \'导入测试\'

from pkg1.module1 import Class    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1.module1 import Class
from 导入测试.sr.pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named \'导入测试\'

from pkg2.module2 import Class    # ModuleNotFoundError: No module named \'pkg2\'
from pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1.pkg2.module2 import Class
from 导入测试.sr.pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named \'导入测试\'

# function 测试
from sr.module import func
from 导入测试.sr.module import func    # ModuleNotFoundError: No module named \'导入测试\'

from pkg.module0 import func    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.module0 import func
from 导入测试.sr.pkg.module0 import func    # ModuleNotFoundError: No module named \'导入测试\'

from pkg1.module1 import func    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1.module1 import func    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1.module1 import func
from 导入测试.sr.pkg.pkg1.module1 import func    # ModuleNotFoundError: No module named \'导入测试\'

from pkg2.module2 import func    # ModuleNotFoundError: No module named \'pkg2\'
from pkg1.pkg2.module2 import func    # ModuleNotFoundError: No module named \'pkg1\'
from pkg.pkg1.pkg2.module2 import func    # ModuleNotFoundError: No module named \'pkg\'
from sr.pkg.pkg1.pkg2.module2 import func
from 导入测试.sr.pkg.pkg1.pkg2.module2 import func    # ModuleNotFoundError: No module named \'导入测试\'

3.2. PyCharm 导入测试

# package 测试
# import sr
# import 导入测试.sr    # ModuleNotFoundError: No module named \'导入测试\'

# import pkg    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# import sr.pkg
# import 导入测试.sr.pkg    # ModuleNotFoundError: No module named \'导入测试\'

# import pkg1    # ModuleNotFoundError: No module named \'pkg1\'
# import pkg.pkg1    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# import sr.pkg.pkg1
# import 导入测试.sr.pkg.pkg1    # ModuleNotFoundError: No module named \'导入测试\'

# import pkg2    # ModuleNotFoundError: No module named \'pkg2\'
# import pkg1.pkg2    # ModuleNotFoundError: No module named \'pkg1\'
# import pkg.pkg1.pkg2    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# import sr.pkg.pkg1.pkg2
# import 导入测试.sr.pkg.pkg1.pkg2    # ModuleNotFoundError: No module named \'导入测试\'

# from 导入测试 import sr    # ModuleNotFoundError: No module named \'导入测试\'

# from sr import pkg
# from 导入测试.sr import pkg    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg import pkg1    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg import pkg1
# from 导入测试.sr.pkg import pkg1    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg1 import pkg2    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1 import pkg2    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1 import pkg2
# from 导入测试.sr.pkg.pkg1 import pkg2    # ModuleNotFoundError: No module named \'导入测试\'

# module 测试
# import module    # PyCharm 可用    # ModuleNotFoundError: No module named \'module\'
# import sr.module
# import 导入测试.sr.module    # ModuleNotFoundError: No module named \'导入测试\'

# import module0    # ModuleNotFoundError: No module named \'module0\'
# import pkg.module0    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# import sr.pkg.module0
# import 导入测试.sr.pkg.module0    # ModuleNotFoundError: No module named \'导入测试\'

# import module1    # ModuleNotFoundError: No module named \'module1\'
# import pkg1.module1    # ModuleNotFoundError: No module named \'pkg1\'
# import pkg.pkg1.module1    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# import sr.pkg.pkg1.module1
# import 导入测试.sr.pkg.pkg1.module1    # ModuleNotFoundError: No module named \'导入测试\'

# import module2    # ModuleNotFoundError: No module named \'module2\'
# import pkg2.module2    # ModuleNotFoundError: No module named \'pkg2\'
# import pkg1.pkg2.module2    # ModuleNotFoundError: No module named \'pkg1\'
# import pkg.pkg1.pkg2.module2    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# import sr.pkg.pkg1.pkg2.module2
# import 导入测试.sr.pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named \'导入测试\'

# from sr import module
# from 导入测试.sr import module    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg import module0    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg import module0
# from 导入测试.sr.pkg import module0    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg1 import module1    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1 import module1    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1 import module1
# from 导入测试.sr.pkg.pkg1 import module1    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg2 import module2    # ModuleNotFoundError: No module named \'pkg2\'
# from pkg1.pkg2 import module2    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1.pkg2 import module2    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1.pkg2 import module2
# from 导入测试.sr.pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named \'导入测试\'

# class 测试
# from sr.module import Class
# from 导入测试.sr.module import Class    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg.module0 import Class    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.module0 import Class
# from 导入测试.sr.pkg.module0 import Class    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg1.module1 import Class    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1.module1 import Class    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1.module1 import Class
# from 导入测试.sr.pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg2.module2 import Class    # ModuleNotFoundError: No module named \'pkg2\'
# from pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1.pkg2.module2 import Class    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1.pkg2.module2 import Class
# from 导入测试.sr.pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named \'导入测试\'

# function 测试
# from sr.module import func
# from 导入测试.sr.module import func    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg.module0 import func    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.module0 import func
# from 导入测试.sr.pkg.module0 import func    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg1.module1 import func    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1.module1 import func    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1.module1 import func
# from 导入测试.sr.pkg.pkg1.module1 import func    # ModuleNotFoundError: No module named \'导入测试\'

# from pkg2.module2 import func    # ModuleNotFoundError: No module named \'pkg2\'
# from pkg1.pkg2.module2 import func    # ModuleNotFoundError: No module named \'pkg1\'
# from pkg.pkg1.pkg2.module2 import func    # PyCharm 可用    # ModuleNotFoundError: No module named \'pkg\'
# from sr.pkg.pkg1.pkg2.module2 import func
# from 导入测试.sr.pkg.pkg1.pkg2.module2 import func    # ModuleNotFoundError: No module named \'导入测试\'


总结

  1. 能在 Spyder 里运行的导入语句,也可以在 PyCharm 里运行。
  2. 不推荐相对导入
  3. 严格遵守 import statement 的定义
  4. 推荐 from … import …
  5. 养成良好的代码习惯是避免报错的王道!

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号