首頁 > 軟體

python 映象環境搭建總結

2022-09-24 14:01:08

取最小安裝包

使用python-3.7.3-embed-win32,注意對:python37._pth的修改,修改方法:

# uncomment_pth.py
from pathlib import Path
def main(path_cur):
    for p in path_cur.glob('*._pth'):
        if not (p.suffix == '._pth' and p.stem.startswith('python')):
            continue
        print('found: ' + str(p))
        f_lines = []
        with p.open() as f:
            for line in f.readlines():
                if line.startswith('#import site'):
                    line = line.split('#', 2)[1]
                f_lines.append(line)
        with p.open('w') as f:
            f.writelines(f_lines)
        return True
    return False

if __name__ == '__main__':
    path_cur = Path('.')
    main(path_cur)

使用get-pip.py安裝pip,並在有網終的情況下安裝好環境

::first_install_with_net.bat
 
set old_dir=%cd%
 
cd /d "%~dp0"
 
python uncomment_pth.py
python get-pip.py
pip install devpi devpi-web
 
cd /d "%old_dir%"
pause

使用pip打包好環境,注意打包get-pip.py

::pkg_py_env.bat
 
set req=..req_pkg_py_env.txt
set whs=..wheels
pip freeze >%req%
pip wheel -r %req% -w %whs%
pip wheel pip -w %whs%
pip wheel setuptools -w %whs%
pip wheel wheel -w %whs%
pip uninstall wheel -y
pip uninstall setuptools -y
:: pip 不能用pip解除安裝自己,需要用下列語句
python -m pip uninstall pip -y
rd /s /q Lib
rd /s /q Scripts
pause

壓縮清理後的環境,並傳入有網路的環境後,解壓基礎環境

::unpkg_py_env.bat
 
set req=..req_pkg_py_env.txt
set whs=..wheels
set get-pip=%~dp0get-pip.py
set uncomment=%~dp0uncomment_pth.py
python %uncomment%
python %get-pip% --no-index --find-links=%whs%
set PATH=%PATHON_HOME%Scripts
pip install  -r %req% --no-index --find-links=%whs%
 
pause

編寫設定環境變數的指令碼

:: py_env.bat
 
@echo off
set old_dir=%cd%
cd /d "%~dp0"
set PATHON_HOME=%cd%
set PATH=%PATHON_HOME%;%PATHON_HOME%Scripts;%PATH%
title Ptython Env:PATHON_HOME=%PATHON_HOME%
cd /d "%old_dir%"
 
if "%1" == "" call cmd 
 
REM @echo off
REM set PYTHONHOME=%cd%
REM set PATH=%PYTHONHOME%/Scripts;%PYTHONHOME%;%PATH%
 
REM set PYTHONPATH=%PYTHONHOME%/Lib
REM title %PYTHONHOME%python.exe
REM call cmd

安裝devpi-server服務,並初始化好環境,這裡需要依賴nssm,需要自行下載,注意nssm需要管理員許可權

::install_service.bat
 
set svr_name=devpi
set svr_dir=d:devip_server_datas
cd /d "%~dp0"
::用於獲取許可權
nssm
call py_env.bat
devpi-init --serverdir %svr_dir%
devpi-gen-config --serverdir %svr_dir%
nssm install %svr_name% "%cd%Scriptsdevpi-server.exe" --serverdir %svr_dir% --host 0.0.0.0
pause
nssm start %svr_name%
devpi use root/pypi --always-set-cfg=yes
pause

如果不需要了,則解除安裝相關服務

::remove_service.bat
 
net stop devpi
nssm remove devpi confirm
pause

映象更新問題

編寫基礎功能指令碼檔案, 並放到pythong.exe同級目錄

# tasks.py
 
import datetime
import io
import os
from invoke import task
from tqdm import tqdm
 
# @task
# def package(c):
#     dir_path = datetime.datetime.now().strftime('%Y_%m_%d-%H_%m_%S-devpi_server_datas')
#     c.run(f'devpi-export.exe --serverdir D:\devip_server_datas --include-mirrored-files {dir_path}')
#     c.run(fr'"C:Program FilesWinRARRar.exe" a {dir_path}.zip {dir_path}')
#     # print('hello')

@task(iterable=['p'])
def down(c, p):
    from pathlib import Path
    Path(r'tmp_whl_dirs').mkdir(exist_ok=True)
    c.run(fr'cd tmp_whl_dirs & pip download {" ".join(p)}')
@task
def up(c):
    dir_path = datetime.datetime.now().strftime('%Y_%m_%d-%H_%m_%S-devpi_server_datas')
    c.run(f'devpi-export.exe --serverdir D:\devip_server_datas --include-mirrored-files {dir_path}')
    c.run(fr'"C:Program FilesWinRARRar.exe" a {dir_path}.zip  {dir_path}')
 
    from ftplib import FTP
    ftp = FTP('xxx')
    ftp.login('xxx', 'xxxxx')
    # ftp.cwd('devip_server_datas')
 
    with open(dir_path + '.zip', 'rb') as f:
        f.seek(0, io.SEEK_END)
        file_size = f.tell()
        f.seek(0, io.SEEK_SET)
        with tqdm(unit='blocks', unit_scale=True, leave=True, miniters=1, desc='Uploading......',
                  total=file_size) as tqdm_instance:
            ftp.storbinary(f'STOR {dir_path}.zip', f, 2048, callback=lambda sent: tqdm_instance.update(len(sent)))
    c.run('net stop devpi')
    c.run('net start devpi')
        # ftp.storbinary(f'STOR {dir_path}.zip', f)
    # print('finished!!')
    # os.unlink(dir_path)

下載需要的包:

:: down.bat
@echo off
set old_dir=%cd%
cd /d "%~dp0"
set PATHON_HOME=%cd%
set PATH=%PATHON_HOME%;%PATHON_HOME%Scripts;%PATH%
title Ptython Env:PATHON_HOME=%PATHON_HOME%
 
:download
 
set /p down_pkgs=請輸入需要下載的包名,用空格隔開:
inv down -p "%down_pkgs%"
 
goto :download
pause

匯出需要映象的包,並上傳映象包到無網路的環境裡

:: up_load.bat
@echo off
set old_dir=%cd%
cd /d "%~dp0"
set PATHON_HOME=%cd%
set PATH=%PATHON_HOME%;%PATHON_HOME%Scripts;%PATH%
title Ptython Env:PATHON_HOME=%PATHON_HOME%
REM set /p down_pkgs=請輸入需要下載的包名 用空格隔開
inv up
pause

匯入映象

to do...

到此這篇關於python 映象環境搭建總結的文章就介紹到這了,更多相關python 映象搭建內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com