Python: PYTHONHOME和PYTHONPATH的区别

简介: Python: PYTHONHOME和PYTHONPATH的区别

PYTHONHOME和PYTHONPATH的区别


官方文档解读:https://docs.python.org/3/tutorial/modules.html


6.1.2. The Module Search Path


When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

The directory containing the input script (or the current directory when no file is specified).

PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

The installation-dependent default.

当导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。如果没有找到,它会搜索一个名为spam.py的文件。变量sys.path. sys给出的目录列表中的py。路径是从这些位置初始化的:

包含输入脚本的目录(或未指定文件时的当前目录)。

PYTHONPATH(一个目录名列表,与shell变量PATH具有相同的语法)。

installation-dependent默认。

Note:On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

注意:在支持符号链接的文件系统上,包含输入脚本的目录是在符号链接之后计算的。换句话说,包含符号链接的目录没有添加到模块搜索路径中。初始化之后,Python程序可以修改sys.path。包含正在运行的脚本的目录位于搜索路径的开头,在标准库路径的前面。这意味着将加载该目录中的脚本,而不是库目录中的同名模块。除非是有意替换,否则这是一个错误。有关更多信息,请参见标准模块一节。


sys.path


sys.path 包含输入模块的目录名列表,并且当前目录也是sys.path的一部分。



相关文章
|
7天前
|
存储 Python
Python中类方法、实例方法与静态方法的区别
这三种方法的正确使用可以使代码更加清晰、组织良好并且易于理解,从而有效地支持软件开发的面向对象编程范式。
9 1
|
18天前
|
Python
全网最适合入门的面向对象编程教程:Python函数方法与接口-函数与方法的区别和lamda匿名函数
【9月更文挑战第15天】在 Python 中,函数与方法有所区别:函数是独立的代码块,可通过函数名直接调用,不依赖特定类或对象;方法则是与类或对象关联的函数,通常在类内部定义并通过对象调用。Lambda 函数是一种简洁的匿名函数定义方式,常用于简单的操作或作为其他函数的参数。根据需求,可选择使用函数、方法或 lambda 函数来实现代码逻辑。
|
19天前
|
机器学习/深度学习 人工智能 安全
python和Java的区别以及特性
Python:适合快速开发、易于维护、学习成本低、灵活高效。如果你需要快速上手,写脚本、数据处理、做点机器学习,Python就是你的首选。 Java:适合大型项目、企业级应用,性能要求较高的场景。它类型安全、跨平台能力强,而且有丰富的生态,适合更复杂和规模化的开发。
19 3
|
5天前
|
存储 编译器 Linux
Cython 和 Python 的区别
Cython 和 Python 的区别
10 0
|
6天前
|
Python
Python中类属性与实例属性的区别
了解这些区别对于编写高效、易维护的Python代码至关重要。正确地使用类属性和实例属性不仅能帮助我们更好地组织代码,还能提高代码运行的效率。
6 0
|
2月前
|
存储 测试技术 Python
Python 数组和列表有什么区别?
【8月更文挑战第29天】
37 4
|
2月前
|
C++ Python
python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
16 1
|
2月前
|
Python
python中set和frozenset方法和区别
python中set和frozenset方法和区别
|
3月前
|
存储 JSON 数据格式
Python教程:json中load和loads的区别
【7月更文挑战第17天】在Python的`json`模块中, `load`与`loads`函数均用于JSON至Python对象的转换, 区别在于: - **`loads`**处理JSON格式的**字符串** 其中`data.json`文件内容为`{"name": "Bob", "age": 30}`。 简而言之, `loads`用于字符串, 而`load`用于文件对象。根据数据来源选择合适的方法。
|
3月前
|
消息中间件 安全 数据处理
Python中的并发编程:理解多线程与多进程的区别与应用
在Python编程中,理解并发编程是提高程序性能和响应速度的关键。本文将深入探讨多线程和多进程的区别、适用场景及实际应用,帮助开发者更好地利用Python进行并发编程。
下一篇
无影云桌面