随笔 - 121  文章 - 0  评论 - 11  阅读 - 74万

import as from import 区别

        在python中import或者from…import是用来导入相应的模块。那每一种有什么具体的差别呢?

一、import
        只有import,为最简单的引入对应的包。例如:

import pickle #引入 pickle包
import os #引入 os包
二、from A import B
        这种方式意味着从A中引入B。相当于:import A, b=A.b。

from urllib.parse import urlparse

from sys import argv
        除了这种基本形式,还有另外两种,例如:

from os import makedirs, unlink, sep #从os包中引入 makedirs.unlink,sep类
from os import listdir, getcwd #从os包中引入 listdir, getcwd 类

from os.path import dirname, exists, isdir, splitext #从 os包中的path类中引入 dirmame exists 方法
from os.path import join                              #从 os包中的path类中引入 join 方法
三、import A as B
        这种方式为给引入的包A定义一个别名B,例如:

import xml.etree.ElementTree as ET #给包xml.etree.ElementTree 定义一个 ET 别名

posted on   feng..liu  阅读(15198)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示