samurai-jack07
samurai-jack07

Reputation: 49

Embedded Python install manually package without pip

I download and unzip python-3.9.6-embed-amd64.zip file from python.org . Then, I unzipped python39.zip and I found site-packages folder in there.I added manually pandas package from pypi. Lastly, I tried this command: python.exe setup.py install . But it doesnt work. How can I install manually a package embedded python ? I want to import that for my script.

Upvotes: 2

Views: 1870

Answers (2)

Roberto Lanuza
Roberto Lanuza

Reputation: 1

On Windows you can use a simple cmd file that install the embeddable and add the pip to the installation. Then you have the portable embedded with pip to install whatever new package. This example will use the windows tools like curl and tar and cache the embeddable zip and get-pip.py to accelerate future installations. Just providing a command line with a specific version and installation path like:

  Install_python_embeddable.cmd 3 11 8 python_env

The script can be this:

@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

Upvotes: 0

Luk&#225;š Netřeba
Luk&#225;š Netřeba

Reputation: 11

You can download manually a tar.gz file when you don't want to use pip from pypi.org. Then you just need it to extract it in Windows PowerShell: tar -xvzf C:\Users\USERNAME\Downloads\FILENAME.tar.gz -C C:\Users\USERNAME\Downloads\FILENAMEDIR as 1st path is from and 2nd one path to. Then you just navigate to the dir cd C:\Users\USERNAME\Downloads\FILENAMEDIR\FILENAME and run py setup.py install

Upvotes: 1

Related Questions