嵌入式Python手动安装包无需pip

问题描述 投票:0回答:2

我从 python.org 下载并解压 python-3.9.6-embed-amd64.zip 文件。然后,我解压了 python39.zip,并在其中找到了 site-packages 文件夹。我从 pypi 手动添加了 pandas 包。最后,我尝试了这个命令: python.exe setup.py install 。但这不起作用。如何手动安装嵌入 python 的包?我想将其导入到我的脚本中。

python import path python-embedding site-packages
2个回答
1
投票

当您不想使用 pypi.org 上的 pip 时,可以手动下载 tar.gz 文件。然后您只需要它在 Windows PowerShell 中提取它:

tar -xvzf C:\Users\USERNAME\Downloads\FILENAME.tar.gz -C C:\Users\USERNAME\Downloads\FILENAMEDIR
,因为第一个路径是 from,第二个路径是 to。 然后你只需导航到目录
cd C:\Users\USERNAME\Downloads\FILENAMEDIR\FILENAME
并运行
py setup.py install


0
投票

在Windows上,您可以使用一个简单的cmd文件,该文件使用Windows已安装的工具curl和tar,并将embeddable和get-pip.py存储在缓存中以加速新安装,只需提供一个命令行来询问特定版本和安装路径如:

  Install_python_embeddable.cmd 3 11 8 python_env

脚本可以是这样的:

@echo off
setlocal enableDelayedExpansion
pushd "%~dp0"

rem arguments
set major_v=%~1
set minor_v=%~2
set micro_v=%~3
set python_folder=%~4
set force_clean=%~5

rem default values
if [!major_v!]==[] set major_v=3
if [!minor_v!]==[] set minor_v=11
if [!micro_v!]==[] set micro_v=8
if [!python_folder!]==[] set python_folder=python_env
if NOT [!force_clean!]==[1] set force_clean=0

rem Composed variables
set python_version=!major_v!.!minor_v!.!micro_v!
set python_id=!major_v!!minor_v!
set python_artifact_file=python-!python_version!-embed-amd64.zip
set python_artifact_path=%~dp0\!python_artifact_file!
set cache_folder=%LOCALAPPDATA%\environmentInstall_packages\PYTHON_EMBEDDABLE

rem Check force clean
if [!force_clean!]==[1] (
    echo ++ Remove old embeddable installation [!python_folder!]
    rd /q /s !python_folder!  > nul 2>&1
)

if exist !python_folder!\Scripts\pip.exe (
    if exist !python_folder!\python!python_id!.pth (
        if exist !python_folder!\Python_!python_version!.txt  goto already_exist_python
    )
)
    rem Install an embedded python
    echo.&echo ++ Install the Python_!python_version! on !python_folder! with pip
    echo  - Based on: "https://docs.python.org/3/using/windows.html#windows-embeddable"

    echo.&echo   1- Remove old installation
    rd /q /s !python_folder!  > nul 2>&1

    echo.&echo   2- Check local the embedded version in local cache: !cache_folder!\!python_artifact_file!
    md !cache_folder! 2>nul
    if NOT EXIST !cache_folder!\!python_artifact_file! ((echo - - - - Not found in local cache) & goto download_python)
    echo + + + + Install from local cache
    copy /Y !cache_folder!\!python_artifact_file! !python_artifact_path!
    goto install_python

:download_python
    echo.&echo   3- Download the embedded version and save in local cache
    curl https://www.python.org/ftp/python/!python_version!/!python_artifact_file! -o !python_artifact_path!
    copy /Y !python_artifact_path! !cache_folder!\!python_artifact_file!

:install_python
    echo.&echo   4- Install the embedded version and expand it
    md !python_folder! 2>nul
    pushd !python_folder!
    tar -xf !python_artifact_path!
    popd
    del !python_artifact_path! > nul 2>&1


    echo.&echo   5- Rename !python_folder!\python!python_id!._pth as !python_folder!\python!python_id!.pth and made !python_folder!\DLLs
    move /Y !python_folder!\python!python_id!._pth !python_folder!\python!python_id!.pth
    md !python_folder!\DLLs 2>nul

    echo.&echo   6- Check local the get-pip.py in local cache: !cache_folder!\get-pip.py
    if NOT EXIST !cache_folder!\get-pip.py ((echo - - - - Not found in local cache) & goto download_get_pip)
    echo + + + + Install from local cache
    copy /Y !cache_folder!\get-pip.py get-pip.py
    goto install_get_pip

:download_get_pip
    echo.&echo   7- Download the pip installer and save in local cache
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    copy /Y get-pip.py !cache_folder!\get-pip.py

:install_get_pip
    echo.&echo   8- Execute the get-pip installer
    !python_folder!\python.exe get-pip.py --no-warn-script-location
    del get-pip.py > nul 2>&1

    echo.&echo   9- Update PIP
    !python_folder!\python.exe -m pip install --upgrade pip

    echo.&echo   10- Save version sentinel to let check 3 version numbers
    !python_folder!\python.exe --version > python_ver.txt
    set /P installed_python_version_raw=<python_ver.txt
    del python_ver.txt > nul 2>&1
    set "installed_python_version=!installed_python_version_raw: =_!"
    echo !installed_python_version!
    echo !installed_python_version! > !python_folder!\!installed_python_version!.txt

    echo.&echo --------------- Embedded python installed ---------------&echo.
:already_exist_python

popd
© www.soinside.com 2019 - 2024. All rights reserved.