IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> ModuleNotFoundError & ImportError -> 正文阅读

[Python知识库]ModuleNotFoundError & ImportError

提示:文章尚未写完,感觉一开始的代码分类没有做好,有所遗漏,所以这一篇就停在这里,重开一篇:点击查看。

目录


前言

提示:本文要记录的大概内容:

import statement 并不复杂,无非就是几种格式而已。导入 Python 标准库,只要合乎格式就可以了。但是,导入自定义 module 却常常出错。尤其是在 Anaconda (Spyder)上写成的代码,换到 PyCharm 就不行了,或者换成 IDLE 用命令行执行也是报错。

深入了解这个问题,着实花了我3天时间。故此,记录如下。


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

1 import statement

1.1. 官方规范


这是官方规范

2 常见格式

2.1. 导入标准库

# 以标准库举例如下
import tkinter    # 导入标准库最常用
import turtle as t    # t 作为标识符可直接调用,代表 turtle module
from tkinter.ttk import Combobox as Cx    # Cx 作为标识符可直接调用,代表 tkinter.ttk.Combobox
from tkinter import *    # 通配符形式的导入仅在模块层级上被允许。尝试在类或函数定义中使用它将引发 SyntaxError。

# 查看标识符的类型
type(tkinter)
module
type(tkinter.ttk)
module
type(tkinter.ttk.Combobox)
type    # class
type(tkinter.ttk.Combobox.xview)    # 后面会看到这个不可导入
function
type(tkinter.mainloop)    # 后面会看到这个可导入
function

# 试探 import module (as ...)
import tkinter.ttk
import tkinter.ttk.Combobox
ModuleNotFoundError: No module named 'tkinter.ttk.Combobox'; 'tkinter.ttk' is not a package
import tkinter.ttk.Combobox.xview
ModuleNotFoundError: No module named 'tkinter.ttk.Combobox'; 'tkinter.ttk' is not a package
import tkinter.mainloop
ModuleNotFoundError: No module named 'tkinter.mainloop'
# 结论1:
# import module, OK
# import module.sub-module, OK
# import pkg.module, OK
# import class, NG
# import module.class, NG
# import functon, NG
# import module.function, NG
# import module.class.function, NG

# 试探 from ... import (as ...)
from tkinter import ttk
from tkinter import *    # 类名、常量名、函数名
from tkinter.ttk import Combobox
from tkinter.ttk import *
from tkinter.ttk.Combobox import xview
ModuleNotFoundError: No module named 'tkinter.ttk.Combobox'; 'tkinter.ttk' is not a package
from tkinter.ttk.Combobox import *    
ModuleNotFoundError: No module named 'tkinter.ttk.Combobox'; 'tkinter.ttk' is not a package
from tkinter import mainloop
# 结论2:
# from pkg.module import ..., OK
# from module import ..., OK
# from module.sub-module import ..., OK
# from module imort class, OK    #  连常量都可以导入
# from module import function, OK
# from module.sub-module import class, OK
# from module.sub-module import function, OK
# from module.sub-module.class import function, NG    # 我到这里已崩溃~~~

2.2. 导入自定义库

其实,这次笔记的核心是一定要设置 PYTHONPATH 变量,所以先查看一下两个解释器的环境变量。

2.2.1. 查看 Spyder 的 PYTHONPATH 变量

空的,容易说明问题。
在这里插入图片描述

2.2.2. 查看 Spyder 的 sys.path

import sys

for path in sys.path:
    print(path)
print(len(sys.path))

在这里插入图片描述

2.2.3. 查看 PyCharm 的 PYTHONPATH 变量

其实,重要的不是查看,而是设置。如下图,很重要。
在这里插入图片描述

2.2.4. 查看 PyCharm 的 sys.path

在这里插入图片描述
尽管 PyCharm 调用 Anaconda 的包,但二者的环境变量仍然各自独立。

2.3. 绝对导入(absolute import)与相对导入(relative import)

  1. 看起来很麻烦的两个概念,但纠结这个毫无意义。如果包的 __ init__.py 是空文件,那么就不要用 . 起头的相对导入。除非你懂得如何设置 __ init__.py。
  2. 相对导入必须设置好才能用,否则提示 SyntaxError
  3. 相对导入用 . 起头,使用前缀点号,而且只有一种形式:from <> import <>
  4. 绝对导入可用 import <> 或 from <> import <> 语法

2.4. 在未设置 PYTHONPATH 变量的前提下,不同 IDLE 的表现

2.4.1. 创建层级目录

在这里插入图片描述
3个 modules 的代码如下:

class Class:
    """module2.py"""

    def __init__(self):
        print("module2.Class")

    def test_func1(self):
        print("method module2.Class.test_func1")

    def test_func2(self):
        print("method module2.Class.test_func2")

    def test_func3(self):
        print("method module2.Class.test_func3")


class Class:
    """module1.py"""

    def __init__(self):
        print("module1.Class")

    def test_func1(self):
        print("method module1.Class.test_func1")

    def test_func2(self):
        print("method module1.Class.test_func2")

    def test_func3(self):
        print("method module1.Class.test_func3")


class Class:
    """module0.py"""

    def __init__(self):
        print("module0.Class")

    def test_func1(self):
        print("method module0.Class.test_func1")

    def test_func2(self):
        print("method module0.Class.test_func2")

    def test_func3(self):
        print("method module0.Class.test_func3")

2.4.2. Spyder 的表现

import sys

print(len(sys.path))
12    # 未设置 PYTHONPATH 变量

# 当前模块的绝对路径:C:/Users/Administrator/pkg/import_test.py
# 导入模块的绝对路径:C:/Users/Administrator/pkg/module0.py
# 导入模块的绝对路径:C:/Users/Administrator/pkg/pkg1/module1.py
# 导入模块的绝对路径:C:/Users/Administrator/pkg/pkg1/pkg2/module2.py

# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg.module0.Class'; 'pkg.module0' is not a package
import pkg.module0    # OK
import module0    # ModuleNotFoundError: No module named 'module0'

from pkg.module0 import Class    # OK
from pkg import module0    # OK
from module0 import Class    # ModuleNotFoundError: No module named 'module0'


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.module1.Class'; 'pkg.pkg1.module1' is not a package
import pkg.pkg1.module1    # OK
import pkg.pkg1    # OK

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1    # ModuleNotFoundError: No module named 'pkg1'

from pkg.pkg1.module1 import Class    # OK
from pkg.pkg1 import module1    # OK
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # OK
import pkg.pkg1    # OK
import pkg.pkg1.pkg2    # OK
import pkg.pkg1.pkg2.module2    # OK
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.pkg2.module2.Class'; 'pkg.pkg1.pkg2.module2' is not a package

import pkg1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg1'

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ImportError: cannot import name 'module2' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)
from pkg import module1    # ImportError: cannot import name 'module1' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)

from pkg.pkg1.pkg2 import module2    # OK
from pkg.pkg1 import module1    # OK
from pkg import module0    # OK

from pkg.pkg1.pkg2.module2 import Class    # OK
from pkg.pkg1.module1 import Class    # OK
from pkg.module0 import Class    # OK

2.4.3. PyCharm 的表现

红色波浪线提示 SyntaxError
红色波浪线提示 SyntaxError
那么就逐行试验一下能不能导入:

import sys

print(len(sys.path))
10

# 当前模块的绝对路径:C:/Users/Administrator/pkg/import_test.py
# 导入模块的绝对路径:C:/Users/Administrator/pkg/module0.py
# 导入模块的绝对路径:C:/Users/Administrator/pkg/pkg1/module1.py
# 导入模块的绝对路径:C:/Users/Administrator/pkg/pkg1/pkg2/module2.py

# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg.module0.Class'; 'pkg.module0' is not a package
import pkg.module0    # ModuleNotFoundError: No module named 'pkg'
import module0    # OK

from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'
from module0 import Class    # OK


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.module1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1.module1.Class'; 'pkg1.module1' is not a package
import pkg1.module1    # OK
import pkg1    # OK

from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg'

import pkg1    # OK
import pkg1.pkg2    # OK
import pkg1.pkg2.module2    # OK

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg import module1    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'

两相对比,在 Spyder 里可以导入的语句, 与在 PyCharm 里可以导入的语句,没有任何重叠。当然,前提是都未设置 PYTHONPATH 变量。

2.4.4. Python IDLE 3.8 的表现

在这里插入图片描述
Python IDLE 里执行的结果居然与 Spyder 完全相同,惊了我!

# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg.module0.Class'; 'pkg.module0' is not a package
import pkg.module0    # OK
import module0    # ModuleNotFoundError: No module named 'module0'

from pkg.module0 import Class    # OK
from pkg import module0    # OK
from module0 import Class    # ModuleNotFoundError: No module named 'module0'


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.module1.Class'; 'pkg.pkg1.module1' is not a package
import pkg.pkg1.module1    # OK
import pkg.pkg1    # OK

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1    # ModuleNotFoundError: No module named 'pkg1'

from pkg.pkg1.module1 import Class    # OK
from pkg.pkg1 import module1    # OK
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # OK
import pkg.pkg1    # OK
import pkg.pkg1.pkg2    # OK
import pkg.pkg1.pkg2.module2    # OK
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.pkg2.module2.Class'; 'pkg.pkg1.pkg2.module2' is not a package

import pkg1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg1'

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ImportError: cannot import name 'module2' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)
from pkg import module1    # ImportError: cannot import name 'module1' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)

from pkg.pkg1.pkg2 import module2    # OK
from pkg.pkg1 import module1    # OK
from pkg import module0    # OK

from pkg.pkg1.pkg2.module2 import Class    # OK
from pkg.pkg1.module1 import Class    # OK
from pkg.module0 import Class    # OK

2.5. 为三个 IDLE 的 PYTHONPATH 变量都添加 pkg 目录,并查看其导入语句

2.5.1. 为三个 IDLE 的 PYTHONPATH 变量都添加 pkg 目录

2.5.1.1. 为 Spyder 的 PYTHONPATH 变量添加 pkg 目录

在这里插入图片描述

2.5.1.2. 为 PyCharm 的 PYTHONPATH 变量添加 pkg 目录

在这里插入图片描述
在这里插入图片描述

2.5.1.3. 为 Python IDLE 的 PYTHONPATH 变量添加 pkg 目录

在这里插入图片描述
尝试用另外一种方法添加目录:
在这里插入图片描述

2.5.2. 查看三个 IDLE 的导入语句

2.5.2.1. 查看 Spyder 的导入语句

# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg.module0.Class'; 'pkg.module0' is not a package
import pkg.module0    # OK
import module0    # OK

from pkg.module0 import Class    # OK
from pkg import module0    # OK
from module0 import Class    # OK


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.module1.Class'; 'pkg.pkg1.module1' is not a package
import pkg.pkg1.module1    # OK
import pkg.pkg1    # OK

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # OK
import pkg1    # OK

from pkg.pkg1.module1 import Class    # OK
from pkg.pkg1 import module1    # OK
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # OK
import pkg.pkg1    # OK
import pkg.pkg1.pkg2    # OK
import pkg.pkg1.pkg2.module2    # OK
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.pkg2.module2.Class'; 'pkg.pkg1.pkg2.module2' is not a package

import pkg1    # OK
import pkg1.pkg2    # OK
import pkg1.pkg2.module2    # OK

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ImportError: cannot import name 'module2' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)
from pkg import module1    # ImportError: cannot import name 'module1' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)

from pkg.pkg1.pkg2 import module2    # OK
from pkg.pkg1 import module1    # OK
from pkg import module0    # OK

from pkg.pkg1.pkg2.module2 import Class    # OK
from pkg.pkg1.module1 import Class    # OK
from pkg.module0 import Class    # OK

2.5.2.2. 查看 PyCharm 的导入语句

在这里插入图片描述

2.5.2.3. 查看 Python IDLE 的导入语句

在这里插入图片描述

2.6. 为三个 IDLE 的 PYTHONPATH 变量都添加 pkg.pkg1 目录,并查看其导入语句

2.6.1. 为三个 IDLE 的 PYTHONPATH 变量都添加 pkg.pkg1 目录

2.6.1.1. 为 Spyder 的 PYTHONPATH 变量添加 pkg.pkg1 目录

在这里插入图片描述

2.6.1.2. 为 PyCharm 的 PYTHONPATH 变量添加 pkg.pkg1 目录

在这里插入图片描述

2.6.1.3. 为 Python IDLE 的 PYTHONPATH 变量添加 pkg.pkg1 目录

在这里插入图片描述

2.6.2. 查看三个 IDLE 的导入语句

2.6.2.1. 查看 Spyder 的导入语句

2.6.2.1.1.脚本文件在 pkg 目录
# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg.module0.Class'; 'pkg.module0' is not a package
import pkg.module0    # OK
import module0    # OK

from pkg.module0 import Class    # OK
from pkg import module0    # OK
from module0 import Class    # OK


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.module1.Class'; 'pkg.pkg1.module1' is not a package
import pkg.pkg1.module1    # OK
import pkg.pkg1    # OK

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # OK
import pkg1    # OK

from pkg.pkg1.module1 import Class    # OK
from pkg.pkg1 import module1    # OK
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # OK
import pkg.pkg1    # OK
import pkg.pkg1.pkg2    # OK
import pkg.pkg1.pkg2.module2    # OK
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.pkg2.module2.Class'; 'pkg.pkg1.pkg2.module2' is not a package

import pkg1    # OK
import pkg1.pkg2    # OK
import pkg1.pkg2.module2    # OK

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ImportError: cannot import name 'module2' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)
from pkg import module1    # ImportError: cannot import name 'module1' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)

from pkg.pkg1.pkg2 import module2    # OK
from pkg.pkg1 import module1    # OK
from pkg import module0    # OK

from pkg.pkg1.pkg2.module2 import Class    # OK
from pkg.pkg1.module1 import Class    # OK
from pkg.module0 import Class    # OK

经对比,可导入的语句及错误提示与 pkg 目录加入 PYTHONPATH 变量时相同。

2.6.2.1.2.脚本文件在 pkg.pkg1 目录
# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg.module0.Class'; 'pkg.module0' is not a package
import pkg.module0    # OK
import module0    # OK

from pkg.module0 import Class    # OK
from pkg import module0    # OK
from module0 import Class    # OK


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.module1.Class'; 'pkg.pkg1.module1' is not a package
import pkg.pkg1.module1    # OK
import pkg.pkg1    # OK

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # OK
import pkg1    # OK

from pkg.pkg1.module1 import Class    # OK
from pkg.pkg1 import module1    # OK
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # OK
import pkg.pkg1    # OK
import pkg.pkg1.pkg2    # OK
import pkg.pkg1.pkg2.module2    # OK
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.pkg2.module2.Class'; 'pkg.pkg1.pkg2.module2' is not a package

import pkg1    # OK
import pkg1.pkg2    # OK
import pkg1.pkg2.module2    # OK

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ImportError: cannot import name 'module2' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)
from pkg import module1    # ImportError: cannot import name 'module1' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)

from pkg.pkg1.pkg2 import module2    # OK
from pkg.pkg1 import module1    # OK
from pkg import module0    # OK

from pkg.pkg1.pkg2.module2 import Class    # OK
from pkg.pkg1.module1 import Class    # OK
from pkg.module0 import Class    # OK

2.6.2.1.3. 总结

经对比,可导入的语句及错误提示与 pkg 目录加入 PYTHONPATH 变量时相同,当然也就与脚本文件在 pkg 目录或 pkg1 目录无关。

2.6.2.2. 查看 PyCharm 的导入语句

2.6.2.2.1.脚本文件在 pkg.pkg1 目录
# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.module0    # ModuleNotFoundError: No module named 'pkg'
import module0    # OK

from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'
from module0 import Class    # OK


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.module1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1.module1.Class'; 'pkg1.module1' is not a package
import pkg1.module1    # OK
import pkg1    # OK

from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg'

import pkg1    # OK
import pkg1.pkg2    # OK
import pkg1.pkg2.module2    # OK

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg import module1    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'

2.6.2.2.1.脚本文件在 pkg.pkg1 目录

这个就厉害了!连一个可导入的语句都没有!而且全部红色波浪线!

# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.module0    # ModuleNotFoundError: No module named 'pkg'
import module0    # ModuleNotFoundError: No module named 'module0'

from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'
from module0 import Class    # ModuleNotFoundError: No module named 'module0'


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.module1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1    # ModuleNotFoundError: No module named 'pkg1'

from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # ModuleNotFoundError: No module named 'module2'
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg'

import pkg1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg1'

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg import module1    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'

2.6.2.2.3. 总结

经对比,可导入的语句及错误提示与 pkg 目录加入 PYTHONPATH 变量时相同,当然也就与脚本文件在 pkg 目录或 pkg1 目录无关。

2.6.2.3. 查看 Python IDLE 的导入语句

这个我就忽略了,烦了。

2.7. 为三个 IDLE 的 PYTHONPATH 变量都添加 pkg.pkg1.pkg2 目录,并查看其导入语句

2.7.1. 为三个 IDLE 的 PYTHONPATH 变量都添加 pkg.pkg1.pkg2 目录

2.7.1.1. 为 Spyder 的 PYTHONPATH 变量添加 pkg.pkg1.pkg2 目录

在这里插入图片描述

2.7.1.2. 为 PyCharm 的 PYTHONPATH 变量添加 pkg.pkg1.pkg2 目录

在这里插入图片描述

2.7.1.3. 为 Python IDLE 的 PYTHONPATH 变量添加 pkg.pkg1.pkg2 目录

在这里插入图片描述

2.7.2. 查看三个 IDLE 的导入语句

2.7.2.1. 查看 Spyder 的导入语句

2.7.2.1.1. 脚本文件在 pkg 目录
# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.module0    # OK
import module0    # ModuleNotFoundError: No module named 'module0'

from pkg.module0 import Class    # OK
from pkg import module0    # OK
from module0 import Class    # ModuleNotFoundError: No module named 'module0'


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.module1.Class'; 'pkg.pkg1.module1' is not a package
import pkg.pkg1.module1    # OK
import pkg.pkg1    # OK

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1    # ModuleNotFoundError: No module named 'pkg1'

from pkg.pkg1.module1 import Class    # OK
from pkg.pkg1 import module1    # OK
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # OK
import .module2    # SyntaxError: invalid syntax
import ..module2    # SyntaxError: invalid syntax
import ...module2    # SyntaxError: invalid syntax

from .import module2    # ImportError: attempted relative import with no known parent package
from ..import module2    # ImportError: attempted relative import with no known parent package
from ...import module2    # ImportError: attempted relative import with no known parent package

import pkg    # OK
import pkg.pkg1    # OK
import pkg.pkg1.pkg2    # OK
import pkg.pkg1.pkg2.module2    # OK
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg.pkg1.pkg2.module2.Class'; 'pkg.pkg1.pkg2.module2' is not a package

import pkg1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg1'

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ImportError: cannot import name 'module2' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)
from pkg import module1    # ImportError: cannot import name 'module1' from 'pkg' (C:\Users\Administrator\pkg\__init__.py)

from pkg.pkg1.pkg2 import module2    # OK
from pkg.pkg1 import module1    # OK
from pkg import module0    # OK

from pkg.pkg1.pkg2.module2 import Class    # OK
from pkg.pkg1.module1 import Class    # OK
from pkg.module0 import Class    # OK

2.7.2.1.2. 脚本文件在 pkg.pkg1 目录
# 同上:错误提示略有不同,但能导入的语句都相同
2.7.2.1.3. 脚本文件在 pkg.pkg1.pkg2 目录
# 同上:错误提示略有不同,但能导入的语句都相同
2.7.2.1.4. 总结

无论脚本文件在 pkg or pkg1 or pkg2,能导入的语句相同,但错误提示略有不同

2.7.2.2. 查看 PyCharm 的导入语句

2.7.2.2.1. 脚本文件在 pkg 目录
# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.module0    # ModuleNotFoundError: No module named 'pkg'
import module0    # OK

from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'
from module0 import Class    # OK


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax    # 相对导入不能用这种格式

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.module1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1.module1.Class'; 'pkg1.module1' is not a package
import pkg1.module1    # OK
import pkg1    # OK

from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # OK
import .module2    # SyntaxError: invalid syntax    # 相对导入不能用这种格式
import ..module2    # SyntaxError: invalid syntax    # 相对导入不能用这种格式
import ...module2    # SyntaxError: invalid syntax    # 相对导入不能用这种格式

from . import module2    # ImportError: attempted relative import with no known parent package
from .. import module2    # ImportError: attempted relative import with no known parent package
from ... import module2    # ImportError: attempted relative import with no known parent package

import pkg    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg'

import pkg1    # OK
import pkg1.pkg2    # OK
import pkg1.pkg2.module2    # OK

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg import module1    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'

2.7.2.2.2. 脚本文件在 pkg.pkg1 目录
# 导入 module0
from . import module0    # ImportError: attempted relative import with no known parent package
from .. import module0    # ImportError: attempted relative import with no known parent package
from .module0 import Class    # ImportError: attempted relative import with no known parent package

import pkg.module0.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.module0    # ModuleNotFoundError: No module named 'pkg'
import module0    # ModuleNotFoundError: No module named 'module0'

from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'
from module0 import Class    # ModuleNotFoundError: No module named 'module0'


# 导入 module1
from . import module1    # ImportError: attempted relative import with no known parent package
from .. import module1    # ImportError: attempted relative import with no known parent package
from ... import module1    # ImportError: attempted relative import with no known parent package

from .module1 import Class    # ImportError: attempted relative import with no known parent package
import .module1    # SyntaxError: invalid syntax    # 相对导入不能用这种格式

import pkg.pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.module1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'

import pkg1.module1.Class    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.module1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1    # ModuleNotFoundError: No module named 'pkg1'

from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import pkg1.module1    # SyntaxError: invalid syntax


# 导入 module2
import module2    # OK
import .module2    # SyntaxError: invalid syntax    # 相对导入不能用这种格式
import ..module2    # SyntaxError: invalid syntax    # 相对导入不能用这种格式
import ...module2    # SyntaxError: invalid syntax    # 相对导入不能用这种格式

from . import module2    # ImportError: attempted relative import with no known parent package
from .. import module2    # ImportError: attempted relative import with no known parent package
from ... import module2    # ImportError: attempted relative import with no known parent package

import pkg    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg'
import pkg.pkg1.pkg2.module2.Class    # ModuleNotFoundError: No module named 'pkg'

import pkg1    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2    # ModuleNotFoundError: No module named 'pkg1'
import pkg1.pkg2.module2    # ModuleNotFoundError: No module named 'pkg1'

from pkg import module2.Class    # SyntaxError: invalid syntax
from pkg import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg import module1    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2 import module2    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1 import module1    # ModuleNotFoundError: No module named 'pkg'
from pkg import module0    # ModuleNotFoundError: No module named 'pkg'

from pkg.pkg1.pkg2.module2 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.pkg1.module1 import Class    # ModuleNotFoundError: No module named 'pkg'
from pkg.module0 import Class    # ModuleNotFoundError: No module named 'pkg'

2.7.2.2.3. 脚本文件在 pkg.pkg1.pkg2 目录
2.7.2.2.4. 总结

2.7.2.3. 查看 Python IDLE 的导入语句

(略)


3 总结

  1. import statement 并不像看起来那么人畜无害,自求多福吧!
  2. 我只能用设置 PYTHONPATH 变量和递进式 self-debugging 啦!
  3. 不推荐相对导入。
  4. 只要换了 IDLE,就要先查看 PYTHONPATH 变量里有没有脚本引用的 module 所在的路径,没有就用 sys.path.append() 添加。
  5. PyCharm 使用 Anaconda 的系统环境也很普遍了,但二者的 PYTHONPATH 变量仍然各自独立,需要各自添加。
  6. 添加 PYTHONPATH 变量推荐直达子包。
  7. 其实,善用 site-package 也许才是真正的王道,只是我还不熟悉的情况下,就发生了这么多事情**

4 系统环境

  1. win7-64bit
  2. Spyder 4.2.5 ( Python 3.7.10)
  3. PyCharm 2020.3
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-04-22 18:32:27  更:2022-04-22 18:33:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 17:20:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码