3.5.3 示例import-python2.7搜索當前包
python2.7先搜索當前包目錄,再搜索sys.path模塊搜索路徑。即先相對再絕對。
本示例在交互模式執(zhí)行,非文件模式執(zhí)行。
E**:**\\documents\\F盤\\ 目錄不存在pkgmoda.py文件
# 打開cmd,執(zhí)行下面命令
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
3.5.4 示例import-python3.7略過當前包
python3.7不搜索當前包目錄,直接搜索sys.path模塊搜索路徑,即絕對導入。
本示例在交互模式執(zhí)行,非文件模式執(zhí)行。
E**:**\\documents\\F盤\\ 目錄不存在pkgmoda.py文件
# 打開cmd,執(zhí)行下面命令
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'
3.5.5 示例import-python3.7程序目錄-文件模式
python3.7直接搜索sys.path模塊搜索路徑,即絕對導入。
本示例在文件模式執(zhí)行,非交互模式執(zhí)行。
E**:**\\documents\\F盤\\ 目錄不存在pkgmoda.py文件
# 打開cmd,執(zhí)行下面命令
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
3.5.6 示例import-sys.path
將pkg1\\pkgmoda.py剪切到“E**:**\\documents\\F盤”。
當前包目錄沒有模塊,sys.path的程序目錄有模塊。
python2.7先當前包目錄,在sys.path,
python3.7只sys.path。
本示例在交互模式執(zhí)行,非文件模式執(zhí)行。
# 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
3.5.7 示例from點相對導入
“目錄文件內(nèi)容-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))
本示例在交互模式執(zhí)行,非文件模式執(zhí)行。
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)
-
編程語言
+關(guān)注
關(guān)注
10文章
1929瀏覽量
34539 -
代碼
+關(guān)注
關(guān)注
30文章
4722瀏覽量
68231 -
python
+關(guān)注
關(guān)注
55文章
4767瀏覽量
84375
發(fā)布評論請先 登錄
相關(guān)推薦
評論