好的,如果“便携式 Python”指的是 Python.org 提供的可嵌入 zip 文件,那么本指南解决了我的问题:https://michlstechblog.info/blog/python-install-python-with-pip-on-windows-by-the-embeddable-zip-file/ https://michlstechblog.info/blog/python-install-python-with-pip-on-windows-by-the-embeddable-zip-file/
这是以防离线时的文本:
要在 Windows 上安装 Python,请下载最新版本。在此示例中,Python 3.6.5。
将 zip 文件解压到一个目录,例如D:\python3.6.5。
要安装 pip,请下载最新版本get-pip https://bootstrap.pypa.io/get-pip.py到pythons安装路径并开始安装。
> d:\> cd /d D:\Python3.6.5 D:\Python3.6.5> python get-pip.py ...
> Installing collected packages: pip, setuptools, wheel Successfully
> installed pip-10.0.1 setuptools-39.2.0 wheel-0.31.1
如果您位于代理后面,请添加 –proxy 开关
D:\Python3.6.5> python get-pip.py --proxy="http://192.168.254.1:8888"
不幸的是,在默认配置中,您无法加载 pip 安装的任何模块,pip 本身也是如此。因为 sys.path 变量只包含 Python Zip 文件和 python 可执行文件所在的 python 目录的路径。
>>> import sys
>>> print(sys.path)
['D:\\Python3.6.5\\python36.zip', 'D:\\Python3.6.5']
>>> import pip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pip'
任何通过设置 PYTHONPATH 变量来扩展变量的尝试都会被忽略。根本原因是可嵌入的 zip 文件安装包包含一个文件 python36._pth,该文件覆盖了设置 sys.path 变量的所有其他可能性。 sys.path 包含 python 查找模块的所有目录。
要设置 sys.path 变量,请打开 _pth 文件并在文件的 和 处添加以下路径。将“D:\Python3.6.5”替换为您的安装目录。
D:\Python3.6.5
D:\Python3.6.5\DLLs
D:\Python3.6.5\lib
D:\Python3.6.5\lib\plat-win
D:\Python3.6.5\lib\site-packages
或者重命名 python36._pth 文件
D:\Python3.6.5> ren python36._pth python36._pth.save
并为当前用户设置 PYTHONPATH 环境变量。
setx PYTHONPATH "D:\Python3.6.5;D:\Python3.6.5\DLLs;D:\Python3.6.5\lib;D:\Python3.6.5\lib\plat-win;D:\Python3.6.5\lib\site-packages"
或对于整个系统
setx /M PYTHONPATH "D:\Python3.6.5;D:\Python3.6.5\DLLs;D:\Python3.6.5\lib;D:\Python3.6.5\lib\plat-win;D:\Python3.6.5\lib\site-packages"