如何让pip虚拟环境真正可移植?

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

我正在尝试将 Open WebUI 安装为 portable 安装,其中可以移动或重命名基本文件夹。然而,我在使虚拟环境真正可移植方面遇到了问题。

在安装时,我尝试使用动态路径,但不知何故,路径在安装过程中被硬编码。当我尝试在不同位置激活虚拟环境时,我收到错误:

Fatal error in launcher: Unable to create process using '"...original\path\venv\Scripts\python.exe"  "...new\path\venv\Scripts\open-webui.exe" serve': The system cannot find the file specified.

如何解决这个问题并使我的 pip 虚拟环境真正可移植

在此示例中,我使用两个批处理文件来安装打开 WebUI

设置.bat

@echo on
REM setup.bat - Run this first to create the Open-WebUI_portable environment
set BASE_DIR=%~dp0
set VENV_DIR=%BASE_DIR%venv
set PYTHON_DIR=%BASE_DIR%python
set PIP_DIR=%BASE_DIR%pip-cache
set APPDATA=%BASE_DIR%appdata
set USERPROFILE=%BASE_DIR%userprofile
set HOME=%BASE_DIR%home
set XDG_CACHE_HOME=%BASE_DIR%cache
set PYTHONUSERBASE=%BASE_DIR%pythonuser

echo Creating directories...
mkdir "%PIP_DIR%" 2>nul
mkdir "%APPDATA%" 2>nul
mkdir "%USERPROFILE%" 2>nul
mkdir "%HOME%" 2>nul
mkdir "%XDG_CACHE_HOME%" 2>nul
mkdir "%PYTHONUSERBASE%" 2>nul

:DOWNLOAD_PYTHON
echo Downloading Python...
curl -L "https://www.python.org/ftp/python/3.11.8/python-3.11.8-embed-amd64.zip" -o "%BASE_DIR%python.zip"
if not exist "%BASE_DIR%python.zip" (
    echo ERROR: Failed to download Python
    echo Would you like to retry? (Y/N^)
    choice /c yn /n
    if errorlevel 2 (
        echo Installation cancelled
        exit /b 1
    )
    goto DOWNLOAD_PYTHON
)

echo Extracting Python...
powershell -Command "Expand-Archive -Path '%BASE_DIR%python.zip' -DestinationPath '%PYTHON_DIR%' -Force"
if not exist "%PYTHON_DIR%\python.exe" (
    echo ERROR: Failed to extract Python
    pause
    exit /b 1
)

echo Configuring Python for pip...
del "%PYTHON_DIR%\python311._pth" 2>nul
(
echo python311.zip
echo .
echo %PYTHON_DIR%\Lib\site-packages
echo .
echo import site
) > "%PYTHON_DIR%\python311._pth"

:DOWNLOAD_GETPIP
echo Downloading get-pip.py...
curl -L "https://bootstrap.pypa.io/get-pip.py" -o "%BASE_DIR%get-pip.py"
if not exist "%BASE_DIR%get-pip.py" (
    echo ERROR: Failed to download get-pip.py
    echo You can try to download it manually from https://bootstrap.pypa.io/get-pip.py
    echo and place it in %BASE_DIR%get-pip.py
    echo.
    echo Would you like to retry the download? (Y/N^)
    choice /c yn /n
    if errorlevel 2 (
        echo Installation cancelled
        exit /b 1
    )
    goto DOWNLOAD_GETPIP
)

echo Installing pip...
"%PYTHON_DIR%\python.exe" "%BASE_DIR%get-pip.py" --no-warn-script-location
if errorlevel 1 (
    echo ERROR: Failed to install pip
    echo This is a critical error. Please ensure:
    echo 1. You have internet connection
    echo 2. Any antivirus is not blocking the installation
    echo 3. You are running this script as administrator
    pause
    exit /b 1
)

echo Installing virtualenv...
"%PYTHON_DIR%\Scripts\pip.exe" install virtualenv
if errorlevel 1 (
    echo ERROR: Failed to install virtualenv
    echo This is a critical error. Please check your internet connection
    pause
    exit /b 1
)

echo Creating virtual environment...
"%PYTHON_DIR%\Scripts\virtualenv.exe" "%VENV_DIR%"
if not exist "%VENV_DIR%\Scripts\activate.bat" (
    echo ERROR: Failed to create virtual environment
    pause
    exit /b 1
)

echo Installing Open WebUI...
call "%VENV_DIR%\Scripts\activate.bat"
"%VENV_DIR%\Scripts\pip.exe" install --cache-dir="%PIP_DIR%" open-webui
if errorlevel 1 (
    echo ERROR: Failed to install Open WebUI
    pause
    exit /b 1
)

echo Creating postactivate script for relocatable paths...
mkdir "%VENV_DIR%\Scripts\postactivate.d" 2>nul
(
echo @echo on
echo set BASE_DIR=%%~dp0
echo set APPDATA=%%BASE_DIR%%appdata
echo set LOCALAPPDATA=%%BASE_DIR%%localappdata
echo set USERPROFILE=%%BASE_DIR%%userprofile
echo set HOME=%%BASE_DIR%%home
echo set XDG_CACHE_HOME=%%BASE_DIR%%cache
echo set XDG_CONFIG_HOME=%%BASE_DIR%%config
echo set PYTHONUSERBASE=%%BASE_DIR%%pythonuser
echo set PIP_CACHE_DIR=%%BASE_DIR%%pip-cache
echo set TEMP=%%BASE_DIR%%temp
echo set TMP=%%BASE_DIR%%temp
) > "%VENV_DIR%\Scripts\postactivate.d\relocatable.bat"

echo Cleaning up...
del "%BASE_DIR%python.zip"
del "%BASE_DIR%get-pip.py"

echo Setup complete! Use run.bat to start Open WebUI
pause

run.bat

@echo on
set BASE_DIR=%~dp0
set VENV_DIR=%BASE_DIR%venv

REM Set all environment variables to keep files in Open-WebUI_portable directory
set APPDATA=%BASE_DIR%appdata
set LOCALAPPDATA=%BASE_DIR%localappdata
set USERPROFILE=%BASE_DIR%userprofile
set HOME=%BASE_DIR%home
set XDG_CACHE_HOME=%BASE_DIR%cache
set XDG_CONFIG_HOME=%BASE_DIR%config
set PYTHONUSERBASE=%BASE_DIR%pythonuser
set PIP_CACHE_DIR=%BASE_DIR%pip-cache
set TEMP=%BASE_DIR%temp
set TMP=%BASE_DIR%temp

REM Create necessary directories
mkdir "%APPDATA%" 2>nul
mkdir "%LOCALAPPDATA%" 2>nul
mkdir "%USERPROFILE%" 2>nul
mkdir "%HOME%" 2>nul
mkdir "%XDG_CACHE_HOME%" 2>nul
mkdir "%XDG_CONFIG_HOME%" 2>nul
mkdir "%PYTHONUSERBASE%" 2>nul
mkdir "%TEMP%" 2>nul


REM Activate virtual environment
call "%VENV_DIR%\Scripts\activate.bat"

REM Run postactivate script for relocatable paths
call "%VENV_DIR%\Scripts\postactivate.d\relocatable.bat"

REM Run Open WebUI
"%VENV_DIR%\Scripts\open-webui.exe" serve

REM Open browser to localhost:8080
start "" http://localhost:8080

pause

它最初工作正常,但是当我重命名或移动基本文件夹时,它停止工作。 错误:

F:\OpenWebUI-portable-NEW>call "F:\OpenWebUI-portable-NEW\venv\Scripts\activate.bat"
Fatal error in launcher: Unable to create process using '"F:\OpenWebUI-portable-OLD\venv\Scripts\python.exe"  "F:\OpenWebUI-portable-NEW\venv\Scripts\open-webui.exe" serve': The system cannot find the file specified.
python python-3.x pip virtualenv
1个回答
0
投票

根据文档:

虚拟环境(除其他外)是:

  • 用于包含支持项目(库或应用程序)所需的特定Python解释器以及软件库和二进制文件。默认情况下,它们与其他虚拟环境中的软件以及操作系统中安装的 Python 解释器和库隔离。
  • 包含在项目目录中的目录中,通常命名为 .venv 或 venv,或者位于许多虚拟环境的容器目录下,例如 ~/.virtualenvs。
  • 未签入 Git 等源代码控制系统。
  • 被认为是一次性的——应该很容易删除并从头开始重新创建它。您没有在环境中放置任何项目代码。
  • 不被视为可移动或可复制 - 您只需在目标位置重新创建相同的环境。

但是我发现了这个包venvctl,它用于以编程方式创建可移植的虚拟环境(尚未对此进行测试,因此使用风险自负)。

我也不知道这里的答案是否对你有帮助,尽管没有被接受的答案。

只是强调我的立场是文档的立场并在评论中陈述。

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