fix installer for externally managed Python
This commit is contained in:
@@ -217,7 +217,7 @@ Windows PowerShell:
|
||||
irm https://raw.githubusercontent.com/HKUDS/nanobot/main/scripts/install.ps1 | iex
|
||||
```
|
||||
|
||||
The default command installs or upgrades `nanobot-ai` from PyPI, then starts `nanobot onboard --wizard`. If you finish the wizard and save the config, skip the manual initialize/configure steps below and go straight to **Test one message**.
|
||||
The default command installs or upgrades `nanobot-ai` from PyPI, then starts `nanobot onboard --wizard`. It avoids system-wide pip installs by using an active virtual environment, `uv`, `pipx`, or a managed venv under `~/.nanobot/venv`. If you finish the wizard and save the config, skip the manual initialize/configure steps below and go straight to **Test one message**.
|
||||
|
||||
To preview the plan without changing your environment, pass `--dry-run`; combine it with `--dev` when you want to preview the main-branch install.
|
||||
|
||||
@@ -241,18 +241,20 @@ sh -c "$(curl -fsSL https://raw.githubusercontent.com/HKUDS/nanobot/main/scripts
|
||||
|
||||
If you prefer to inspect the script first, open [`scripts/install.sh`](./scripts/install.sh) or [`scripts/install.ps1`](./scripts/install.ps1).
|
||||
|
||||
**Install from PyPI**
|
||||
|
||||
```bash
|
||||
python -m pip install nanobot-ai
|
||||
```
|
||||
|
||||
**Install with `uv`**
|
||||
|
||||
```bash
|
||||
uv tool install nanobot-ai
|
||||
```
|
||||
|
||||
**Install from PyPI with pip**
|
||||
|
||||
```bash
|
||||
python -m pip install nanobot-ai
|
||||
```
|
||||
|
||||
If pip reports `externally-managed-environment` on macOS or Linux, use the one-command installer, `uv tool install nanobot-ai`, `pipx install nanobot-ai`, or install inside a virtual environment.
|
||||
|
||||
**Install from source**
|
||||
|
||||
```bash
|
||||
|
||||
+12
-1
@@ -32,7 +32,7 @@ On Windows PowerShell:
|
||||
irm https://raw.githubusercontent.com/HKUDS/nanobot/main/scripts/install.ps1 | iex
|
||||
```
|
||||
|
||||
The default command installs or upgrades `nanobot-ai` from PyPI, then starts `nanobot onboard --wizard`. If you finish the wizard and save the config, skip the manual initialize/configure steps and go straight to [Check the Setup](#4-check-the-setup).
|
||||
The default command installs or upgrades `nanobot-ai` from PyPI, then starts `nanobot onboard --wizard`. It avoids system-wide pip installs by using an active virtual environment, `uv`, `pipx`, or a managed venv under `~/.nanobot/venv`. If you finish the wizard and save the config, skip the manual initialize/configure steps and go straight to [Check the Setup](#4-check-the-setup).
|
||||
|
||||
To preview the plan without changing your environment, pass `--dry-run`; combine it with `--dev` when you want to preview the main-branch install.
|
||||
|
||||
@@ -72,6 +72,8 @@ python -m pip install nanobot-ai
|
||||
nanobot --version
|
||||
```
|
||||
|
||||
Use pip only inside an environment you control. If pip reports `externally-managed-environment` on macOS or Linux, use the one-command installer, `uv tool install nanobot-ai`, `pipx install nanobot-ai`, or create a virtual environment first.
|
||||
|
||||
**Latest source checkout:**
|
||||
|
||||
```bash
|
||||
@@ -286,6 +288,8 @@ python -m pip install -U nanobot-ai
|
||||
nanobot --version
|
||||
```
|
||||
|
||||
If pip reports `externally-managed-environment`, upgrade with the same isolated method you used to install nanobot, such as `uv tool upgrade nanobot-ai`, `pipx upgrade nanobot-ai`, or the managed venv created by the one-command installer.
|
||||
|
||||
**uv:**
|
||||
|
||||
```bash
|
||||
@@ -293,6 +297,13 @@ uv tool upgrade nanobot-ai
|
||||
nanobot --version
|
||||
```
|
||||
|
||||
**pipx:**
|
||||
|
||||
```bash
|
||||
pipx upgrade nanobot-ai
|
||||
nanobot --version
|
||||
```
|
||||
|
||||
**Source checkout:**
|
||||
|
||||
```bash
|
||||
|
||||
@@ -76,7 +76,7 @@ An OpenRouter key usually starts with `sk-or-v1-`. Other providers use different
|
||||
|
||||
## 4. Install nanobot
|
||||
|
||||
The easiest path is the one-command installer. It installs or upgrades nanobot, then starts the setup wizard.
|
||||
The easiest path is the one-command installer. It installs or upgrades nanobot, then starts the setup wizard. On macOS and Linux it avoids system-wide pip installs by using an active virtual environment, `uv`, `pipx`, or a managed venv under `~/.nanobot/venv`.
|
||||
|
||||
**macOS / Linux**
|
||||
|
||||
@@ -110,14 +110,22 @@ sh -c "$(curl -fsSL https://raw.githubusercontent.com/HKUDS/nanobot/main/scripts
|
||||
& ([scriptblock]::Create((irm https://raw.githubusercontent.com/HKUDS/nanobot/main/scripts/install.ps1))) --dev
|
||||
```
|
||||
|
||||
If the command says `curl` or `irm` is not found, or it cannot download from GitHub, use the manual install command below.
|
||||
If the command says `curl` or `irm` is not found, or it cannot download from GitHub, use one of the manual install commands below.
|
||||
|
||||
If you prefer to install manually, run:
|
||||
If `uv` is installed, use:
|
||||
|
||||
```bash
|
||||
uv tool install nanobot-ai
|
||||
```
|
||||
|
||||
If you prefer pip, use it only inside an environment you control:
|
||||
|
||||
```bash
|
||||
python -m pip install nanobot-ai
|
||||
```
|
||||
|
||||
If pip reports `externally-managed-environment` on macOS or Linux, go back to the one-command installer, use `uv tool install nanobot-ai`, use `pipx install nanobot-ai`, or create a virtual environment first.
|
||||
|
||||
Then check that nanobot is installed:
|
||||
|
||||
```bash
|
||||
|
||||
@@ -65,13 +65,13 @@ Use the same Python command for install checks and module fallback. On macOS/Lin
|
||||
| Symptom | Check |
|
||||
|---|---|
|
||||
| `python: command not found` | Try `python3 --version` on macOS/Linux or `py --version` on Windows. Then replace `python` in docs commands with the command that worked. |
|
||||
| `curl: command not found` | The macOS/Linux one-command installer could not download the script. Install curl, or use manual install: `python -m pip install nanobot-ai`, replacing `python` with `python3` if needed. |
|
||||
| `irm` is not recognized | PowerShell could not run the download helper. Use manual install: `python -m pip install nanobot-ai`, or `py -m pip install nanobot-ai` on Windows. |
|
||||
| `curl: command not found` | The macOS/Linux one-command installer could not download the script. Install curl, or use a manual isolated install such as `uv tool install nanobot-ai` or `pipx install nanobot-ai`. |
|
||||
| `irm` is not recognized | PowerShell could not run the download helper. Use manual install: `uv tool install nanobot-ai`, `pipx install nanobot-ai`, or `py -m pip install nanobot-ai` inside an environment you control. |
|
||||
| Could not download `raw.githubusercontent.com` | Your network, proxy, or firewall blocked the installer script download. Use manual install from PyPI, or configure your proxy and rerun the command. |
|
||||
| `nanobot: command not found` | Use the module form, for example `python -m nanobot ...`, `python3 -m nanobot ...`, or `py -m nanobot ...`. Reinstall with the same Python command, or add that Python's scripts directory to `PATH`. |
|
||||
| `No module named nanobot` | You are running a different Python than the one used for installation. Run `python -m pip show nanobot-ai`, `python3 -m pip show nanobot-ai`, or `py -m pip show nanobot-ai`, matching the command that installed nanobot. |
|
||||
| `pip is not available` | The installer tries `python -m ensurepip --upgrade` first. If that fails, install pip for that Python, or use a Python installer/distribution that includes pip. |
|
||||
| `externally-managed-environment` | Your system Python blocks global pip installs. The one-command installer retries with `--user`; if that still fails, create a virtual environment or install with `uv`/`pipx`. |
|
||||
| `pip is not available` | When the installer uses a virtual environment, it tries `python -m ensurepip --upgrade`. If that fails, install pip for that Python, or use a Python installer/distribution that includes pip. |
|
||||
| `externally-managed-environment` | Your system Python blocks global pip installs. Use the one-command installer, `uv tool install nanobot-ai`, `pipx install nanobot-ai`, or create a virtual environment; do not add `--break-system-packages` for nanobot. |
|
||||
| Installer chose the wrong Python | Set `PYTHON` before running the installer, such as `PYTHON=python3 sh -c "$(curl -fsSL https://raw.githubusercontent.com/HKUDS/nanobot/main/scripts/install.sh)"` or `$env:PYTHON="py"` before the PowerShell command. |
|
||||
| Editable source install does not update | From the repo root, run `python -m pip install -e .` again with the Python command used for development, then check `python -m nanobot --version` or `nanobot --version`. |
|
||||
| WebUI build tools missing | They are only needed for WebUI development. Packaged installs already include the WebUI bundle. |
|
||||
|
||||
+180
-35
@@ -11,6 +11,9 @@ $Package = "nanobot-ai"
|
||||
$MainSource = "https://github.com/HKUDS/nanobot/archive/refs/heads/main.zip"
|
||||
$InstallTarget = $Package
|
||||
$InstallSource = "PyPI"
|
||||
$script:NanobotRunner = $null
|
||||
$script:NanobotPython = $null
|
||||
$script:LastInstallSucceeded = $false
|
||||
|
||||
function Write-Info {
|
||||
param([string]$Message)
|
||||
@@ -23,13 +26,15 @@ function Fail {
|
||||
}
|
||||
|
||||
function Show-InstallFailureHint {
|
||||
[Console]::Error.WriteLine("Error: pip could not install nanobot from $InstallSource.")
|
||||
[Console]::Error.WriteLine("If pip mentioned externally-managed-environment, install in a virtual environment or use uv/pipx.")
|
||||
[Console]::Error.WriteLine("Error: could not install nanobot from $InstallSource.")
|
||||
[Console]::Error.WriteLine("If pip mentioned externally-managed-environment, use uv, pipx, or a virtual environment instead of system pip.")
|
||||
[Console]::Error.WriteLine("You can also run manually:")
|
||||
[Console]::Error.WriteLine(" $Python -m pip install --upgrade $InstallTarget")
|
||||
[Console]::Error.WriteLine(" uv tool install --force --upgrade $InstallTarget")
|
||||
[Console]::Error.WriteLine(" $Python -m venv `$HOME\.nanobot\venv")
|
||||
[Console]::Error.WriteLine(" `$HOME\.nanobot\venv\Scripts\python.exe -m pip install --upgrade $InstallTarget")
|
||||
[Console]::Error.WriteLine("Then start setup with:")
|
||||
[Console]::Error.WriteLine(" $Python -m nanobot onboard --wizard")
|
||||
throw "pip could not install nanobot from $InstallSource"
|
||||
[Console]::Error.WriteLine(" nanobot onboard --wizard")
|
||||
throw "could not install nanobot from $InstallSource"
|
||||
}
|
||||
|
||||
function Show-Usage {
|
||||
@@ -72,6 +77,131 @@ function Find-Python {
|
||||
Fail "Python 3.11 or newer was not found. Install Python first, then rerun this command."
|
||||
}
|
||||
|
||||
function Test-VirtualEnv {
|
||||
param([string]$Command)
|
||||
try {
|
||||
& $Command -c "import sys; raise SystemExit(0 if sys.prefix != sys.base_prefix else 1)" *> $null
|
||||
return $LASTEXITCODE -eq 0
|
||||
} catch {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Ensure-Pip {
|
||||
param([string]$Command)
|
||||
|
||||
try {
|
||||
& $Command -m pip --version *> $null
|
||||
} catch {}
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
return
|
||||
}
|
||||
|
||||
Write-Info "pip was not found for $Command. Trying ensurepip..."
|
||||
& $Command -m ensurepip --upgrade *> $null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Fail "pip is not available. Install pip for $Command, then rerun this command."
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-Nanobot {
|
||||
param([string[]]$NanobotArgs)
|
||||
|
||||
switch ($script:NanobotRunner) {
|
||||
"uv" {
|
||||
& uv tool run --from $InstallTarget nanobot @NanobotArgs
|
||||
}
|
||||
"pipx" {
|
||||
& pipx run --spec $InstallTarget nanobot @NanobotArgs
|
||||
}
|
||||
"python" {
|
||||
& $script:NanobotPython -m nanobot @NanobotArgs
|
||||
}
|
||||
default {
|
||||
Fail "nanobot was installed, but no runner was configured."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Get-NanobotCommand {
|
||||
switch ($script:NanobotRunner) {
|
||||
"uv" { return "uv tool run --from $InstallTarget nanobot" }
|
||||
"pipx" { return "pipx run --spec $InstallTarget nanobot" }
|
||||
"python" { return "$script:NanobotPython -m nanobot" }
|
||||
default { return "nanobot" }
|
||||
}
|
||||
}
|
||||
|
||||
function Install-WithActivePython {
|
||||
Write-Info "Detected an active virtual environment. Installing into it..."
|
||||
Ensure-Pip $Python
|
||||
& $Python -m pip install --upgrade $InstallTarget
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Show-InstallFailureHint
|
||||
}
|
||||
$script:NanobotRunner = "python"
|
||||
$script:NanobotPython = $Python
|
||||
}
|
||||
|
||||
function Install-WithUv {
|
||||
$script:LastInstallSucceeded = $false
|
||||
Write-Info "Installing or upgrading nanobot from $InstallSource with uv tool..."
|
||||
& uv tool install --python $Python --force --upgrade $InstallTarget
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
return
|
||||
}
|
||||
$script:NanobotRunner = "uv"
|
||||
$script:LastInstallSucceeded = $true
|
||||
}
|
||||
|
||||
function Install-WithPipx {
|
||||
$script:LastInstallSucceeded = $false
|
||||
Write-Info "Installing or upgrading nanobot from $InstallSource with pipx..."
|
||||
& pipx install --python $Python --force $InstallTarget
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
return
|
||||
}
|
||||
$script:NanobotRunner = "pipx"
|
||||
$script:LastInstallSucceeded = $true
|
||||
}
|
||||
|
||||
function Install-WithManagedVenv {
|
||||
$HomeDir = if ($env:HOME) { $env:HOME } elseif ($env:USERPROFILE) { $env:USERPROFILE } else { $null }
|
||||
if (-not $HomeDir) {
|
||||
Fail "HOME is not set; cannot create a managed virtual environment."
|
||||
}
|
||||
|
||||
$VenvDir = if ($env:NANOBOT_VENV) { $env:NANOBOT_VENV } else { Join-Path $HomeDir ".nanobot\venv" }
|
||||
$VenvPython = Join-Path $VenvDir "Scripts\python.exe"
|
||||
|
||||
if (-not (Test-Path $VenvPython)) {
|
||||
Write-Info "Creating a dedicated virtual environment at $VenvDir..."
|
||||
$Parent = Split-Path -Parent $VenvDir
|
||||
if ($Parent) {
|
||||
New-Item -ItemType Directory -Force -Path $Parent *> $null
|
||||
}
|
||||
& $Python -m venv $VenvDir
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Show-InstallFailureHint
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Python $VenvPython)) {
|
||||
Fail "The managed venv uses Python older than 3.11. Remove it or set NANOBOT_VENV to a new path."
|
||||
}
|
||||
|
||||
Write-Info "Installing or upgrading nanobot from $InstallSource in $VenvDir..."
|
||||
Ensure-Pip $VenvPython
|
||||
& $VenvPython -m pip install --upgrade $InstallTarget
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Show-InstallFailureHint
|
||||
}
|
||||
|
||||
$script:NanobotRunner = "python"
|
||||
$script:NanobotPython = $VenvPython
|
||||
}
|
||||
|
||||
foreach ($Arg in $RemainingArgs) {
|
||||
switch ($Arg) {
|
||||
"--dev" {
|
||||
@@ -103,61 +233,76 @@ $Python = Find-Python
|
||||
$Version = & $Python --version
|
||||
Write-Info "Using Python: $Version"
|
||||
|
||||
try {
|
||||
& $Python -m pip --version *> $null
|
||||
} catch {}
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
if ($DryRun) {
|
||||
Write-Info "Dry run: pip was not found. Install would try: $Python -m ensurepip --upgrade"
|
||||
} else {
|
||||
Write-Info "pip was not found for this Python. Trying ensurepip..."
|
||||
& $Python -m ensurepip --upgrade *> $null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Fail "pip is not available. Install pip for $Python, then rerun this command."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($DryRun) {
|
||||
Write-Info "Dry run: would install or upgrade nanobot from $InstallSource."
|
||||
Write-Info "Dry run: would run: $Python -m pip install --upgrade $InstallTarget"
|
||||
Write-Info "Dry run: if that fails because system site-packages are not writable, would retry: $Python -m pip install --user --upgrade $InstallTarget"
|
||||
if (Test-VirtualEnv $Python) {
|
||||
Write-Info "Dry run: active virtual environment detected; would run: $Python -m pip install --upgrade $InstallTarget"
|
||||
Write-Info "Dry run: would run nanobot as: $Python -m nanobot"
|
||||
} elseif (Get-Command uv -ErrorAction SilentlyContinue) {
|
||||
Write-Info "Dry run: would run: uv tool install --python $Python --force --upgrade $InstallTarget"
|
||||
Write-Info "Dry run: would run nanobot as: uv tool run --from $InstallTarget nanobot"
|
||||
} elseif (Get-Command pipx -ErrorAction SilentlyContinue) {
|
||||
Write-Info "Dry run: would run: pipx install --python $Python --force $InstallTarget"
|
||||
Write-Info "Dry run: would run nanobot as: pipx run --spec $InstallTarget nanobot"
|
||||
} else {
|
||||
$HomeDir = if ($env:HOME) { $env:HOME } elseif ($env:USERPROFILE) { $env:USERPROFILE } else { "~" }
|
||||
$VenvDir = if ($env:NANOBOT_VENV) { $env:NANOBOT_VENV } else { Join-Path $HomeDir ".nanobot\venv" }
|
||||
Write-Info "Dry run: would create or reuse a dedicated virtual environment: $VenvDir"
|
||||
Write-Info "Dry run: would run: $VenvDir\Scripts\python.exe -m pip install --upgrade $InstallTarget"
|
||||
Write-Info "Dry run: would run nanobot as: $VenvDir\Scripts\python.exe -m nanobot"
|
||||
}
|
||||
if ($env:NANOBOT_SKIP_WIZARD -eq "1") {
|
||||
Write-Info "Dry run: would skip setup wizard because NANOBOT_SKIP_WIZARD=1."
|
||||
} else {
|
||||
Write-Info "Dry run: would run: $Python -m nanobot onboard --wizard"
|
||||
Write-Info "Dry run: would run the setup wizard."
|
||||
}
|
||||
Write-Info "Dry run: no changes made."
|
||||
return
|
||||
}
|
||||
|
||||
Write-Info "Installing or upgrading nanobot from $InstallSource..."
|
||||
& $Python -m pip install --upgrade $InstallTarget
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Info "Install failed. Retrying as a user install..."
|
||||
& $Python -m pip install --user --upgrade $InstallTarget
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Show-InstallFailureHint
|
||||
if (Test-VirtualEnv $Python) {
|
||||
Install-WithActivePython
|
||||
} else {
|
||||
$Installed = $false
|
||||
|
||||
if (Get-Command uv -ErrorAction SilentlyContinue) {
|
||||
Install-WithUv
|
||||
$Installed = $script:LastInstallSucceeded
|
||||
if (-not $Installed) {
|
||||
Write-Info "uv tool install failed. Trying the next isolated install method..."
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $Installed -and (Get-Command pipx -ErrorAction SilentlyContinue)) {
|
||||
Install-WithPipx
|
||||
$Installed = $script:LastInstallSucceeded
|
||||
if (-not $Installed) {
|
||||
Write-Info "pipx install failed. Trying the managed virtual environment..."
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $Installed) {
|
||||
Write-Info "Using a dedicated virtual environment to avoid system pip."
|
||||
Install-WithManagedVenv
|
||||
}
|
||||
}
|
||||
|
||||
Write-Info "Installed nanobot:"
|
||||
& $Python -m nanobot --version
|
||||
Invoke-Nanobot @("--version")
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Fail "nanobot was installed, but the command could not be started."
|
||||
}
|
||||
|
||||
if ($env:NANOBOT_SKIP_WIZARD -eq "1") {
|
||||
Write-Info "Skipping setup wizard because NANOBOT_SKIP_WIZARD=1."
|
||||
Write-Info "Run this later: $Python -m nanobot onboard --wizard"
|
||||
Write-Info "Run this later: $(Get-NanobotCommand) onboard --wizard"
|
||||
return
|
||||
}
|
||||
|
||||
Write-Info "Starting setup wizard..."
|
||||
& $Python -m nanobot onboard --wizard
|
||||
Invoke-Nanobot @("onboard", "--wizard")
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Fail "Setup wizard did not complete."
|
||||
}
|
||||
|
||||
Write-Info "Done. Try: $Python -m nanobot agent -m `"Hello!`""
|
||||
Write-Info "Done. Try: $(Get-NanobotCommand) agent -m `"Hello!`""
|
||||
|
||||
+170
-24
@@ -6,6 +6,8 @@ main_source="https://github.com/HKUDS/nanobot/archive/refs/heads/main.zip"
|
||||
install_target="$package"
|
||||
install_source="PyPI"
|
||||
dry_run="0"
|
||||
nanobot_runner=""
|
||||
nanobot_python=""
|
||||
|
||||
info() {
|
||||
printf '%s\n' "$*"
|
||||
@@ -17,12 +19,14 @@ fail() {
|
||||
}
|
||||
|
||||
install_failure_hint() {
|
||||
printf '%s\n' "Error: pip could not install nanobot from $install_source." >&2
|
||||
printf '%s\n' "If pip mentioned externally-managed-environment, install in a virtual environment or use uv/pipx." >&2
|
||||
printf '%s\n' "Error: could not install nanobot from $install_source." >&2
|
||||
printf '%s\n' "If pip mentioned externally-managed-environment, use uv, pipx, or a virtual environment instead of system pip." >&2
|
||||
printf '%s\n' "You can also run manually:" >&2
|
||||
printf ' %s\n' "$python_bin -m pip install --upgrade $install_target" >&2
|
||||
printf ' %s\n' "uv tool install --force --upgrade $install_target" >&2
|
||||
printf ' %s\n' "$python_bin -m venv ~/.nanobot/venv" >&2
|
||||
printf ' %s\n' "~/.nanobot/venv/bin/python -m pip install --upgrade $install_target" >&2
|
||||
printf '%s\n' "Then start setup with:" >&2
|
||||
printf ' %s\n' "$python_bin -m nanobot onboard --wizard" >&2
|
||||
printf ' %s\n' "nanobot onboard --wizard" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -52,6 +56,123 @@ PY
|
||||
return 1
|
||||
}
|
||||
|
||||
python_is_virtual_env() {
|
||||
"$python_bin" - <<'PY'
|
||||
import sys
|
||||
raise SystemExit(0 if sys.prefix != sys.base_prefix else 1)
|
||||
PY
|
||||
}
|
||||
|
||||
ensure_pip() {
|
||||
target_python="$1"
|
||||
if "$target_python" -m pip --version >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
info "pip was not found for $target_python. Trying ensurepip..."
|
||||
"$target_python" -m ensurepip --upgrade >/dev/null 2>&1
|
||||
}
|
||||
|
||||
run_nanobot() {
|
||||
case "$nanobot_runner" in
|
||||
uv)
|
||||
uv tool run --from "$install_target" nanobot "$@"
|
||||
;;
|
||||
pipx)
|
||||
pipx run --spec "$install_target" nanobot "$@"
|
||||
;;
|
||||
python)
|
||||
"$nanobot_python" -m nanobot "$@"
|
||||
;;
|
||||
*)
|
||||
fail "nanobot was installed, but no runner was configured"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
nanobot_try_command() {
|
||||
case "$nanobot_runner" in
|
||||
uv)
|
||||
printf '%s\n' "uv tool run --from $install_target nanobot"
|
||||
;;
|
||||
pipx)
|
||||
printf '%s\n' "pipx run --spec $install_target nanobot"
|
||||
;;
|
||||
python)
|
||||
printf '%s\n' "$nanobot_python -m nanobot"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
install_with_active_python() {
|
||||
info "Detected an active virtual environment. Installing into it..."
|
||||
ensure_pip "$python_bin" || return 1
|
||||
"$python_bin" -m pip install --upgrade "$install_target" || return 1
|
||||
nanobot_runner="python"
|
||||
nanobot_python="$python_bin"
|
||||
}
|
||||
|
||||
install_with_uv() {
|
||||
info "Installing or upgrading nanobot from $install_source with uv tool..."
|
||||
uv tool install --python "$python_bin" --force --upgrade "$install_target" || return 1
|
||||
nanobot_runner="uv"
|
||||
}
|
||||
|
||||
install_with_pipx() {
|
||||
info "Installing or upgrading nanobot from $install_source with pipx..."
|
||||
pipx install --python "$python_bin" --force "$install_target" || return 1
|
||||
nanobot_runner="pipx"
|
||||
}
|
||||
|
||||
write_managed_wrapper() {
|
||||
bin_dir="${NANOBOT_BIN_DIR:-$HOME/.local/bin}"
|
||||
wrapper="$bin_dir/nanobot"
|
||||
mkdir -p "$bin_dir" || return 0
|
||||
|
||||
if [ -e "$wrapper" ] && ! grep -q "Generated by nanobot installer" "$wrapper" 2>/dev/null; then
|
||||
info "Not updating $wrapper because it already exists."
|
||||
return 0
|
||||
fi
|
||||
|
||||
cat > "$wrapper" <<EOF
|
||||
#!/bin/sh
|
||||
# Generated by nanobot installer.
|
||||
exec "$nanobot_python" -m nanobot "\$@"
|
||||
EOF
|
||||
chmod +x "$wrapper" || return 0
|
||||
|
||||
if ! command -v nanobot >/dev/null 2>&1; then
|
||||
info "Installed a nanobot launcher at $wrapper."
|
||||
info "Add $bin_dir to PATH to run nanobot directly."
|
||||
fi
|
||||
}
|
||||
|
||||
install_with_managed_venv() {
|
||||
[ -n "${HOME:-}" ] || fail "HOME is not set; cannot create a managed virtual environment"
|
||||
|
||||
venv_dir="${NANOBOT_VENV:-$HOME/.nanobot/venv}"
|
||||
venv_python="$venv_dir/bin/python"
|
||||
|
||||
if [ ! -x "$venv_python" ]; then
|
||||
info "Creating a dedicated virtual environment at $venv_dir..."
|
||||
mkdir -p "$(dirname "$venv_dir")"
|
||||
"$python_bin" -m venv "$venv_dir" || return 1
|
||||
fi
|
||||
|
||||
"$venv_python" - <<'PY' >/dev/null 2>&1 || fail "The managed venv uses Python older than 3.11. Remove it or set NANOBOT_VENV to a new path."
|
||||
import sys
|
||||
raise SystemExit(0 if sys.version_info >= (3, 11) else 1)
|
||||
PY
|
||||
|
||||
info "Installing or upgrading nanobot from $install_source in $venv_dir..."
|
||||
ensure_pip "$venv_python" || return 1
|
||||
"$venv_python" -m pip install --upgrade "$install_target" || return 1
|
||||
|
||||
nanobot_runner="python"
|
||||
nanobot_python="$venv_python"
|
||||
write_managed_wrapper
|
||||
}
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
--dev)
|
||||
@@ -86,44 +207,69 @@ fi
|
||||
|
||||
info "Using Python: $("$python_bin" --version 2>&1)"
|
||||
|
||||
if ! "$python_bin" -m pip --version >/dev/null 2>&1; then
|
||||
if [ "$dry_run" = "1" ]; then
|
||||
info "Dry run: pip was not found. Install would try: $python_bin -m ensurepip --upgrade"
|
||||
else
|
||||
info "pip was not found for this Python. Trying ensurepip..."
|
||||
"$python_bin" -m ensurepip --upgrade >/dev/null 2>&1 || fail "pip is not available. Install pip for $python_bin, then rerun this command."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$dry_run" = "1" ]; then
|
||||
info "Dry run: would install or upgrade nanobot from $install_source."
|
||||
info "Dry run: would run: $python_bin -m pip install --upgrade $install_target"
|
||||
info "Dry run: if that fails because system site-packages are not writable, would retry: $python_bin -m pip install --user --upgrade $install_target"
|
||||
if python_is_virtual_env; then
|
||||
info "Dry run: active virtual environment detected; would run: $python_bin -m pip install --upgrade $install_target"
|
||||
info "Dry run: would run nanobot as: $python_bin -m nanobot"
|
||||
elif command -v uv >/dev/null 2>&1; then
|
||||
info "Dry run: would run: uv tool install --python $python_bin --force --upgrade $install_target"
|
||||
info "Dry run: would run nanobot as: uv tool run --from $install_target nanobot"
|
||||
elif command -v pipx >/dev/null 2>&1; then
|
||||
info "Dry run: would run: pipx install --python $python_bin --force $install_target"
|
||||
info "Dry run: would run nanobot as: pipx run --spec $install_target nanobot"
|
||||
else
|
||||
venv_dir="${NANOBOT_VENV:-$HOME/.nanobot/venv}"
|
||||
info "Dry run: would create or reuse a dedicated virtual environment: $venv_dir"
|
||||
info "Dry run: would run: $venv_dir/bin/python -m pip install --upgrade $install_target"
|
||||
info "Dry run: would run nanobot as: $venv_dir/bin/python -m nanobot"
|
||||
fi
|
||||
if [ "${NANOBOT_SKIP_WIZARD:-}" = "1" ]; then
|
||||
info "Dry run: would skip setup wizard because NANOBOT_SKIP_WIZARD=1."
|
||||
else
|
||||
info "Dry run: would run: $python_bin -m nanobot onboard --wizard"
|
||||
info "Dry run: would run the setup wizard."
|
||||
fi
|
||||
info "Dry run: no changes made."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
info "Installing or upgrading nanobot from $install_source..."
|
||||
if ! "$python_bin" -m pip install --upgrade "$install_target"; then
|
||||
info "Install failed. Retrying as a user install..."
|
||||
"$python_bin" -m pip install --user --upgrade "$install_target" || install_failure_hint
|
||||
if python_is_virtual_env; then
|
||||
install_with_active_python || install_failure_hint
|
||||
else
|
||||
installed="0"
|
||||
|
||||
if command -v uv >/dev/null 2>&1; then
|
||||
if install_with_uv; then
|
||||
installed="1"
|
||||
else
|
||||
info "uv tool install failed. Trying the next isolated install method..."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$installed" != "1" ] && command -v pipx >/dev/null 2>&1; then
|
||||
if install_with_pipx; then
|
||||
installed="1"
|
||||
else
|
||||
info "pipx install failed. Trying the managed virtual environment..."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$installed" != "1" ]; then
|
||||
info "Using a dedicated virtual environment to avoid system pip."
|
||||
install_with_managed_venv || install_failure_hint
|
||||
fi
|
||||
fi
|
||||
|
||||
info "Installed nanobot:"
|
||||
"$python_bin" -m nanobot --version
|
||||
run_nanobot --version
|
||||
|
||||
if [ "${NANOBOT_SKIP_WIZARD:-}" = "1" ]; then
|
||||
info "Skipping setup wizard because NANOBOT_SKIP_WIZARD=1."
|
||||
info "Run this later: $python_bin -m nanobot onboard --wizard"
|
||||
info "Run this later: $(nanobot_try_command) onboard --wizard"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
info "Starting setup wizard..."
|
||||
"$python_bin" -m nanobot onboard --wizard
|
||||
run_nanobot onboard --wizard
|
||||
|
||||
info "Done. Try: $python_bin -m nanobot agent -m \"Hello!\""
|
||||
info "Done. Try: $(nanobot_try_command) agent -m \"Hello!\""
|
||||
|
||||
Reference in New Issue
Block a user