Python命名空间包

news/2024/10/18 23:19:48/文章来源:https://www.cnblogs.com/smartljy/p/18475211

python命名空间包

python namespace package

What is a Namespace Package?

A namespace package is a type of package introduced in Python 3.3 that does not require an __init__.py file. Unlike traditional packages, multiple directories (often spread across different locations) can collectively contribute to the same namespace.

Key Idea:
A namespace package allows the same package name (like mypackage) to be split across multiple directories. Python treats each directory as part of the same logical package.


Example of a Namespace Package

Let's say you have two folders:

/project1/mypackage/└── module1.py/project2/mypackage/└── module2.py

If both /project1 and /project2 are in your PYTHONPATH, you can import from mypackage seamlessly:

from mypackage.module1 import function1
from mypackage.module2 import function2

Python combines both directories into a single namespace under mypackage.


Namespace Package vs. Traditional Package

  • Traditional Package:
    Requires an __init__.py file to be present in each folder that is part of the package.
    E.g.: /mypackage/__init__.py defines the root of the package.

  • Namespace Package:
    Does not require __init__.py, allowing multiple directories to contribute to the same namespace.

Use Case: Namespace packages are useful when:

  • You need to split a package across multiple projects.
  • You want to distribute parts of a package separately (e.g., plugins).

What is the Advantage of python -m?

The python -m command runs a module or package as a script, but with several advantages over running the script directly.

1. Executes with the Correct sys.path Configuration

  • When you run a script directly (python script.py), the directory containing the script is automatically added to sys.path as the first entry.
  • However, when using python -m, Python treats the module or package as part of your importable packages and loads it properly within the Python environment.

Example:

python -m mypackage.module1

This ensures the module is executed in the context of the package (i.e., mypackage), making relative imports within the package work correctly.


2. Supports Relative Imports

If your module uses relative imports, such as:

from .module2 import some_function

This will only work if you use python -m to run the module within the package context:

python -m mypackage.module1

If you try to run it directly as a script:

python mypackage/module1.py

You’ll get an ImportError because Python doesn't treat it as a package import.


3. Better Handling of Package Structure

Using python -m ensures that Python respects the package hierarchy and imports work correctly. This is especially useful in larger projects where modules and submodules are interdependent.


4. Works with Packages and Modules Alike

You can use python -m to run:

  • Single modules:

    python -m module_name
    
  • Packages with __main__.py files:
    If a package contains a __main__.py file, you can run it as:

    python -m package_name
    

This allows Python to initialize the package before executing the main logic.


Summary

  1. Namespace Packages:

    • Allow splitting the same package across multiple directories.
    • Do not require __init__.py.
    • Useful for plugins or modular codebases.
  2. Advantages of python -m:

    • Ensures correct import context with proper handling of sys.path.
    • Supports relative imports within packages.
    • Works seamlessly with both modules and packages.
    • Makes it easier to handle package-based execution with __main__.py.

In summary, python -m is preferred when working with packages, especially if you need to ensure the package structure is respected and relative imports work correctly.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/817128.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20222306 2024-2025-1 《网络与系统攻防技术》实验二实验报告

1.实验内容 1.1 实践目标 (1)使用netcat获取主机操作Shell,cron启动某项任务(任务自定) PS:cron是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF meterpreter(或其他软件)生成可执行文件,利…

transformers 推理 Qwen2.5 等大模型技术细节详解(一)transformers 初始化和对象加载(文末免费送书)

本文详细讲解 transformers 推理大语言模型的初始化过程,包括 Python 包搜索、LazyModule 延迟模块、模块搜索和 Python 包 API 设计美学……上周收到一位网友的私信,希望老牛同学写一篇有关使用 transformers 框架推理大模型的技术细节的文章。 老牛同学刚开始以为这类的文章…

UI开发概述

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs…

MiGPT让你的小爱音响更聪明

大家好,我是晓凡。 今天要给大家带来一个超级有趣的开源项目MiGPT。 这个项目,简直就是给小爱音箱装上了超级大脑,让你的小爱音箱更聪明。 想象一下,当小爱音箱接入大模型后,上知天文,下知地理,从“人工智障”秒变学霸。 一、什么是MiGPTMiGPT是一个由idootop团队开发的…

基于amis后端低代码平台

写这个平台是为了解决多年对于项目的困扰,不想碰到新项目就重新来,通过业务模块的积累,能进行模块化安装。新的项目只需要安装模块就能搭建一套完整的业务系统。amis-api 的所有基础代码都以模块的形式组合在一起。这些模块可以随时从数据库中安装或卸载。这些模块有两大目的…

数据采集与融合技术实验课程作业一

数据采集与融合技术实验课程作业一作业所属课程 https://edu.cnblogs.com/campus/fzu/2024DataCollectionandFusiontechnology作业链接 https://edu.cnblogs.com/campus/fzu/2024DataCollectionandFusiontechnology/homework/13286gitee码云代码位置 https://gitee.com/wang-qi…

C3P0 链子分析学习

C3P0 链子分析学习 概述 C3P0是一个开源的数据库连接池,它实现了数据源与JNDI绑定,支持JDBC3规范和实现了JDBC2的标准扩展说明的Connection和Statement池的DataSources对象。即将用于连接数据库的连接整合在一起形成一个随取随用的数据库连接池,使用它的开源项目有Hibernate…

20241018每日一题洛谷P2386

普及 每日一题 信息学竞赛 1206:放苹果 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。 第一行是测试数据的数目t(0<=t<=20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N…

图片与向量的关系

如何从向量角度描述表示图片黑白图片黑白图片(灰度图)通过 2 维向量(矩阵)来表达。2个维度的长度分别代表了图片的高度和宽度(以像素为单位),向量元素记录着每一个像素的灰度(数值越大,颜色越浅) 例如下面右图矩阵标注了左图像素点的灰度分布:彩色图片彩色图片通过 …

数据采集与融合技术第二次作业

学号姓名 102202132 郑冰智这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/2024DataCollectionandFusiontechnology/homework/13285这个作业的目标 爬取天气网、股票相关信息、中国大学2021主榜所有院校信息,并存储在数据库中实验二仓库地址 https://gitee.com/zheng…

【LGR-203-Div.4】洛谷入门赛 #28

【LGR-203-Div.4】洛谷入门赛 #28\(A\) luogu B4042 [语言月赛 202410] 顺序结构 \(AC\)顺序结构。点击查看代码 int main() { ll a;cin>>a;cout<<3*(5+a)<<" "<<3*a+5<<endl;return 0; }\(B\) luogu B4043 [语言月赛 202410] 刻度尺…

uni-app小程序(快手、抖音)getCurrentPages使用坑位记录2

前情 uni-app是我比较喜欢的跨平台框架,它能开发小程序/H5/APP(安卓/iOS),重要的是对前端开发友好,自带的IDE让开发体验也挺棒的,现公司项目就是主推uni-app,我主要负责抖音和快手端小程序。 坑位 公司历史原因项目有APP端小程序端,但并不使用uni-app的一端发布所有平台,…