电子说
python2.7先搜索当前包目录,再搜索sys.path模块搜索路径。即先相对再绝对。
本示例在交互模式执行,非文件模式执行。
E**:**\\documents\\F盘\\ 目录不存在pkgmoda.py文件
# 打开cmd,执行下面命令
E:\\documents\\F盘>d:\\Python27\\python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.pyc
print(sys.path[0]):
run:E:\\documents\\F盘\\pkg1\\pkgmoda.pyc
print(string):<module 'string' from 'd:\\Python27\\lib\\string.pyc'>
run:E:\\documents\\F盘\\pkg1\\main_moda.pyc
# 先搜索当前包目录
print(pkgmoda):<module 'pkg1.pkgmoda' from 'pkg1\\pkgmoda.pyc'>
pkgmoda.index:E:\\documents\\F盘\\pkg1\\pkgmoda.pyc
python3.7不搜索当前包目录,直接搜索sys.path模块搜索路径,即绝对导入。
本示例在交互模式执行,非文件模式执行。
E**:**\\documents\\F盘\\ 目录不存在pkgmoda.py文件
# 打开cmd,执行下面命令
E:\\documents\\F盘>d:\\python3\\python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.py
print(sys.path[0]):
# 不搜索当前包目录
Traceback (most recent call last):
File "", line 1, in
File "E:\\documents\\F盘\\pkg1\\main_moda.py", line 4, in
import pkgmoda
ModuleNotFoundError: No module named 'pkgmoda'
python3.7直接搜索sys.path模块搜索路径,即绝对导入。
本示例在文件模式执行,非交互模式执行。
E**:**\\documents\\F盘\\ 目录不存在pkgmoda.py文件
# 打开cmd,执行下面命令
E:\\documents\\F盘>d:\\python3\\python pkg1\\main_moda.py
# 程序目录为 pkg1
print(sys.path[0]):E:\\documents\\F盘\\pkg1
run:E:\\documents\\F盘\\pkg1\\pkgmoda.py
print(string):<module 'string' from 'd:\\\\python3\\\\lib\\\\string.py'>
run:E:\\documents\\F盘\\pkg1\\main_moda.py
# 搜索sys.path的程序目录
print(pkgmoda):<module 'pkgmoda' from 'E:\\\\documents\\\\F盘\\\\pkg1\\\\pkgmoda.py'>
pkgmoda.index:E:\\documents\\F盘\\pkg1\\pkgmoda.py
将pkg1\\pkgmoda.py剪切到“E**:**\\documents\\F盘”。
当前包目录没有模块,sys.path的程序目录有模块。
python2.7先当前包目录,在sys.path,
python3.7只sys.path。
本示例在交互模式执行,非文件模式执行。
# python2.7示例
E:\\documents\\F盘>d:\\Python27\\python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.pyc
print(sys.path[0]):
run:E:\\documents\\F盘\\pkgmoda.py
print(string):<module 'string' from 'd:\\Python27\\lib\\string.pyc'>
run:E:\\documents\\F盘\\pkg1\\main_moda.py
# 搜索程序目录
print(pkgmoda):<module 'pkgmoda' from 'pkgmoda.py'>
pkgmoda.index:E:\\documents\\F盘\\pkgmoda.py
# python3.7示例
E:\\documents\\F盘>d:\\python3\\python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.py
print(sys.path[0]):
run:E:\\documents\\F盘\\pkgmoda.py
print(string):<module 'string' from 'd:\\\\python3\\\\lib\\\\string.py'>
run:E:\\documents\\F盘\\pkg1\\main_moda.py
# 搜索程序目录
print(pkgmoda):<module 'pkgmoda' from 'E:\\\\documents\\\\F盘\\\\pkgmoda.py'>
pkgmoda.index:E:\\documents\\F盘\\pkgmoda.py
“目录文件内容-1”修改 main_moda.py 为相对导入 from **.**import pkgmoda
# main_moda.py
# coding=utf-8
import sys,os
print('print(sys.path[0]):{}'.format(sys.path[0]))
from . import pkgmoda
print('run:{}'.format(os.path.abspath(__file__)))
print('print(pkgmoda):{}'.format(pkgmoda))
print('pkgmoda.index:{}'.format(pkgmoda.index))
本示例在交互模式执行,非文件模式执行。
E**:**\\documents\\F盘\\ 目录不存在pkgmoda.py文件
相对导入from . import module 从当前包搜索。
# python2.7示例
E:\\documents\\F盘>d:\\Python27\\python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.py
print(sys.path[0]):
run:E:\\documents\\F盘\\pkg1\\pkgmoda.py
print(string):<module 'string' from 'd:\\Python27\\lib\\string.pyc'>
run:E:\\documents\\F盘\\pkg1\\main_moda.py
# python2.7 相对导入从当前包搜索
print(pkgmoda):<module 'pkg1.pkgmoda' from 'pkg1\\pkgmoda.py'>
pkgmoda.index:E:\\documents\\F盘\\pkg1\\pkgmoda.py
# python3.7示例
E:\\documents\\F盘>d:\\python3\\python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.py
print(sys.path[0]):
run:E:\\documents\\F盘\\pkg1\\pkgmoda.py
print(string):<module 'string' from 'd:\\\\python3\\\\lib\\\\string.py'>
run:E:\\documents\\F盘\\pkg1\\main_moda.py
# python3.7 相对导入从当前包搜索
print(pkgmoda):<module 'pkg1.pkgmoda' from 'E:\\\\documents\\\\F盘\\\\pkg1\\\\pkgmoda.py'>
pkgmoda.index:E:\\documents\\F盘\\pkg1\\pkgmoda.py
相对导入只搜索当前包路径,不搜索sys.path路径。
将pkg1\\pkgmoda.py剪切到“E**:**\\documents\\F盘”
# python2.7示例
E:\\documents\\F盘>d:\\Python27\\python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.py
print(sys.path[0]):
# 只搜索包当前路径,不搜索sys.path路径
Traceback (most recent call last):
File "", line 1, in
File "pkg1\\main_moda.py", line 5, in
from . import pkgmoda
ImportError: cannot import name pkgmoda
# python3.7示例
E:\\documents\\F盘>d:\\python3\\python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 07:55:33) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg1.main_moda
run:E:\\documents\\F盘\\pkg1\\__init__.py
print(sys.path[0]):
# 只搜索包当前路径,不搜索sys.path路径
Traceback (most recent call last):
File "", line 1, in
File "E:\\documents\\F盘\\pkg1\\main_moda.py", line 5, in
from . import pkgmoda
ImportError: cannot import name 'pkgmoda' from 'pkg1' (E:\\documents\\F盘\\pkg1\\__init__.py)
全部0条评论
快来发表一下你的评论吧 !