继《即插即用Python--嵌入版 Python量身定制》之后,我用相同的方法在多个电脑上安装了独立运行环境,但是有几个问题, 一是pip cache被遗留在user data目录里,因此,单独删除python安装目录并不能清除遗留文件;二是pip国内镜像服务器设置麻烦;三是安装不同版本python嵌入版,都要从头来一遍太考验耐心,所以就搞了一个windows批处理文件,可以将指定目录下的python嵌入版本相关设置一步搞定。
rem installpy.bat @echo off if "%1"=="" goto exampl set curdir=%~dp0%1 echo ========================================= echo pyton install dir: %curdir% echo ========================================= set appdir=%appdata% echo you user data dir: %appdir% echo ========================================= if not exist %curdir%pipCache mkdir %curdir%pipCache if not exist %appdata%pip mkdir %appdata%pip echo [global] >%appdata%pippip.ini echo cache-dir = %curdir%pipCache >>%appdata%pippip.ini echo index-url=https://pypi.tuna.tsinghua.edu.cn/simple >>%appdata%pippip.ini echo [install] >>%appdata%pippip.ini echo use-mirrors=true >>%appdata%pippip.ini echo mirrors=https://pypi.tuna.tsinghua.edu.cn/simple >>%appdata%pippip.ini echo trusted-host=pypi.tuna.tsinghua.edu.cn >>%appdata%pippip.ini copy .get-pip.py %curdir%>nul echo python -m pip install %%1 %%2 -i https://mirrors.aliyun.com/pypi/simple >%curdir%ipip.bat echo set path=%%~dp0;%%~dp0Scripts;%%path%%>%curdir%ppy.bat echo python -V >>%curdir%ppy.bat cd %curdir% set pthNm= for /r %curdir% %%l in (*._pth) do set pthNm=%%l echo This python _pth file: %pthNm% call :edPth %pthNm% pause call ppy.bat python get-pip.py pip config set global.cache-dir "%curdir%pipCache" echo ========================================= echo that ok,good job! echo ========================================= goto :end :edPth @echo off set sv= (for /f "eol=; tokens=1,* delims= " %%i in (%1) do ( set sv=%%i if "!sv!"=="#import" set sv=import echo !sv! %%j if "!sv!"=="." echo. ))>test.txt type test.txt move /y test.txt %1 >nul goto :eof :exampl echo use: installpy py37 :end
使用此批处理文件步骤如下:
1、下载python embed版本zip文件。网址:Python Releases for Windows | Python.org
2、下载get-pip.py,网址: https://bootstrap.pypa.io/get-pip.py
3、将python embed版本解压缩到一个文件夹,比如 ,将其解压缩至 E:pythonpy37
4、将上述批处理文件installpy.bat和get-pip.py 拷贝到解压缩文件夹所在父目录,如上例的E:python
5、打开windows命令行,进入上述父目录E:python
6、执行批下得命令:
installpy.bat py37
7、批处理文件执行完毕后:
(1)为python embed安装pip包;
(2)你的pip cache被指定在python embed版本年在文件夹中的pipCache目录;并在用户%appdata%Roamingpip目录中建立pip.ini文件,写入cache设置和下述镜像服务器设置。
(3)指定https://pypi.tuna.tsinghua.edu.cn/simple 为镜像服务器;
(4)加入两个批处理文件:ppy.bat用于启动python;ipip.bat用于安装新的python包
8、现在可以在python embed版本目录中执行下述命令,启动python环境
ppy.bat
执行下述命令安装功能包,比如jupyter
ipip.bat jupyter



