文档介绍:该【2025年在Win10上玩Python3(上) 】是由【小屁孩】上传分享,文档一共【7】页,该文档可以免费在线阅读,需要了解更多关于【2025年在Win10上玩Python3(上) 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。 : .
长风破浪会有时,直挂云帆济沧海。——李白
在 Win10上玩Python3(上)
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% (当前⽤户的应⽤数据⽬录)中的 ;(⾼优先级)
launcher 所在⽬录下的 。
Finding modules
参考
Python 通常将它的库放在安装⽬录下,默认的库位于 {root}\Lib\,第三⽅库位于 {root}\Lib\site-packages\。
._pth 和 .pth ⽂件
⽂件
Pip
config
参考 ,
⼦命令:
list
edit
需要使⽤ --editor <editor> 指定所⽤编辑器,否则使⽤变量 VISUAL 或 EDITOR 指定。
很奇怪的是,传⼊ VScode 的路径,总是报错,⽽使⽤ notepad 却没有问题。
get
set
unset
debug
作⽤域选项:
Options Descriptions Locations
--globalUse the system-wide configuration file onlyC:\ProgramData\pip\
--user Use the user configuration file only
--site Use the current environment configuration file only
file
位置
Per-user
Default is %appdata%\pip\;
由于历史原因,%home%\pip\;
当然,也可以⾃定⼀个位置,设置环境变量 PIP_CONFIG_FILE 即可。
Inside a virtualenv
%virtual_env%\
Global
C:\ProgramData\pip\
哪⼀个
如果发现有多个配置⽂件,那么按以下顺序进⾏读取,且后者覆盖前者:
global
per-user
virtualenv-specific : .
以铜为镜,可以正衣冠;以古为镜,可以知兴替;以人为镜,可以明得失。——《旧唐书·魏征列传》
怎么写
The names of the settings are derived from the long command line option, . 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 =
Each subcommand can be configured optionally in its own section so that every global setting with the same name will be overridden; .
decreasing the timeout to 10 seconds when running the 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 file. This is applicable to appending options like --
find-links or --trusted-host , which can be written on multiple lines:
[global]
find-links =
[install]
find-links =
trusted-host =
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 -url
这将创建全局配置⽂件,并写⼊。
随后我直接在该⽂件中添加了⼀些配置项:
[global]
index-url =
cache-dir = D:\never\Documents\Python\pip\cache : .
老当益壮,宁移白首之心;穷且益坚,不坠青云之志。——唐·王勃
注意,虽然看起来 cache-dir 像是和 cache 命令相关的,但实际上它是⼀个通⽤选项(General Options),⽽⾮ Cache Options,所以,如果
将其放在 [cache] 下,将不会产⽣任何效果。
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 中为 a data folder used as cache by the virtualenv
%localappdata%\pypa\virtualenv
--read-only-app-data False 以只读模式使⽤ app data ⽂件夹
--reset-app-data False 移除 app data ⽂件夹(即使没有传递必需的 dest)
⼿动升级内置的 wheels(注意,这个不是特指⽤于种包的那个 wheel
--upgrade-embed-wheelsFalse
包)
-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
app-data 种包的安装⽅式。若选择 pip,则很多选项将⽆意义
--no-seed, --without-pipFalse 不安装种包
--no-download, --never-
True 不要从 PyPI 上下载最新的种包
download
--download False 从 PyPI 上下载最新的种包使⽤
种的版本,默认为与 virtualenv 绑定的那个,否则使⽤指定的版本(所
--pip, --setuptools, --wheelbundle
谓“绑定”,即为“内置”) : .
士不可以不弘毅,任重而道远。仁以为己任,不亦重乎?死而后已,不亦远乎? ——《论语》
--no-pip, --no-setuptools, --Options False Default 不安装种包 Description
no-wheel
--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\。
在 --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 的隔离,若想知道原理,可继续阅读下⾯的说
明,并阅读源码。
项⽬地址为:。
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)
c