Absolute Imports

You’ve gotten up to speed on how to write import statements and how to style them like a pro. Now it’s time to learn a little more about absolute imports.

An absolute import specifies the resource to be imported using its full path from the project’s root folder.

Syntax and Practical Examples

Let’s say you have the following directory structure:

└── project

├── package1

│ ├── module1.py

│ └── module2.py

└── package2

├── __init__.py

├── module3.py

├── module4.py

└── subpackage1

└── module5.py

There’s a directory, project, which contains two sub-directories, package1 and package2. The package1 directory has two files, module1.py and module2.py.

The package2 directory has three files: two modules, module3.py and module4.py, and an initialization file, __init__.py. It also contains a directory, subpackage, which in turn contains a file, module5.py.

Let’s assume the following:

package1/module2.py contains a function, function1.

package2/__init__.py contains a class, class1.

package2/subpackage1/module5.py contains a function, function2.

The following are practical examples of absolute imports:

from package1 import module1

from package1.module2 import function1

from package2 import class1

from package2.subpackage1.module5 import function2

Note that you must give a detailed path for each package or file, from the top-level package folder. This is somewhat similar to its file path, but we use a dot (.) instead of a slash (/).

Pros and Cons of Absolute Imports

Absolute imports are preferred because they are quite clear and straightforward. It is easy to tell exactly where the imported resource is, just by looking at the statement. Additionally, absolute imports remain valid even if the current location of the import statement changes. In fact, PEP 8 explicitly recommends absolute imports.

Sometimes, however, absolute imports can get quite verbose, depending on the complexity of the directory structure. Imagine having a statement like this:

from package1.subpackage2.subpackage3.subpackage4.module5 import function6

That’s ridiculous, right? Luckily, relative imports are a good alternative in such cases!

Relative Imports

A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There are two types of relative imports: implicit and explicit. Implicit relative imports have been deprecated in Python 3, so I won’t be covering them here.

Syntax and Practical Examples

The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:

from .some_module import some_class

from ..some_package import some_function

from . import some_class

You can see that there is at least one dot in each import statement above. Relative imports make use of dot notation to specify location.

A single dot means that the module or package referenced is in the same directory as the current location. Two dots mean that it is in the parent directory of the current location—that is, the directory above. Three dots mean that it is in the grandparent directory, and so on. This will probably be familiar to you if you use a Unix-like operating system!

Let’s assume you have the same directory structure as before:

└── project

├── package1

│ ├── module1.py

│ └── module2.py

└── package2

├── __init__.py

├── module3.py

├── module4.py

└── subpackage1

└── module5.py

Recall the file contents:

package1/module2.py contains a function, function1.

package2/__init__.py contains a class, class1.

package2/subpackage1/module5.py contains a function, function2.

You can import function1 into the package1/module1.py file this way:

# package1/module1.py

from .module2 import function1

You’d use only one dot here because module2.py is in the same directory as the current module, which is module1.py.

You can import class1 and function2 into the package2/module3.py file this way:

# package2/module3.py

from . import class1

from .subpackage1.module5 import function2

In the first import statement, the single dot means that you are importing class1 from the current package. Remember that importing a package essentially imports the package’s __init__.py file as a module.

In the second import statement, you’d use a single dot again because subpackage1 is in the same directory as the current module, which is module3.py.

python3 相对路径导入_Python中相对路径(导入)和绝对路径(导入)的区别相关推荐

  1. python绝对导入_Python中的绝对导入和相对导入

    如果你做过有多个文件的Python项目,那么你一定用过import语句. 即使是对于有多个项目的Python支持者来说,import也会让人困惑!你可能正阅读本文,因为希望更深入地了解Python中的 ...

  2. idea 启动php项目路径,关于idea中Java Web项目的访问路径问题

    说明 这里只以 servlet 为例,没有涉及到框架,但其实路径的基本原理和框架的关系不大,所以学了框架的同学如果对路径有疑惑的也可以阅读此文 项目结构 在 idea 中新建一个 Java Web 项 ...

  3. easyexcel多个sheet导入_Java中Easypoi实现excel多sheet表导入导出功能

    Easypoi简化了开发中对文档的导入导出实现,并不像poi那样都要写大段工具类来搞定文档的读写. 第一步引入Easypoi依赖 cn.afterturn easypoi-spring-boot-st ...

  4. python海龟库如何导入_Python 中的海龟绘图turtle函数库的详细使用方法

    Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行 ...

  5. python3 asyncio 不阻塞_Python中的并发处理之asyncio包使用的详解

    导语:本文章记录了本人在学习Python基础之控制流程篇的重点知识及个人心得,打算入门Python的朋友们可以来一起学习并交流. 本文重点: 1.了解asyncio包的功能和使用方法: 2.了解如何避 ...

  6. python3不等于号_python中的不等于号是什么

    python中的不等于号用!=表示.Python支持的比较运算符如下图所示 比较运算符,也成关系运算符,用于对常量.变量或表达式的结果进行大小.真假等比较,如果比较结果为真,则返回 True:反之,则 ...

  7. python中x y表示_Python中表达式x += y和x = x+y 的区别详解

    前言 本文主要给大家介绍的是关于Python中表达式x += y和x = x+y 区别的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 直接看下面代码: x +=y In [66]: id( ...

  8. c语言双引号和单引号的区别_Python中的单引号和双引号有什么区别?

    在Python中使用单引号或双引号是没有区别的,都可以用来表示一个字符串.但是这两种通用的表达方式可以避免出错之外,还可以减少转义字符的使用,使程序看起来更清晰. 举两个例子: 1.包含单引号的字符 ...

  9. python中read和readline的区别_Python中read()、readline()和readlines()三者间的区别和用法...

    前言 众所周知在python中读取文件常用的三种方法:read(),readline(),readlines(),今天看项目是又忘记他们的区别了.以前看书的时候觉得这东西很简单,一眼扫过,待到用时却也 ...

  10. python代码什么时候用单引号_Python 中的单引号和双引号有什么区别?

    在Python当中可以使用单引号,也可以使用双引号,那两者有什么区别吗? 简单来说,在Python中使用单引号或双引号是没有区别的,都可以用来表示一个字符串.但是这两种通用的表达方式,除了可以简化程序 ...

最新文章

  1. C语言对strtok(),与strdup()介绍
  2. oa部署mysql_oa系统部署
  3. modbus调试时间超时_Java调试器和超时
  4. java 无法执行export 命令_模块中的export、import以及复合模式的使用方法
  5. C++的迭代器Interator
  6. python svn库_python实现svn新老库迁移
  7. 修改代理_IP代理修改上网IP地址的作用
  8. matlab模式识别实验二,模式识别实验二
  9. RxJava操作符serialize 笔记-二十六
  10. Postman局部变量设置
  11. 制作WIN7、WINPE2003、Ubuntu、dos工具箱多启动U盘
  12. 谷歌浏览器不支持html2.0,谷歌浏览器不能播放视频怎么办_chrome浏览器无法播放视频的解决方法-系统城...
  13. java 的qq 语音文件怎么打开,用手机java的QQ可以和电脑上的QQ进行语音通话吗?
  14. 2019java面试(二)
  15. 众核tilera的使用
  16. 招聘:高级运维工程师
  17. js中数组的entries方法
  18. 华为MAC-VLAN举例
  19. 最简单的无线充电传输电路
  20. Android2.3触摸屏功能详解

热门文章

  1. VMware系统运维(十一)部署虚拟化桌面 Horizon View 5.2 HTML ACCESS安装
  2. poj1054The Troublesome FrogDP
  3. 传智播客 机器学习之数据降维 学习笔记
  4. Atitit word结构化提取考试试题读取 poi读取word nlp aiprj 目录 1.1. 结构化后数据 1 1.2. 文字读取 1 1.3. 图片读取 1 1.4. Doc的表格读取 /
  5. Atitit pg10分区 总结 1.1. create table tmp_log (  1 1.2. -创建索引 1 1.3. 查看表 in pgadmin4 2 2. 二 分区表管理 2 2.1
  6. Atitit 界面接口技术 cli gui nui cui管理 attilax总结 1. NUI 1 1.1. 问:什么是“自然用户界面”? 1 2. Cli到gui到nui CUI 2 2.1.
  7. Atitit xml命名空间机制
  8. Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx
  9. atitit.人脸识别的应用场景and使用最佳实践 java .net php
  10. Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率..