目录提示:文章尚未写完,感觉一开始的代码分类没有做好,有所遗漏,所以这一篇就停在这里,重开一篇:点击查看。
- 前言
- 1 import statement
- 1.1. 官方规范
- 2 常见格式
- 2.1. 导入标准库
- 2.2. 导入自定义库
- 2.2.1. 查看 Spyder 的 PYTHONPATH 变量
- 2.2.2. 查看 Spyder 的 sys.path
- 2.2.3. 查看 PyCharm 的 PYTHONPATH 变量
- 2.2.4. 查看 PyCharm 的 sys.path
- 2.3. 绝对导入(absolute import)与相对导入(relative import)
- 2.4. 在未设置 PYTHONPATH 变量的前提下,不同 IDLE 的表现
- 2.4.1. 创建层级目录
- 2.4.2. Spyder 的表现
- 2.4.3. PyCharm 的表现
- 2.4.4. Python IDLE 3.8 的表现
- 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 的导入语句
- 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 目录
- 2.6.2.1.2.脚本文件在 pkg.pkg1 目录
- 2.6.2.1.3. 总结
- 2.6.2.2. 查看 PyCharm 的导入语句
- 2.6.2.2.1.脚本文件在 pkg.pkg1 目录
- 2.6.2.2.1.脚本文件在 pkg.pkg1 目录
- 2.6.2.2.3. 总结
- 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 目录
- 2.7.2.1.2. 脚本文件在 pkg.pkg1 目录
- 2.7.2.1.3. 脚本文件在 pkg.pkg1.pkg2 目录
- 2.7.2.1.4. 总结
- 2.7.2.2. 查看 PyCharm 的导入语句
- 2.7.2.2.1. 脚本文件在 pkg 目录
- 2.7.2.2.2. 脚本文件在 pkg.pkg1 目录
- 2.7.2.2.3. 脚本文件在 pkg.pkg1.pkg2 目录
- 2.7.2.2.4. 总结
- 2.7.2.3. 查看 Python IDLE 的导入语句
- 3 总结
- 4 系统环境
前言
提示:本文要记录的大概内容:
import statement 并不复杂,无非就是几种格式而已。导入 Python 标准库,只要合乎格式就可以了。但是,导入自定义 module 却常常出错。尤其是在 Anaconda (Spyder)上写成的代码,换到 PyCharm 就不行了,或者换成 IDLE 用命令行执行也是报错。
深入了解这个问题,着实花了我3天时间。故此,记录如下。
提示:以下是本篇文章正文内容,下面案例可供参考
这是官方规范。
# 以标准库举例如下
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 变量空的,容易说明问题。
import sys
for path in sys.path:
print(path)
print(len(sys.path))
2.2.3. 查看 PyCharm 的 PYTHONPATH 变量
其实,重要的不是查看,而是设置。如下图,很重要。
尽管 PyCharm 调用 Anaconda 的包,但二者的环境变量仍然各自独立。
- 看起来很麻烦的两个概念,但纠结这个毫无意义。如果包的 __ init__.py 是空文件,那么就不要用 . 起头的相对导入。除非你懂得如何设置 __ init__.py。
- 相对导入必须设置好才能用,否则提示 SyntaxError
- 相对导入用 . 起头,使用前缀点号,而且只有一种形式:from <> import <>
- 绝对导入可用 import <> 或 from <> import <> 语法
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
那么就逐行试验一下能不能导入:
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 目录
尝试用另外一种方法添加目录:
# 导入 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 总结
- import statement 并不像看起来那么人畜无害,自求多福吧!
- 我只能用设置 PYTHONPATH 变量和递进式 self-debugging 啦!
- 不推荐相对导入。
- 只要换了 IDLE,就要先查看 PYTHONPATH 变量里有没有脚本引用的 module 所在的路径,没有就用 sys.path.append() 添加。
- PyCharm 使用 Anaconda 的系统环境也很普遍了,但二者的 PYTHONPATH 变量仍然各自独立,需要各自添加。
- 添加 PYTHONPATH 变量推荐直达子包。
- 其实,善用 site-package 也许才是真正的王道,只是我还不熟悉的情况下,就发生了这么多事情**
- win7-64bit
- Spyder 4.2.5 ( Python 3.7.10)
- PyCharm 2020.3
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)