Win10 安裝 Python3 (上)

z發表於2021-06-03

Python3 For Windows 10

installer

參考 The full installer

安裝

隨後可以看到,installer 在使用者環境變數PATH中,新增了三項:

解除安裝

使用 installer 解除安裝 python 時,並不會移除 launcher。若要移除 launcher,需要開啟“程式和功能”,在列表中選擇刪除。同時,python 也可以通過這裡刪除或修改。

簡單配置

參考 Configuring Python

UTF-8 mode

兩種方式:

  • 設定環境變數 PYTHONUTF8 為 1;
  • 在命令列中傳遞引數 -X utf8

即使該模式未開啟,在下面兩種情況下,仍將使用 UTF-8:

  • Console I/O including standard I/O
  • The filesystem encoding

Python Launcher

參考 Python Launcher for Windows

一個用於選擇和執行不同版本 Python 的實用程式。

虛擬環境

如果沒有顯式給出 Python 版本,且啟用了虛擬環境(由標準庫 venv 模組或外部的 virtualenv 工具建立),那麼 launcher 將使用虛擬環境中的直譯器,而非全域性的。

檔案關聯

.py, .pyw, .pyc

只有安裝了 launcher,才會產生這些關聯。這意味著,我們可以雙擊執行一個指令碼檔案,而不用開啟命令列。

Shebang Lines

這個東西在 Nix 系統上是原生支援的,而在 Windows 上,由 launcher 來提供這一支援。

雖然很奇怪,但

#!/usr/bin/python

將啟用預設的 Python 版本,當然,也可以顯式地新增版本字尾,以啟用其它版本。

使用 /usr/bin/env 形式的 shebang 將會在 PATH 中搜尋 Python 可執行檔案,其行為,就如同 Nix 中的 env 程式一樣。

此外,也可以在 shebang 中向直譯器傳遞一些選項。

Customization

INI

  • %appdata%(當前使用者的應用資料目錄)中的 py.ini;(高優先順序)
  • launcher 所在目錄下的 py.ini

Finding modules

參考 Finding modules

Python 通常將它的庫放在安裝目錄下,預設的庫位於 {root}\Lib\,第三方庫位於 {root}\Lib\site-packages\

._pth.pth 檔案

pyvenv.cfg 檔案

Pip

config

參考 pip configConfiguration

子命令:

  • list
  • edit
    • 需要使用 --editor <editor> 指定所用編輯器,否則使用變數 VISUAL 或 EDITOR 指定。
    • 很奇怪的是,傳入 VScode 的路徑,總是報錯,而使用 notepad 卻沒有問題。
  • get
  • set
  • unset
  • debug

作用域選項:

Options Descriptions Locations
--global Use the system-wide configuration file only C:\ProgramData\pip\pip.ini
--user Use the user configuration file only
--site Use the current environment configuration file only

file

位置
Per-user
  • Default is %appdata%\pip\pip.ini
  • 由於歷史原因,%home%\pip\pip.ini也會被考慮;
  • 當然,也可以自定一個位置,設定環境變數 PIP_CONFIG_FILE 即可。
Inside a virtualenv

%virtual_env%\pip.ini

Global

C:\ProgramData\pip\pip.ini

哪一個

如果發現有多個配置檔案,那麼按以下順序進行讀取,且後者覆蓋前者:

  • global
  • per-user
  • virtualenv-specific
怎麼寫

The names of the settings are derived from the long command line option, e.g. if you want to use a different package index (--index-url) and set the HTTP timeout (--default-timeout) to 60 seconds your config file would look like this:

[global]
timeout = 60
index-url = https://download.zope.org/ppix

Each subcommand can be configured optionally in its own section so that every global setting with the same name will be overridden; e.g. decreasing the timeout to 10 seconds when running the freeze (pip freeze) command and using 60 seconds for all other commands is possible with:

[global]
timeout = 60

[freeze]
timeout = 10

Boolean options like --ignore-installed or --no-dependencies can be set like this:

[install]
ignore-installed = true
no-dependencies = yes

To enable the boolean options --no-compile, --no-warn-script-location and --no-cache-dir, falsy values have to be used:

[global]
no-cache-dir = false

[install]
no-compile = no
no-warn-script-location = false

For options which can be repeated like --verbose and --quiet, a non-negative integer can be used to represent the level to be specified:

[global]
quiet = 0
verbose = 2

It is possible to append values to a section within a configuration file such as the pip.ini file. This is applicable to appending options like --find-links or --trusted-host, which can be written on multiple lines:

[global]
find-links =
    http://download.example.com

[install]
find-links =
    http://mirror1.example.com
    http://mirror2.example.com

trusted-host =
    mirror1.example.com
    mirror2.example.com

This enables users to add additional values in the order of entry for such command line arguments.

環境變數

pip’s command line options can be set with environment variables using the format PIP_<UPPER_LONG_NAME> . Dashes (-) have to be replaced with underscores (_).

優先順序

命令列引數>環境變數>配置檔案

aboutMe

配置了一下映象源:

> pip config --global set global.index-url https://mirrors.bfsu.edu.cn/pypi/web/simple

這將建立全域性配置檔案,並寫入。

隨後我直接在該檔案中新增了一些配置項:

[global]
index-url = https://mirrors.bfsu.edu.cn/pypi/web/simple
cache-dir = D:\xxxxx\Documents\Python\pip\cache

注意,雖然看起來 cache-dir 像是和 cache 命令相關的,但實際上它是一個通用選項(General Options),而非 Cache Options,所以,如果將其放在 [cache] 下,將不會產生任何效果。

virtualenv

參考 Virtualenv

Creators

venv

可將建立行為委託給 Python 標準庫中的 venv 模組。virtualenv 將建立一個程式來呼叫該模組,這在 Windows 上將是一筆不小的開銷。

builtin

virtualenv 本身便可以執行建立操作。

Seeders

種包指的是 pipsetuptoolswheel 三者中的一或多個。安裝種包,將使得你可以在建立的虛擬環境中安裝其它的包。對於種包的安裝,有兩種機制:

pip

使用與 virtualenv 繫結的 pip 安裝種包,這將建立一個程式來執行之。

add-data

在使用者應用資料目錄下建立一個安裝映象,隨後要用到時,只需要簡單的連結或拷貝這些映象即可。對 Windows 來說,可能沒有使能 symlink,但即便是拷貝,也快得多了。

可以使用環境變數 VIRTUALENV_OVERRIDE_APP_DATA 指定 the seed cache 的位置。

Activators

在虛擬環境目錄下,Scripts資料夾中,有一些啟用指令碼。它們被用來修改 shell 的設定,以確保虛擬環境中的命令要優先於全域性路徑下的。

CLI interface

virtualenv 主要是一個命令列應用。預設的命令列標誌可以被配置檔案覆蓋,而環境變數又優先於配置。使用 --help 時,可以在幫助資訊的最後看到該標誌的值,以及是否預設。

Options Default Description
--app-data 平臺相關(win10 中為 %localappdata%\pypa\virtualenv a data folder used as cache by the virtualenv
--read-only-app-data False 以只讀模式使用 app data 資料夾
--reset-app-data False 移除 app data 資料夾(即使沒有傳遞必需的 dest)
--upgrade-embed-wheels False 手動升級內建的 wheels(注意,這個不是特指用於種包的那個 wheel 包)
-p, --python 安裝時使用的 python 可執行版本 使用哪個 python 可執行檔案來建立環境
--creator builtin if exist, else venv 用於建立環境的執行者
dest 要在哪裡建立
--clear False 如果目標目錄不為空,清除之
--no-vcs-ignore False 不要在目標目錄下建立 VCS 忽略檔案(如 .gitignore
--system-site-packages False 使得虛擬環境可以訪問到全域性的 site-packages
--symlinks True 可以 symlink,就不 copy
--copies, --always-copy False 總是 copy
--seeder app-data 種包的安裝方式。若選擇 pip,則很多選項將無意義
--no-seed, --without-pip False 不安裝種包
--no-download, --never-download True 不要從 PyPI 上下載最新的種包
--download False 從 PyPI 上下載最新的種包使用
--pip, --setuptools, --wheel bundle 種的版本,預設為與 virtualenv 繫結的那個,否則使用指定的版本(所謂“繫結”,即為“內建”)
--no-pip, --no-setuptools, --no-wheel False 不安裝種包
--no-periodic-update False 不對內建的 wheels 進行週期性(14天)更新
--symlink-app-data False 從 app data 中符號連結 python 包
--activators 全部 要生成哪些啟用器:bash, batch, cshell, fish, powershell, python, xonsh
--prompt (venv_name) 為該環境指定一個提示符字首

Configuration file

位置

pip 一樣,virtualenv 也使用標準的 ini 格式的配置檔案,預設為 %localappdata%\pypa\virtualenv\virtualenv.ini

--help 輸出的最後,可以看到配置檔案的位置。可使用 VIRTUALENV_CONFIG_FILE 自定義其位置。

怎麼寫

基於命令列選項,將左邊的 - 符號移除,使用 _ 替換 - 即可配置之。

aboutMe
[virtualenv]

app_data = D:\xxxxx\Documents\Python\pypa\virtualenv

no_vcs_ignore = true

system_site_packages = true

no_periodic_update = true

activators = batch

Misc

  • venv — Creation of virtual environments

Note: While symlinks are supported on Windows, they are not recommended. Of particular note is that double-clicking python.exe in File Explorer will resolve the symlink eagerly and ignore the virtual environment.

The created pyvenv.cfg file also includes the include-system-site-packages key, set to true if venv is run with the --system-site-packages option, false otherwise.

When a virtual environment is active, the VIRTUAL_ENV environment variable is set to the path of the virtual environment. This can be used to check if one is running inside a virtual environment.

... However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.

Note: A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

A virtual environment is a directory tree which contains Python executable files and other files which indicate that it is a virtual environment.

Common installation tools such as setuptools and pip work as expected with virtual environments. In other words, when a virtual environment is active, they install Python packages into the virtual environment without needing to be told to do so explicitly.

When a virtual environment is active (i.e., the virtual environment’s Python interpreter is running), the attributes sys.prefix and sys.exec_prefix point to the base directory of the virtual environment, whereas sys.base_prefix and sys.base_exec_prefix point to the non-virtual environment Python installation which was used to create the virtual environment. If a virtual environment is not active, then sys.prefix is the same as sys.base_prefix and sys.exec_prefix is the same as sys.base_exec_prefix (they all point to a non-virtual environment Python installation).

When a virtual environment is active, any options that change the installation path will be ignored from all distutils configuration files to prevent projects being inadvertently installed outside of the virtual environment.

When working in a command shell, users can make a virtual environment active by running an activate script in the virtual environment’s executables directory (the precise filename and command to use the file is shell-dependent), which prepends the virtual environment’s directory for executables to the PATH environment variable for the running shell. There should be no need in other circumstances to activate a virtual environment; scripts installed into virtual environments have a “shebang” line which points to the virtual environment’s Python interpreter. This means that the script will run with that interpreter regardless of the value of PATH. On Windows, “shebang” line processing is supported if you have the Python Launcher for Windows installed (this was added to Python in 3.3 - see PEP 397 for more details). Thus, double-clicking an installed script in a Windows Explorer window should run the script with the correct interpreter without there needing to be any reference to its virtual environment in PATH.


相關文章