Python3 For Windows 10
installer
安裝







隨后可以看到,installer 在用戶環境變量PATH中,添加了三項:

卸載
使用 installer 卸載 python 時,並不會移除 launcher。若要移除 launcher,需要打開“程序和功能”,在列表中選擇刪除。另外,python 也可以通過這里刪除或修改。
簡單配置
UTF-8 mode
兩種方式:
- 設置環境變量 PYTHONUTF8 為 1;
- 在命令行中傳遞參數
-X utf8
即使該模式未開啟,在下面兩種情況下,仍將使用 UTF-8:
- Console I/O including standard I/O
- The filesystem encoding
Python Launcher
可使用 py -0p 列出可用的 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
Python 通常將它的庫放在安裝目錄下,默認的庫位於 {root}\Lib\,第三方庫位於 {root}\Lib\site-packages\。
._pth 和 .pth 文件
pyvenv.cfg 文件
Pip
config
子命令:
- 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:\never\Documents\Python\pip\cache
注意,雖然看起來 cache-dir 像是和 cache 命令相關的,但實際上它是一個通用選項(General Options),而非 Cache Options,所以,如果將其放在 [cache] 下,將不會產生任何效果。
virtualenv
參考 Virtualenv
Creators
venv
可將創建行為委托給 Python 標准庫中的 venv 模塊。virtualenv 將創建一個進程來調用該模塊,這在 Windows 上將是一筆不小的開銷。
builtin
virtualenv 本身便可以執行創建操作。
Seeders
種包指的是 pip,setuptools,wheel 三者中的一或多個。安裝種包,將使得你可以在創建的虛擬環境中安裝其它的包。對於種包的安裝,有兩種機制:
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:\never\Documents\Python\pypa\virtualenv
no_vcs_ignore = true
system_site_packages = true
no_periodic_update = true
activators = batch,powershell
wrapper
為了更清晰地管理虛擬環境,我自己寫了個簡單的 wrapper,而不是用哪些流行的復雜的 wrapper 來管理環境。
這個 wrapper 不僅提供了基本的對虛擬環境的統一管理,還實現了針對 IPython 和 Jupyter 的隔離,若想知道原理,可繼續閱讀下面的說明,並閱讀源碼。
項目地址為:PowerShell 上的 VirtualEnv 封裝器。
IPython
The IPython directory
IPython 將它的文件:配置、命令歷史、擴展,存放在~/.ipython/中,可使用 IPYTHONDIR 覆蓋這個默認值。
Jupyter
安裝
使用 pip 安裝即可:
pip install jupyter
這之后,Jupyter Notebook 便安裝好了,同時還會安裝 ipykernel,這樣 notebook 便可以使用 Python 語言了,若要使用其它語言,則需要安裝對應的 kernel。
Common Directories and File Locations
Jupyter 在幾個不同的位置,存放不同的文件,如配置、數據、運行時。可使用環境變量來自定義這些位置。
- data files (nbextensions, kernelspecs)
- runtime fiels (logs, pid files, connection files)
- configuration (config files, custom.js)
Configuration files
默認為 home 下的 .jupyter,可使用 JUPYTER_CONFIG_DIR 覆蓋這個默認值。
此外,它還會到 {sys.prefix}/etc/jupyter 和 %programdata%/jupyter下尋找配置文件。
Data files
默認為 %appdata%/jupyter,可使用 JUPYTER_DATA_DIR 覆蓋這個默認值。
此外,它還會到 {sys.prefix}/share/jupyter 和 %programdata%/jupyter 下尋找數據文件。
Runtime files
默認為用戶數據目錄下的 runtime 子目錄,即 %appdata%/jupyter/runtime,可使用 JUPYTER_RUNTIME_DIR 覆蓋這個默認值。
使用
我們敲擊 jupyter -h,可以看到最下面給出了可用的子命令:
Available subcommands:
bundlerextension console kernel kernelspec migrate nbconvert nbextension
notebook qtconsole run serverextension troubleshoot trust
Jupyter console
比 IPython 更強大的 Python Shell,它可以連接到一個已存在的 kernel 上。
這在安裝 jupyter 時,已經安裝好了,如果沒有,這樣來安裝:
pip install jupyter-console
使用 jupyter console 即可啟動一個 console。另外,可使用 jupyter kernelspec list 來列出可用的 kernel。
更多內容,及配置可參考 Jupyter console 6.0。
notebook
執行 jupyter notebook,將在瀏覽器中打開 notebook,並以當前路徑為根目錄。
qtconsole
同樣,該包在安裝 jupyter 時,就已經安裝好了,pip install qtconsole,但 pip 並沒有自動安裝 Qt 依賴。
在 Linux Debian's system 中,可使用 apt 安裝 Qt 依賴;
在 Windows 下,這里詳細說一下。
首先,執行 pip show QtPy,可以看到其描述為:
- Provides an abstraction layer on top of the various Qt bindings (PyQt5, PyQt4 and PySide) and additional custom QWidgets.
該包被 qtconsole 所依賴:
- Required-by: qtconsole
To Write,first,需要好好學習一下 Qt
更多內容,及配置可參考 The Qt Console for Jupyter — Jupyter Qt Console 5.1.0 documentation。
VScode
The following records how to play Python using VScode.
To Write
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.exein 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.prefixandsys.exec_prefixpoint to the base directory of the virtual environment, whereassys.base_prefixandsys.base_exec_prefixpoint to the non-virtual environment Python installation which was used to create the virtual environment. If a virtual environment is not active, thensys.prefixis the same assys.base_prefixandsys.exec_prefixis the same assys.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
distutilsconfiguration 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
activatescript 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 thePATHenvironment 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 ofPATH. 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 inPATH.
