Python Fundamentals
文章平均质量分 83
Python ins and outs
captainOO7
学习使人进步,分享使人快乐
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
What Does “Directory of the Script Being Run” Mean?
When running Python scripts, sys.path[0] is set to the script's directory, not the shell's current working directory (CWD). For example, executing /path/script.py makes Python search for imports in /path/, even if the shell is elsewhere原创 2025-07-22 20:36:23 · 1014 阅读 · 0 评论 -
The Module Search Path in Python
When you run import mod in Python, the interpreter follows a specific search flow: First, it checks sys.modules for previously loaded modules. If not found, it looks for built-in modules, then searches through sys.path directories (including script locatio原创 2025-07-22 19:29:22 · 931 阅读 · 0 评论 -
ImportError: attempted relative import beyond top-level package
Python's relative import system enforces boundaries based on package hierarchy. When using from ..... import, five dots mean going up four levels from the current package. If the package depth is insufficient (e.g., a.b.c.d has only 3 parent levels), Pytho原创 2025-07-24 00:09:18 · 392 阅读 · 0 评论 -
What is Current Working Directory (CWD) in Python
当前工作目录(CWD)是计算过程中使用的默认文件系统位置,作为访问文件和执行相对路径命令的基准点。其主要特点包括:作为上下文锚点(决定系统查找文件的起始位置)、进程特定性(每个进程独立继承父进程的CWD)和动态可变性(可通过命令如cd或Python的os.chdir()修改)。CWD直接影响文件访问路径、脚本执行行为和调试过程,误解CWD常导致"文件未找到"错误。开发人员可通过os.getcwd()查看当前目录,需注意脚本执行位置与保存位置可能不同,这是常见误区。原创 2025-07-22 07:08:12 · 891 阅读 · 0 评论 -
Three ways to run a python script file
Python offers three ways to run code with key differences in behavior: 1️⃣ Direct script execution (python script.py) Sets sys.path[0] to script's directory __name__ == "__main__" ❌ Relative imports fail 2️⃣ Module execution (python -m package.mo原创 2025-07-23 00:01:10 · 429 阅读 · 0 评论 -
Python Modules and Packages -- dive deep
This article explores Python modules and Python packages, two mechanisms that facilitate modular programming.Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks or module原创 2025-07-22 16:35:20 · 892 阅读 · 0 评论 -
How script location, CWD, and relative imports interact
Python模块导入最佳实践 当项目采用模块化结构时,直接运行子模块脚本(如python utils.py)会导致相对导入失败,因为Python无法识别其所属包。正确方法是: 顶层脚本直接运行(如python main.py) 子模块通过-m参数作为模块运行(如python -m package.utils),确保相对导入正常工作 调试时检查sys.path和__name__,确认模块搜索路径和执行上下文 关键点:始终从项目根目录执行,避免直接运行嵌套脚本,使用-m参数保持包结构完整性。原创 2025-07-22 23:17:46 · 410 阅读 · 0 评论 -
PATH, sys.path, and PYTHONPATH
摘要: Python模块搜索路径涉及三个关键机制:PATH(操作系统级,定位Python解释器)、PYTHONPATH(环境变量级,影响模块搜索)和sys.path(Python运行时级,实际导入路径列表)。PYTHONPATH在运行时将指定目录注入sys.path,优先级较高。例如,设置PYTHONPATH=/home/jun/my_libs可使任意脚本导入cool_module.py,而手动修改sys.path仅对后续导入生效。需注意:PYTHONPATH需显式指定目录(非递归),可能被IDE忽略,且与原创 2025-07-22 18:01:51 · 870 阅读 · 0 评论 -
Differences between sys.path vs PATH
sys.path and PATH serve distinct roles: sys.path is a Python-specific list that determines where the interpreter searches for modules/packages. It can be modified at runtime (sys.path.append()). PATH is an OS environment variable that locates executables原创 2025-07-22 18:06:40 · 926 阅读 · 0 评论 -
How to Check a Module’s Type
本文介绍了四种在Python中检测模块类型的方法:1)使用__file__属性区分普通模块和内置模块;2)尝试用inspect模块;3)通过sys.builtin_module_names检查内置模块;4)结合sys和importlib.util.find_spec()准确分类模块类型(内置/纯Python/C扩展)。这些方法可帮助开发者识别模块来源,了解其实现方式(如itertools是内置模块,re是C扩展,json是纯Python模块)。原创 2025-07-23 16:45:21 · 405 阅读 · 0 评论 -
[draft]Python Relative Path v.s. Relative Import
摘要:相对路径和相对导入虽然都使用点符号(.和..),但在Python中作用不同。相对路径基于操作系统当前工作目录定位文件/文件夹,如open("data/input.txt");而相对导入基于Python包层次结构导入模块,如from..configs import log_config。相对路径依赖文件系统位置,相对导入依赖包的__init__.py结构。两者都可能因上下文变化失效,但分别服务于文件访问和代码复用。例如,Path(__file__).parent用于文件路径,from原创 2025-07-22 02:55:45 · 869 阅读 · 0 评论 -
Python Absolute Path v.s. Relative Path
Understanding Absolute vs. Relative Paths in Python Absolute paths specify the full location of a file from the root directory (e.g., C:\Users\file.txt or /home/user/file.txt), ensuring precise access regardless of the working directory. Relative paths ref原创 2025-07-21 22:22:26 · 745 阅读 · 0 评论 -
Python Absolute v.s. Relative Imports
pyfile).原创 2025-07-22 02:47:56 · 971 阅读 · 0 评论
分享