程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

It is very convenient to use python embedded environment to run programs in Windows environment

編輯:Python

Windows環境下用pythonIt is very convenient to run programs in an embedded environment

    • 1. Default folder settings for embedded packages
    • 2. 建立python環境文件夾
    • 3. 添加pythonIndex package path
    • 4. 安裝pip和requirements
    • 5. 運行環境
    • 6. 完整的powershell install.bat 腳本

之前覺得windows電腦安裝的pythonThe environment is too messy and too impure,Or temporarily need to go to another office to run quicklypython環境比較麻煩,因此了解了一下python嵌入式版本.上一期windows環境下安裝多個任意版本的python環境Teach you how to build onepythonA few basic steps for an embedded environment.String them together with a script in this issue.

My first thought is,安裝python基本環境 + 添加pythonPackage index path + 配置pip + 用pipDownload the required third-party libraries,最後運行測試.因此windowsThe script revolves around these steps.

1. Default folder settings for embedded packages

Folder settings are similar to projects,pkg是用於安裝python環境的配置文件,src是源代碼,testis the test script storage path

5. 目錄結構:根目錄 ---
|--- pkg (python環境安裝包)
|--- src (源代碼存放路徑)
|---test (Test script storage path)

2. 建立python環境文件夾

根據pycharm的習慣,The environment is generally installed in venv下面.Hence the command requiredmkdir,batThe command to script the new folder is md.We noticed that it is generally downloaded from the official websitepython Embedded packages arezip壓縮文件,So we also need to compress it with a compression script.我選擇的是winrar,Of course, in order to prevent everyone from not using this software,A judgment branch for manual decompression has also been added.

腳本如下:

echo\
echo Environmental inspection is underway......
if exist %head%\venv (
echo environment already exists,If you wish to reinstall or repair the environment,Please delete this folder and re-execute this file
pause
exit
)
if exist "D:\Program Files\WinRAR\winrar.exe" (
set localwinrar=D:\Program Files\WinRAR\winrar.exe
goto compressFileLine
) else (
echo There is no default pathwinrar文件...
echo Please enter on your computerwinrarExecutable file path and name
echo (如 C:\Program Files\WinRAR\winrar.exe)
set /p localwinrar=(如果沒有winrarPress Enter to decompress manually):
)
echo\
if "%localwinrar%" == "" (
echo Please manually unzip to %head%\venv\pythonafter this path
pause
goto bulidPythonBaseLine
)
:compressFileLine
echo\
echo Decompressing required files...
if exist %head%\pkg\python-3.9.13-embed-amd64.zip (
md %head%\venv
md %head%\venv\python
rem 測試代碼,Subsequent improvements are required
"%localwinrar%" e -or "%head%\pkg\python-3.9.13-embed-amd64.zip" "%head%\venv\python"
) else (
echo Critical files are missing,安裝失敗!!!
pause
exit
)
echo\

3. 添加pythonIndex package path

pythonThe index package path is made by python*._pth文件決定的,To support indexing of third-party libraries and indexing of your own code.我們需要編寫一個pythonThe script modifies thispython*._pth文件.
python腳本update-pth.py

import sys
with open("python39._pth", "a") as f:
f.writelines(["\n", "Lib\\site-packages\n", "..\n", "..\\..\\src\n"])

BAT腳本

:bulidPythonBaseLine
echo\
echo 正在構建pythonPackage index environment...
if exist %head%\venv\python (
echo\
) else (
echo No valid ones existpython路徑
pause
exit
)
cd %head%\venv\python
set pythonenv=%head%\venv\python
rem 執行一條命令,Assign the command result to pythonVersion
rem for /f "delims=" %%t in ('命令字符串') do set str=%%t
for /f "delims=" %%t in ('.\python.exe --version') do set pythonVersion=%%t
echo which you are currently usingpython環境%pythonVersion%
set pyv310=3.10
set pyv39=3.9
set pyv38=3.8
set pyv37=3.7
set pyv36=3.6
setLocal EnableDelayedExpansion
if not "x!pythonVersion:%pyv310%=!"=="x%pythonVersion%" (
echo 310
) else if not "x!pythonVersion:%pyv39%=!"=="x%pythonVersion%" (
echo The current index environment is Python3.9
rem Don't know why it is herebug,Sometimes you have to turn it on first and then turn it off,否則沒法執行
rem @echo on
rem @echo off
set fileName=python39._pth
echo Modify the index file%fileName%
%pythonenv%\python.exe %head%\pkg\update-pth.py %fileName%
) else if not "x!pythonVersion:%pyv38%=!"=="x%pythonVersion%" (
echo 38
) else if not "x!pythonVersion:%pyv37%=!"=="x%pythonVersion%" (
echo 37
) else if not "x!pythonVersion:%pyv36%=!"=="x%pythonVersion%" (
echo 36
) else (
echo 不支持的版本!!!
pause
exit
)
endlocal
echo\

4. 安裝pip和requirements

使用get-pip.py 安裝pip,The official script has been given,沒啥好說的,Copy and download it as a script.Then download according to the project needsrequirements中的內容,pipFor details on how to use it, please refer tohttps://blog.csdn.net/immerseinstudy/article/details/124072198

echo\
echo 正在安裝pip環境...
"%pythonenv%\python.exe" "%head%\pkg\get-pip.py" -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
echo Done
echo\
echo\
echo The installation package required by the project is being installed...
"%pythonenv%\Scripts\pip.exe" install -r "%head%\pkg\requirements.txt" -i https://pypi.tuna.tsinghua.edu.cn/simple/
cd %head%
echo Done
echo\

5. 運行環境

The method of running the environment is as follows,python.exe main.py.Just adding an absolute path.注意bash腳本可以通過pwd獲取當前路徑,而batThe current path of the script is stored in%cd%變量中.

echo\
echo 運行測試用例...
md "%head%\test"
"%pythonenv%\python.exe" "%head%\src\main.py"
echo 運行成功
echo\
echo 恭喜 ^_^!! 環境搭建完成~~

If necessary, I will put the complete embedded compressed package up,Because I don't know how to upload content to csdn..

6. 完整的powershell install.bat 腳本

@echo off
echo\
echo XXX軟件 -- 作者:XXX
echo\
echo ----------------------------------------
echo 嵌入式pythonEnvironment is being installed......
echo 【1】It needs to be installed in the local environment firstWinRAR環境
echo ----------------------------------------
set head=%cd%
echo\
echo Environmental inspection is underway......
if exist %head%\venv (
echo environment already exists,If you wish to reinstall or repair the environment,Please delete this folder and re-execute this file
pause
exit
)
if exist "D:\Program Files\WinRAR\winrar.exe" (
set localwinrar=D:\Program Files\WinRAR\winrar.exe
goto compressFileLine
) else (
echo There is no default pathwinrar文件...
echo Please enter on your computerwinrarExecutable file path and name
echo (如 C:\Program Files\WinRAR\winrar.exe)
set /p localwinrar=(如果沒有winrarPress Enter to decompress manually):
)
echo\
if "%localwinrar%" == "" (
echo Please manually unzip to %head%\venv\pythonafter this path
pause
goto bulidPythonBaseLine
)
:compressFileLine
echo\
echo Decompressing required files...
if exist %head%\pkg\python-3.9.13-embed-amd64.zip (
md %head%\venv
md %head%\venv\python
rem 測試代碼,Subsequent improvements are required
"%localwinrar%" e -or "%head%\pkg\python-3.9.13-embed-amd64.zip" "%head%\venv\python"
) else (
echo Critical files are missing,安裝失敗!!!
pause
exit
)
echo\
:bulidPythonBaseLine
echo\
echo 正在構建pythonPackage index environment...
if exist %head%\venv\python (
echo\
) else (
echo No valid ones existpython路徑
pause
exit
)
cd %head%\venv\python
set pythonenv=%head%\venv\python
rem 執行一條命令,Assign the command result to pythonVersion
rem for /f "delims=" %%t in ('命令字符串') do set str=%%t
for /f "delims=" %%t in ('.\python.exe --version') do set pythonVersion=%%t
echo which you are currently usingpython環境%pythonVersion%
set pyv310=3.10
set pyv39=3.9
set pyv38=3.8
set pyv37=3.7
set pyv36=3.6
setLocal EnableDelayedExpansion
if not "x!pythonVersion:%pyv310%=!"=="x%pythonVersion%" (
echo 310
) else if not "x!pythonVersion:%pyv39%=!"=="x%pythonVersion%" (
echo The current index environment is Python3.9
rem Don't know why it is herebug,Sometimes you have to turn it on first and then turn it off,否則沒法執行
rem @echo on
rem @echo off
set fileName=python39._pth
echo Modify the index file%fileName%
%pythonenv%\python.exe %head%\pkg\update-pth.py %fileName%
) else if not "x!pythonVersion:%pyv38%=!"=="x%pythonVersion%" (
echo 38
) else if not "x!pythonVersion:%pyv37%=!"=="x%pythonVersion%" (
echo 37
) else if not "x!pythonVersion:%pyv36%=!"=="x%pythonVersion%" (
echo 36
) else (
echo 不支持的版本!!!
pause
exit
)
endlocal
echo\
echo\
echo 正在安裝pip環境...
"%pythonenv%\python.exe" "%head%\pkg\get-pip.py" -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
echo Done
echo\
echo\
echo The installation package required by the project is being installed...
"%pythonenv%\Scripts\pip.exe" install -r "%head%\pkg\requirements.txt" -i https://pypi.tuna.tsinghua.edu.cn/simple/
cd %head%
echo Done
echo\
echo\
echo 運行測試用例...
md "%head%\test"
"%pythonenv%\python.exe" "%head%\src\main.py"
echo 運行成功
echo\
echo 恭喜 ^_^!! 環境搭建完成~~
pause

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved