<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](unix.xhtml "2. 在Unix平臺中使用Python") | - [上一頁](index.xhtml "安裝和使用 Python") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [安裝和使用 Python](index.xhtml) ? - $('.inline-search').show(0); | # 1. 命令行與環境 CPython 解析器會掃描命令行與環境用于獲取各種設置信息。 **CPython implementation detail:** 其他實現的命令行方案可能有所不同。 更多相關資源請參閱 [其他實現](../reference/introduction.xhtml#implementations)。 ## 1.1. 命令行 對 Python 發起調用時,你可以指定以下的任意選項: ``` python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args] ``` 當然最常見的用例就是簡單地啟動執行一個腳本: ``` python myscript.py ``` ### 1.1.1. 接口選項 解釋器接口類似于 UNIX shell,但提供了一些額外的發起調用方法: - 當調用時附帶連接到某個 tty 設備的標準輸入時,它會提示輸入命令并執行它們,直到讀入一個 EOF(文件結束字符,其產生方式是在 UNIX 中按 Ctrl-D 或在 Windows 中按 Ctrl-Z, Enter。) - 當調用時附帶一個文件名參數或以一個文件作為標準輸入時,它會從該文件讀取并執行腳本程序。 - 當調用時附帶一個目錄名參數時,它會從該目錄讀取并執行具有適當名稱的腳本程序。 - 當調用時附帶 `-c command` 時,它會執行 *command* 所給出的 Python 語句。 在這里 *command* 可以包含以換行符分隔的多條語句。 請注意前導空格在 Python 語句中是有重要作用的! - 當調用時附帶 `-m module-name` 時,會在 Python 模塊路徑中查找指定的模塊,并將其作為腳本程序執行。 在非交互模式下,會對全部輸入先解析再執行。 一個接口選項會終結解釋器所讀入的選項列表,后續的所有參數將被放入 [`sys.argv`](../library/sys.xhtml#sys.argv "sys.argv") -- 請注意其中首個元素即第零項 (`sys.argv[0]`) 會是一個表示程序源的字符串。 `-c`` <command>`執行 *command* 中的 Python 代碼。 *command* 可以為一條或以換行符分隔的多條語句,其中前導空格像在普通模塊代碼中一樣具有作用。 如果給出此選項,[`sys.argv`](../library/sys.xhtml#sys.argv "sys.argv") 的首個元素將為 `"-c"` 并且當前目錄將被加入 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的開頭(以允許該目錄中的模塊作為最高層級模塊被導入)。 `-m`` <module-name>`在 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 中搜索指定名稱的模塊并將其內容作為 [`__main__`](../library/__main__.xhtml#module-__main__ "__main__: The environment where the top-level script is run.") 模塊來執行。 由于該參數為 *module* 名稱,你不應給出文件擴展名 (`.py`)。 模塊名稱應為絕對有效的 Python 模塊名稱,但具體實現可能并不總是強制要求這一點(例如它可能允許你使用包含連字符的名稱)。 包名稱(包括命名空間包)也允許使用。 當所提供的是包名稱而非普通模塊名稱時,解釋器將把 `<pkg>.__main__` 作為主模塊來執行。 此行為特意被設計為與作為腳本參數傳遞給解釋器的目錄和 zip 文件的處理方式類似。 注解 此選項不適用于內置模塊和以 C 編寫的擴展模塊,因為它們并沒有對應的 Python 模塊文件。 但是它仍然適用于預編譯的模塊,即使沒有可用的初始源文件。 如果給出此選項,[`sys.argv`](../library/sys.xhtml#sys.argv "sys.argv") 的首個元素將為模塊文件的完整路徑 (在定位模塊文件期間,首個元素將設為 `"-m"`)。 與 [`-c`](#cmdoption-c) 選項一樣,當前目錄將被加入 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的開頭。 許多標準庫模塊都包含作為腳本執行時會被發起調用的代碼。 其中的一個例子是 [`timeit`](../library/timeit.xhtml#module-timeit "timeit: Measure the execution time of small code snippets.") 模塊: ``` python -mtimeit -s 'setup here' 'benchmarked code here' python -mtimeit -h # for details ``` 參見 [`runpy.run_module()`](../library/runpy.xhtml#runpy.run_module "runpy.run_module")Python 代碼可以直接使用的等效功能 [**PEP 338**](https://www.python.org/dev/peps/pep-0338) \[https://www.python.org/dev/peps/pep-0338\] -- 將模塊作為腳本執行 在 3.1 版更改: 提供包名稱來運行 `__main__` 子模塊。 在 3.4 版更改: 同樣支持命名空間包 `-`從標準輸入 ([`sys.stdin`](../library/sys.xhtml#sys.stdin "sys.stdin")) 讀取命令。 如果標準輸入為一個終端,則使用 [`-i`](#cmdoption-i)。 如果給出此選項,[`sys.argv`](../library/sys.xhtml#sys.argv "sys.argv") 的首個元素將為 `"-"` 并且當前目錄將被加入 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的開頭。 `<script>`執行 *script* 中的 Python 代碼,該參數應為一個(絕對或相對)文件系統路徑,指向某個 Python 文件、包含 `__main__.py` 文件的目錄,或包含 `__main__.py` 文件的 zip 文件。 如果給出此選項,[`sys.argv`](../library/sys.xhtml#sys.argv "sys.argv") 的首個元素將為在命令行中指定的腳本名稱。 如果腳本名稱直接指向一個 Python 文件,則包含該文件的目錄將被加入 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的開頭,并且該文件會被作為 [`__main__`](../library/__main__.xhtml#module-__main__ "__main__: The environment where the top-level script is run.") 模塊來執行。 如果腳本名稱指向一個目錄或 zip 文件,則腳本名稱將被加入 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的開頭,并且該位置中的 `__main__.py` 文件會被作為 [`__main__`](../library/__main__.xhtml#module-__main__ "__main__: The environment where the top-level script is run.") 模塊來執行。 參見 [`runpy.run_path()`](../library/runpy.xhtml#runpy.run_path "runpy.run_path")Python 代碼可以直接使用的等效功能 如果沒有給出接口選項,則使用 [`-i`](#cmdoption-i),`sys.argv[0]` 將為空字符串 (`""`),并且當前目錄會被加入 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的開頭。 此外,tab 補全和歷史編輯會自動啟用,如果你的系統平臺支持此功能的話 (參見 [Readline configuration](../library/site.xhtml#rlcompleter-config))。 參見 [調用解釋器](../tutorial/interpreter.xhtml#tut-invoking) 在 3.4 版更改: 自動啟用 tab 補全和歷史編輯。 ### 1.1.2. 通用選項 `-?````-h````--help```打印全部命令行選項的簡短描述。 `-V````--version```打印 Python 版本號并退出。 示例輸出信息如下: ``` Python 3.7.0b2+ ``` 如果重復給出,則打印有關構建的更多信息,例如: ``` Python 3.7.0b2+ (3.7:0c076caaa8, Sep 22 2018, 12:04:24) [GCC 6.2.0 20161005] ``` 3\.6 新版功能: `-VV` 選項。 ### 1.1.3. 其他選項 `-b```在將 [`bytes`](../library/stdtypes.xhtml#bytes "bytes") 或 [`bytearray`](../library/stdtypes.xhtml#bytearray "bytearray") 與 [`str`](../library/stdtypes.xhtml#str "str") 或是將 [`bytes`](../library/stdtypes.xhtml#bytes "bytes") 與 [`int`](../library/functions.xhtml#int "int") 比較時發出警告。 如果重復給出該選項 (`-bb`) 則會引發錯誤。 在 3.5 版更改: 影響 [`bytes`](../library/stdtypes.xhtml#bytes "bytes") 與 [`int`](../library/functions.xhtml#int "int") 的比較。 `-B```如果給出此選項,Python 將不會試圖在導入源模塊時寫入 `.pyc` 文件。 另請參閱 [`PYTHONDONTWRITEBYTECODE`](#envvar-PYTHONDONTWRITEBYTECODE)。 `--check-hash-based-pycs`` default|always|never`控制基于哈希值的 `.pyc` 文件的驗證行為。 參見 [已緩存字節碼的失效](../reference/import.xhtml#pyc-invalidation)。 當設為 `default` 時,已選定和未選定的基于哈希值的字節碼緩存文件將根據其默認語義進行驗證。 當設為 `always` 時,所有基于哈希值的 `.pyc` 文件,不論是已選定還是未選定的都將根據其對應的源文件進行驗證。 當設為 `never` 時,基于哈希值的 `.pyc` 文件將不會根據其對應的源文件進行驗證。 基于時間戳的 `.pyc` 文件的語義不會受此選項影響。 `-d```開啟解析器調試輸出(限專家使用,依賴于編譯選項)。 另請參閱 [`PYTHONDEBUG`](#envvar-PYTHONDEBUG)。 `-E```忽略所有 `PYTHON*` 環境變量,例如可能已設置的 [`PYTHONPATH`](#envvar-PYTHONPATH) 和 [`PYTHONHOME`](#envvar-PYTHONHOME)。 `-i```當有腳本被作為首個參數傳入或使用了 [`-c`](#cmdoption-c) 選項時,在執行腳本或命令之后進入交互模式,即使是在 [`sys.stdin`](../library/sys.xhtml#sys.stdin "sys.stdin") 并不是一個終端的時候。 [`PYTHONSTARTUP`](#envvar-PYTHONSTARTUP) 文件不會被讀取。 這一選項的用處是在腳本引發異常時檢查全局變量或者棧跟蹤。 另請參閱 [`PYTHONINSPECT`](#envvar-PYTHONINSPECT)。 `-I```在隔離模式下運行 Python。 這將同時應用 -E 和 -s。 在隔離模式下 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 既不包含腳本所在目錄也不包含用戶的 site-packages 目錄。 所有 `PYTHON*` 環境變量也會被忽略。 還可以施加更進一步的限制以防止用戶注入惡意代碼。 3\.4 新版功能. `-O```移除 assert 語句以及任何以 [`__debug__`](../library/constants.xhtml#__debug__ "__debug__") 的值作為條件的代碼。 通過在 `.pyc` 擴展名之前添加 `.opt-1` 來擴充已編譯文件 ([bytecode](../glossary.xhtml#term-bytecode)) 的文件名 (參見 [**PEP 488**](https://www.python.org/dev/peps/pep-0488) \[https://www.python.org/dev/peps/pep-0488\])。 另請參閱 [`PYTHONOPTIMIZE`](#envvar-PYTHONOPTIMIZE)。 在 3.5 版更改: 依據 [**PEP 488**](https://www.python.org/dev/peps/pep-0488) \[https://www.python.org/dev/peps/pep-0488\] 修改 `.pyc` 文件名。 `-OO```在啟用 [`-O`](#cmdoption-o) 的同時丟棄文檔字符串。 通過在 `.pyc` 擴展名之前添加 `.opt-2` 來擴展已編譯文件 ([bytecode](../glossary.xhtml#term-bytecode)) 的文件名 (參見 [**PEP 488**](https://www.python.org/dev/peps/pep-0488) \[https://www.python.org/dev/peps/pep-0488\])。 在 3.5 版更改: 依據 [**PEP 488**](https://www.python.org/dev/peps/pep-0488) \[https://www.python.org/dev/peps/pep-0488\] 修改 `.pyc` 文件名。 `-q```即使在交互模式下也不顯示版權和版本信息。 3\.2 新版功能. `-R```開啟哈希隨機化。 此選項權 [`PYTHONHASHSEED`](#envvar-PYTHONHASHSEED) 環境變量設置為 `0` 時起作用,因為哈希隨機化是默認啟用的。 在先前的 Python 版本中,此選項會開啟哈希隨機化,這樣 str, bytes 和 datetime 的 [`__hash__()`](../reference/datamodel.xhtml#object.__hash__ "object.__hash__") 值將會使用一個不可預測的隨機值來“加鹽”。 雖然它們在單個 Python 進程中會保持不變,但在重復發起的 Python 調用之間則是不可預測的。 哈希隨機化旨在針對由精心選擇的輸入引起的拒絕服務攻擊提供防護,這種輸入利用了構造 dict 在最壞情況下的性能即 O(n^2) 復雜度。 詳情請參閱 <http://www.ocert.org/advisories/ocert-2011-003.html>。 [`PYTHONHASHSEED`](#envvar-PYTHONHASHSEED) 允許你為哈希種子密碼設置一個固定值。 在 3.7 版更改: 此選項不會再被忽略。 3\.2.3 新版功能. `-s```不要將 [`用戶 site-packages 目錄`](../library/site.xhtml#site.USER_SITE "site.USER_SITE") 添加到 [`sys.path`](../library/sys.xhtml#sys.path "sys.path")。 參見 [**PEP 370**](https://www.python.org/dev/peps/pep-0370) \[https://www.python.org/dev/peps/pep-0370\] -- 分用戶的 site-packages 目錄 `-S```禁用 [`site`](../library/site.xhtml#module-site "site: Module responsible for site-specific configuration.") 的導入及其所附帶的基于站點對 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的操作。 如果 [`site`](../library/site.xhtml#module-site "site: Module responsible for site-specific configuration.") 會在稍后被顯式地導入也會禁用這些操作 (如果你希望觸發它們則應調用 [`site.main()`](../library/site.xhtml#site.main "site.main"))。 `-u```強制 stdout 和 stderr 流不使用緩沖。 此選項對 stdin 流無影響。 另請參閱 [`PYTHONUNBUFFERED`](#envvar-PYTHONUNBUFFERED)。 在 3.7 版更改: stdout 和 stderr 流在文本層現在不使用緩沖。 `-v```每當一個模塊被初始化時打印一條信息,顯示其加載位置(文件名或內置模塊)。 當重復給出時 (`-vv`),為搜索模塊時所檢查的每個文件都打印一條消息。 此外還提供退出時有關模塊清理的信息A。 另請參閱 [`PYTHONVERBOSE`](#envvar-PYTHONVERBOSE)。 `-W`` arg`警告控制。 Python 的警告機制在默認情況下會向 [`sys.stderr`](../library/sys.xhtml#sys.stderr "sys.stderr") 打印警告消息。 典型的警告消息具有如下形式: ``` file:line: category: message ``` 默認情況下,每個警告都對于其發生所在的每個源行都會打印一次。 此選項可控制警告打印的頻繁程度。 可以給出多個 [`-W`](#cmdoption-w) 選項;當某個警告能與多個選項匹配時,將執行最后一個匹配選項的操作。 無效的 [`-W`](#cmdoption-w) 選項將被忽略(但是,在發出第一個警告時會打印有關無效選項的警告消息)。 警告也可以使用 [`PYTHONWARNINGS`](#envvar-PYTHONWARNINGS) 環境變量以及使用 [`warnings`](../library/warnings.xhtml#module-warnings "warnings: Issue warning messages and control their disposition.") 模塊在 Python 程序內部進行控制。 最簡單的設置是將某個特定操作無條件地應用于進程所發出所有警告 (即使是在默認情況下會忽略的那些警告): ``` -Wdefault # Warn once per call location -Werror # Convert to exceptions -Walways # Warn every time -Wmodule # Warn once per calling module -Wonce # Warn once per Python process -Wignore # Never warn ``` 操作名稱可以根據需要進行縮寫 (例如 `-Wi`, `-Wd`, `-Wa`, `-We`),解釋器將會把它們解析為適當的操作名稱。 請參閱 [The Warnings Filter](../library/warnings.xhtml#warning-filter) 和 [Describing Warning Filters](../library/warnings.xhtml#describing-warning-filters) 了解更多細節。 `-x```跳過源中第一行,以允許使用非 Unix 形式的 `#!cmd`。 這適用于 DOS 專屬的破解操作。 `-X```保留用于各種具體實現專屬的選項。 CPython 目前定義了下列可用的值: - `-X faulthandler` 啟用 [`faulthandler`](../library/faulthandler.xhtml#module-faulthandler "faulthandler: Dump the Python traceback."); - `-X showrefcount` 當程序結束或在交互解釋器中的每條語句之后輸出總引用計數和已使用內存塊計數。 此選項僅在調試版本中有效。 - `-X tracemalloc` 使用 [`tracemalloc`](../library/tracemalloc.xhtml#module-tracemalloc "tracemalloc: Trace memory allocations.") 模塊啟動對 Python 內存分配的跟蹤。 默認情況下,只有最近的幀會保存在跟蹤的回溯信息中。 使用 `-X tracemalloc=NFRAME` 以啟動限定回溯 *NFRAME* 幀的跟蹤。 請參閱 [`tracemalloc.start()`](../library/tracemalloc.xhtml#tracemalloc.start "tracemalloc.start") 了解詳情。 - `-X showalloccount` 當程序結束時輸出每種類型的已分配對象的總數。 此選項僅當 Python 在定義了 `COUNT_ALLOCS` 后構建時才會生效。 - `-X importtime` 顯示每次導入耗費的時間。 它會顯示模塊名稱,累計時間(包括嵌套的導入)和自身時間(排除嵌套的導入)。 請注意它的輸出在多線程應用程序中可能會出錯。 典型用法如 `python3 -X importtime -c 'import asyncio'`。 另請參閱 [`PYTHONPROFILEIMPORTTIME`](#envvar-PYTHONPROFILEIMPORTTIME)。 - `-X dev`: 啟用 CPython 的“開發模式”,引入額外的運行時檢測,這些檢測因開銷過大而無法默認啟用。 如果代碼是正確的則它不會比默認輸出更詳細:新增警告只會在發現問題時才會發出。 開發模式的作用效果: - 添加 `default` 警告過濾器,即 [`-W`](#cmdoption-w)`default`。 - 在內存分配器上安裝調試鉤子:參見 [`PyMem_SetupDebugHooks()`](../c-api/memory.xhtml#c.PyMem_SetupDebugHooks "PyMem_SetupDebugHooks") C 函數。 - 啟用 [`faulthandler`](../library/faulthandler.xhtml#module-faulthandler "faulthandler: Dump the Python traceback.") 模塊以在發生崩潰時轉儲 Python 回溯信息。 - 啟用 [asyncio 調試模式](../library/asyncio-dev.xhtml#asyncio-debug-mode)。 - 將 [`sys.flags`](../library/sys.xhtml#sys.flags "sys.flags") 的 `dev_mode` 屬性設為 `True` - `-X utf8` 為操作系統接口啟用 UTF-8 模式,覆蓋默認的區域感知模式。 `-X utf8=0` 顯式地禁用 UTF-8 模式(即使在它應當被自動激活的時候)。 請參閱 [`PYTHONUTF8`](#envvar-PYTHONUTF8) 了解詳情。 它還允許傳入任意值并通過 [`sys._xoptions`](../library/sys.xhtml#sys._xoptions "sys._xoptions") 字典來提取這些值。 在 3.2 版更改: 增加了 [`-X`](#id5) 選項。 3\.3 新版功能: `-X faulthandler` 選項。 3\.4 新版功能: `-X showrefcount` 與 `-X tracemalloc` 選項。 3\.6 新版功能: `-X showalloccount` 選項。 3\.7 新版功能: `-X importtime`, `-X dev` 與 `-X utf8` 選項。 ### 1.1.4. 不應當使用的選項 `-J```保留給 [Jython](http://www.jython.org/) \[http://www.jython.org/\] 使用。 ## 1.2. 環境變量 這些環境變量會影響 Python 的行為,它們是在命令行開關之前被處理的,但 -E 或 -I 除外。 根據約定,當存在沖突時命令行開關會覆蓋環境變量的設置。 `PYTHONHOME`更改標準 Python 庫的位置。 默認情況下庫是在 `prefix/lib/pythonversion` 和 `exec_prefix/lib/pythonversion` 中搜索,其中 `prefix` 和 `exec_prefix` 是由安裝位置確定的目錄,默認都位于 `/usr/local`。 當 [`PYTHONHOME`](#envvar-PYTHONHOME) 被設為單個目錄時,它的值會同時替代 `prefix` 和 `exec_prefix`。 要為兩者指定不同的值,請將 [`PYTHONHOME`](#envvar-PYTHONHOME) 設為 `prefix:exec_prefix`。 `PYTHONPATH`增加模塊文件默認搜索路徑。 所用格式與終端的 `PATH` 相同:一個或多個由 [`os.pathsep`](../library/os.xhtml#os.pathsep "os.pathsep") 分隔的目錄路徑名稱(例如 Unix 上用冒號而在 Windows 上用分號)。 默認忽略不存在的目錄。 除了普通目錄之外,單個 [`PYTHONPATH`](#envvar-PYTHONPATH) 條目可以引用包含純Python模塊的zip文件(源代碼或編譯形式)。無法從zip文件導入擴展模塊。 The default search path is installation dependent, but generally begins with `prefix/lib/pythonversion` (see [`PYTHONHOME`](#envvar-PYTHONHOME) above). It is *always* appended to [`PYTHONPATH`](#envvar-PYTHONPATH). An additional directory will be inserted in the search path in front of [`PYTHONPATH`](#envvar-PYTHONPATH) as described above under [接口選項](#using-on-interface-options). The search path can be manipulated from within a Python program as the variable [`sys.path`](../library/sys.xhtml#sys.path "sys.path"). `PYTHONSTARTUP`If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session. You can also change the prompts [`sys.ps1`](../library/sys.xhtml#sys.ps1 "sys.ps1") and [`sys.ps2`](../library/sys.xhtml#sys.ps2 "sys.ps2") and the hook [`sys.__interactivehook__`](../library/sys.xhtml#sys.__interactivehook__ "sys.__interactivehook__") in this file. `PYTHONOPTIMIZE`If this is set to a non-empty string it is equivalent to specifying the [`-O`](#cmdoption-o) option. If set to an integer, it is equivalent to specifying [`-O`](#cmdoption-o) multiple times. `PYTHONBREAKPOINT`If this is set, it names a callable using dotted-path notation. The module containing the callable will be imported and then the callable will be run by the default implementation of [`sys.breakpointhook()`](../library/sys.xhtml#sys.breakpointhook "sys.breakpointhook") which itself is called by built-in [`breakpoint()`](../library/functions.xhtml#breakpoint "breakpoint"). If not set, or set to the empty string, it is equivalent to the value "pdb.set\_trace". Setting this to the string "0" causes the default implementation of [`sys.breakpointhook()`](../library/sys.xhtml#sys.breakpointhook "sys.breakpointhook")to do nothing but return immediately. 3\.7 新版功能. `PYTHONDEBUG`If this is set to a non-empty string it is equivalent to specifying the [`-d`](#cmdoption-d) option. If set to an integer, it is equivalent to specifying [`-d`](#cmdoption-d) multiple times. `PYTHONINSPECT`If this is set to a non-empty string it is equivalent to specifying the [`-i`](#cmdoption-i) option. This variable can also be modified by Python code using [`os.environ`](../library/os.xhtml#os.environ "os.environ")to force inspect mode on program termination. `PYTHONUNBUFFERED`If this is set to a non-empty string it is equivalent to specifying the [`-u`](#cmdoption-u) option. `PYTHONVERBOSE`If this is set to a non-empty string it is equivalent to specifying the [`-v`](#id4) option. If set to an integer, it is equivalent to specifying [`-v`](#id4) multiple times. `PYTHONCASEOK`If this is set, Python ignores case in [`import`](../reference/simple_stmts.xhtml#import) statements. This only works on Windows and OS X. `PYTHONDONTWRITEBYTECODE`If this is set to a non-empty string, Python won't try to write `.pyc`files on the import of source modules. This is equivalent to specifying the [`-B`](#id1) option. `PYTHONHASHSEED`If this variable is not set or set to `random`, a random value is used to seed the hashes of str, bytes and datetime objects. If [`PYTHONHASHSEED`](#envvar-PYTHONHASHSEED) is set to an integer value, it is used as a fixed seed for generating the hash() of the types covered by the hash randomization. Its purpose is to allow repeatable hashing, such as for selftests for the interpreter itself, or to allow a cluster of python processes to share hash values. The integer must be a decimal number in the range \[0,4294967295\]. Specifying the value 0 will disable hash randomization. 3\.2.3 新版功能. `PYTHONIOENCODING`If this is set before running the interpreter, it overrides the encoding used for stdin/stdout/stderr, in the syntax `encodingname:errorhandler`. Both the `encodingname` and the `:errorhandler` parts are optional and have the same meaning as in [`str.encode()`](../library/stdtypes.xhtml#str.encode "str.encode"). For stderr, the `:errorhandler` part is ignored; the handler will always be `'backslashreplace'`. 在 3.4 版更改: “encodingname” 部分現在是可選的。 在 3.6 版更改: On Windows, the encoding specified by this variable is ignored for interactive console buffers unless [`PYTHONLEGACYWINDOWSSTDIO`](#envvar-PYTHONLEGACYWINDOWSSTDIO) is also specified. Files and pipes redirected through the standard streams are not affected. `PYTHONNOUSERSITE`If this is set, Python won't add the [`user site-packages directory`](../library/site.xhtml#site.USER_SITE "site.USER_SITE") to [`sys.path`](../library/sys.xhtml#sys.path "sys.path"). 參見 [**PEP 370**](https://www.python.org/dev/peps/pep-0370) \[https://www.python.org/dev/peps/pep-0370\] -- 分用戶的 site-packages 目錄 `PYTHONUSERBASE`Defines the [`user base directory`](../library/site.xhtml#site.USER_BASE "site.USER_BASE"), which is used to compute the path of the [`user site-packages directory`](../library/site.xhtml#site.USER_SITE "site.USER_SITE")and [Distutils installation paths](../install/index.xhtml#inst-alt-install-user) for `python setup.py install --user`. 參見 [**PEP 370**](https://www.python.org/dev/peps/pep-0370) \[https://www.python.org/dev/peps/pep-0370\] -- 分用戶的 site-packages 目錄 `PYTHONEXECUTABLE`If this environment variable is set, `sys.argv[0]` will be set to its value instead of the value got through the C runtime. Only works on Mac OS X. `PYTHONWARNINGS`This is equivalent to the [`-W`](#cmdoption-w) option. If set to a comma separated string, it is equivalent to specifying [`-W`](#cmdoption-w) multiple times, with filters later in the list taking precedence over those earlier in the list. 最簡單的設置是將某個特定操作無條件地應用于進程所發出所有警告 (即使是在默認情況下會忽略的那些警告): ``` PYTHONWARNINGS=default # Warn once per call location PYTHONWARNINGS=error # Convert to exceptions PYTHONWARNINGS=always # Warn every time PYTHONWARNINGS=module # Warn once per calling module PYTHONWARNINGS=once # Warn once per Python process PYTHONWARNINGS=ignore # Never warn ``` 請參閱 [The Warnings Filter](../library/warnings.xhtml#warning-filter) 和 [Describing Warning Filters](../library/warnings.xhtml#describing-warning-filters) 了解更多細節。 `PYTHONFAULTHANDLER`If this environment variable is set to a non-empty string, [`faulthandler.enable()`](../library/faulthandler.xhtml#faulthandler.enable "faulthandler.enable") is called at startup: install a handler for `SIGSEGV`, `SIGFPE`, `SIGABRT`, `SIGBUS` and `SIGILL` signals to dump the Python traceback. This is equivalent to [`-X`](#id5)`faulthandler` option. 3\.3 新版功能. `PYTHONTRACEMALLOC`If this environment variable is set to a non-empty string, start tracing Python memory allocations using the [`tracemalloc`](../library/tracemalloc.xhtml#module-tracemalloc "tracemalloc: Trace memory allocations.") module. The value of the variable is the maximum number of frames stored in a traceback of a trace. For example, `PYTHONTRACEMALLOC=1` stores only the most recent frame. See the [`tracemalloc.start()`](../library/tracemalloc.xhtml#tracemalloc.start "tracemalloc.start") for more information. 3\.4 新版功能. `PYTHONPROFILEIMPORTTIME`If this environment variable is set to a non-empty string, Python will show how long each import takes. This is exactly equivalent to setting `-X importtime` on the command line. 3\.7 新版功能. `PYTHONASYNCIODEBUG`If this environment variable is set to a non-empty string, enable the [debug mode](../library/asyncio-dev.xhtml#asyncio-debug-mode) of the [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") module. 3\.4 新版功能. `PYTHONMALLOC`Set the Python memory allocators and/or install debug hooks. Set the family of memory allocators used by Python: - `default`: 使用 [default memory allocators](../c-api/memory.xhtml#default-memory-allocators). - `malloc`: use the `malloc()` function of the C library for all domains ([`PYMEM_DOMAIN_RAW`](../c-api/memory.xhtml#c.PYMEM_DOMAIN_RAW "PYMEM_DOMAIN_RAW"), [`PYMEM_DOMAIN_MEM`](../c-api/memory.xhtml#c.PYMEM_DOMAIN_MEM "PYMEM_DOMAIN_MEM"), [`PYMEM_DOMAIN_OBJ`](../c-api/memory.xhtml#c.PYMEM_DOMAIN_OBJ "PYMEM_DOMAIN_OBJ")). - `pymalloc`: use the [pymalloc allocator](../c-api/memory.xhtml#pymalloc) for [`PYMEM_DOMAIN_MEM`](../c-api/memory.xhtml#c.PYMEM_DOMAIN_MEM "PYMEM_DOMAIN_MEM") and [`PYMEM_DOMAIN_OBJ`](../c-api/memory.xhtml#c.PYMEM_DOMAIN_OBJ "PYMEM_DOMAIN_OBJ") domains and use the `malloc()` function for the [`PYMEM_DOMAIN_RAW`](../c-api/memory.xhtml#c.PYMEM_DOMAIN_RAW "PYMEM_DOMAIN_RAW") domain. 安裝調試鉤子: - `debug`: install debug hooks on top of the [default memory allocators](../c-api/memory.xhtml#default-memory-allocators). - `malloc_debug`: same as `malloc` but also install debug hooks - `pymalloc_debug`: same as `pymalloc` but also install debug hooks See the [default memory allocators](../c-api/memory.xhtml#default-memory-allocators) and the [`PyMem_SetupDebugHooks()`](../c-api/memory.xhtml#c.PyMem_SetupDebugHooks "PyMem_SetupDebugHooks") function (install debug hooks on Python memory allocators). 在 3.7 版更改: Added the `"default"` allocator. 3\.6 新版功能. `PYTHONMALLOCSTATS`If set to a non-empty string, Python will print statistics of the [pymalloc memory allocator](../c-api/memory.xhtml#pymalloc) every time a new pymalloc object arena is created, and on shutdown. This variable is ignored if the [`PYTHONMALLOC`](#envvar-PYTHONMALLOC) environment variable is used to force the `malloc()` allocator of the C library, or if Python is configured without `pymalloc` support. 在 3.6 版更改: This variable can now also be used on Python compiled in release mode. It now has no effect if set to an empty string. `PYTHONLEGACYWINDOWSFSENCODING`If set to a non-empty string, the default filesystem encoding and errors mode will revert to their pre-3.6 values of 'mbcs' and 'replace', respectively. Otherwise, the new defaults 'utf-8' and 'surrogatepass' are used. This may also be enabled at runtime with [`sys._enablelegacywindowsfsencoding()`](../library/sys.xhtml#sys._enablelegacywindowsfsencoding "sys._enablelegacywindowsfsencoding"). [可用性](../library/intro.xhtml#availability): Windows。 3\.6 新版功能: 有關更多詳細信息,請參閱 [**PEP 529**](https://www.python.org/dev/peps/pep-0529) \[https://www.python.org/dev/peps/pep-0529\]。 `PYTHONLEGACYWINDOWSSTDIO`If set to a non-empty string, does not use the new console reader and writer. This means that Unicode characters will be encoded according to the active console code page, rather than using utf-8. This variable is ignored if the standard streams are redirected (to files or pipes) rather than referring to console buffers. [可用性](../library/intro.xhtml#availability): Windows。 3\.6 新版功能. `PYTHONCOERCECLOCALE`If set to the value `0`, causes the main Python command line application to skip coercing the legacy ASCII-based C and POSIX locales to a more capable UTF-8 based alternative. If this variable is *not* set (or is set to a value other than `0`), the `LC_ALL` locale override environment variable is also not set, and the current locale reported for the `LC_CTYPE` category is either the default `C` locale, or else the explicitly ASCII-based `POSIX` locale, then the Python CLI will attempt to configure the following locales for the `LC_CTYPE` category in the order listed before loading the interpreter runtime: - `C.UTF-8` - `C.utf8` - `UTF-8` If setting one of these locale categories succeeds, then the `LC_CTYPE`environment variable will also be set accordingly in the current process environment before the Python runtime is initialized. This ensures that in addition to being seen by both the interpreter itself and other locale-aware components running in the same process (such as the GNU `readline`library), the updated setting is also seen in subprocesses (regardless of whether or not those processes are running a Python interpreter), as well as in operations that query the environment rather than the current C locale (such as Python's own [`locale.getdefaultlocale()`](../library/locale.xhtml#locale.getdefaultlocale "locale.getdefaultlocale")). Configuring one of these locales (either explicitly or via the above implicit locale coercion) automatically enables the `surrogateescape`[error handler](../library/codecs.xhtml#error-handlers) for [`sys.stdin`](../library/sys.xhtml#sys.stdin "sys.stdin") and [`sys.stdout`](../library/sys.xhtml#sys.stdout "sys.stdout") ([`sys.stderr`](../library/sys.xhtml#sys.stderr "sys.stderr") continues to use `backslashreplace`as it does in any other locale). This stream handling behavior can be overridden using [`PYTHONIOENCODING`](#envvar-PYTHONIOENCODING) as usual. For debugging purposes, setting `PYTHONCOERCECLOCALE=warn` will cause Python to emit warning messages on `stderr` if either the locale coercion activates, or else if a locale that *would* have triggered coercion is still active when the Python runtime is initialized. Also note that even when locale coercion is disabled, or when it fails to find a suitable target locale, [`PYTHONUTF8`](#envvar-PYTHONUTF8) will still activate by default in legacy ASCII-based locales. Both features must be disabled in order to force the interpreter to use `ASCII` instead of `UTF-8` for system interfaces. [Availability](../library/intro.xhtml#availability): \*nix. 3\.7 新版功能: See [**PEP 538**](https://www.python.org/dev/peps/pep-0538) \[https://www.python.org/dev/peps/pep-0538\] for more details. `PYTHONDEVMODE`If this environment variable is set to a non-empty string, enable the CPython "development mode". See the [`-X`](#id5)`dev` option. 3\.7 新版功能. `PYTHONUTF8`If set to `1`, enables the interpreter's UTF-8 mode, where `UTF-8` is used as the text encoding for system interfaces, regardless of the current locale setting. This means that: > - [`sys.getfilesystemencoding()`](../library/sys.xhtml#sys.getfilesystemencoding "sys.getfilesystemencoding") returns `'UTF-8'` (the locale encoding is ignored). > - [`locale.getpreferredencoding()`](../library/locale.xhtml#locale.getpreferredencoding "locale.getpreferredencoding") returns `'UTF-8'` (the locale encoding is ignored, and the function's `do_setlocale` parameter has no effect). > - [`sys.stdin`](../library/sys.xhtml#sys.stdin "sys.stdin"), [`sys.stdout`](../library/sys.xhtml#sys.stdout "sys.stdout"), and [`sys.stderr`](../library/sys.xhtml#sys.stderr "sys.stderr") all use UTF-8 as their text encoding, with the `surrogateescape`[error handler](../library/codecs.xhtml#error-handlers) being enabled for [`sys.stdin`](../library/sys.xhtml#sys.stdin "sys.stdin")and [`sys.stdout`](../library/sys.xhtml#sys.stdout "sys.stdout") ([`sys.stderr`](../library/sys.xhtml#sys.stderr "sys.stderr") continues to use `backslashreplace` as it does in the default locale-aware mode) As a consequence of the changes in those lower level APIs, other higher level APIs also exhibit different default behaviours: > - Command line arguments, environment variables and filenames are decoded to text using the UTF-8 encoding. > - [`os.fsdecode()`](../library/os.xhtml#os.fsdecode "os.fsdecode") and [`os.fsencode()`](../library/os.xhtml#os.fsencode "os.fsencode") use the UTF-8 encoding. > - [`open()`](../library/functions.xhtml#open "open"), [`io.open()`](../library/io.xhtml#io.open "io.open"), and [`codecs.open()`](../library/codecs.xhtml#codecs.open "codecs.open") use the UTF-8 encoding by default. However, they still use the strict error handler by default so that attempting to open a binary file in text mode is likely to raise an exception rather than producing nonsense data. Note that the standard stream settings in UTF-8 mode can be overridden by [`PYTHONIOENCODING`](#envvar-PYTHONIOENCODING) (just as they can be in the default locale-aware mode). 如果設置為“0”,則解釋器以其默認的區域識別模式運行。 設置任何其他非空字符串會在解釋器初始化期間導致錯誤。 如果根本未設置此環境變量,則解釋器默認使用當前區域設置,*除非* 當前區域被標識為基于 ASCII 的舊式區域設置(如 [`PYTHONCOERCECLOCALE`](#envvar-PYTHONCOERCECLOCALE) 所述),并且區域強制轉換被禁用或失敗。 在此類舊式區域設置中,解釋器將默認啟用 UTF-8 模式,除非顯式地指定不這樣做。 也可以使用 [`-X`](#id5)`utf8` 選項。 [Availability](../library/intro.xhtml#availability): \*nix. 3\.7 新版功能: 有關更多詳細信息,請參閱 [**PEP 540**](https://www.python.org/dev/peps/pep-0540) \[https://www.python.org/dev/peps/pep-0540\] 。 ### 1.2.1. 調試模式變量 設置這些變量只會在Python的調試版本中產生影響,也就是說,如果Python配置了 `--with-pydebug` 構建選項。 `PYTHONTHREADDEBUG`如果設置,Python將打印線程調試信息。 `PYTHONDUMPREFS`如果設置,Python在關閉解釋器,及轉儲對象和引用計數后仍將保持活動。 ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](unix.xhtml "2. 在Unix平臺中使用Python") | - [上一頁](index.xhtml "安裝和使用 Python") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [安裝和使用 Python](index.xhtml) ? - $('.inline-search').show(0); | ? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看