Installer
The installer component provides cross-platform pip installation functionality, automatically detecting the best installation method for each operating system.
Core Functions
Install
go
func (m *Manager) Install() error
Installs pip on the system using the most appropriate method for the current platform.
Returns:
error
: Error if installation fails.
Installation Methods by Platform:
Windows
- ensurepip module:
python -m ensurepip --upgrade
- get-pip.py: Downloads and executes the official installer
- Chocolatey:
choco install python
(if available) - Scoop:
scoop install python
(if available)
macOS
- ensurepip module:
python3 -m ensurepip --upgrade
- Homebrew:
brew install python
(if available) - MacPorts:
port install python39 +universal
(if available) - get-pip.py: Downloads and executes the official installer
Linux
- System package manager:
- Ubuntu/Debian:
apt-get install python3-pip
- CentOS/RHEL:
yum install python3-pip
ordnf install python3-pip
- Arch:
pacman -S python-pip
- Alpine:
apk add py3-pip
- Ubuntu/Debian:
- ensurepip module:
python3 -m ensurepip --upgrade
- get-pip.py: Downloads and executes the official installer
Installation Detection
IsInstalled
go
func (m *Manager) IsInstalled() (bool, error)
Checks if pip is installed and accessible on the system.
Returns:
bool
:true
if pip is installed and working,false
otherwise.error
: Error if the check fails.
Detection Process:
- Check if pip executable exists in PATH
- Verify pip can be executed (
pip --version
) - Validate pip functionality (
pip list
)
GetVersion
go
func (m *Manager) GetVersion() (string, error)
Gets the installed pip version.
Returns:
string
: Pip version string (e.g., "23.2.1").error
: Error if version retrieval fails.
Platform-Specific Behavior
Windows Installation
go
// Windows-specific installation logic
func (i *Installer) installWindows() error {
// Try ensurepip first
if err := i.tryEnsurepip(); err == nil {
return nil
}
// Try get-pip.py
if err := i.tryGetPip(); err == nil {
return nil
}
// Try package managers
if err := i.tryChocolatey(); err == nil {
return nil
}
return i.tryScoop()
}
macOS Installation
go
// macOS-specific installation logic
func (i *Installer) installMacOS() error {
// Try ensurepip first
if err := i.tryEnsurepip(); err == nil {
return nil
}
// Try Homebrew
if err := i.tryHomebrew(); err == nil {
return nil
}
// Try MacPorts
if err := i.tryMacPorts(); err == nil {
return nil
}
return i.tryGetPip()
}
Linux Installation
go
// Linux-specific installation logic
func (i *Installer) installLinux() error {
// Try system package manager first
if err := i.trySystemPackageManager(); err == nil {
return nil
}
// Try ensurepip
if err := i.tryEnsurepip(); err == nil {
return nil
}
return i.tryGetPip()
}
Error Handling
The installer provides specific error types for different failure scenarios:
go
if err := manager.Install(); err != nil {
switch pip.GetErrorType(err) {
case pip.ErrorTypePythonNotFound:
fmt.Println("Python is not installed or not in PATH")
case pip.ErrorTypePermissionDenied:
fmt.Println("Permission denied - try running with elevated privileges")
case pip.ErrorTypeNetworkError:
fmt.Println("Network error - check internet connection")
case pip.ErrorTypeUnsupportedOS:
fmt.Println("Unsupported operating system")
default:
fmt.Printf("Installation failed: %v\n", err)
}
}
Advanced Usage
Custom Installation Options
go
// Configure custom installation behavior
config := &pip.Config{
PythonPath: "/usr/local/bin/python3.9", // Specific Python version
Timeout: 300 * time.Second, // Extended timeout
Retries: 5, // More retries
}
manager := pip.NewManager(config)
if err := manager.Install(); err != nil {
return err
}
Installation with Logging
go
// Enable detailed logging during installation
logger, err := pip.NewLogger(&pip.LoggerConfig{
Level: pip.LogLevelDebug,
Output: os.Stdout,
})
if err != nil {
return err
}
manager := pip.NewManager(nil)
manager.SetCustomLogger(logger)
// Installation will be logged in detail
if err := manager.Install(); err != nil {
return err
}
Offline Installation
go
// For environments without internet access
// Pre-download get-pip.py and use local file
config := &pip.Config{
ExtraOptions: map[string]string{
"get-pip-path": "/path/to/get-pip.py",
},
}
manager := pip.NewManager(config)
if err := manager.Install(); err != nil {
return err
}
Validation
Post-Installation Verification
go
func verifyInstallation(manager *pip.Manager) error {
// Check if pip is installed
installed, err := manager.IsInstalled()
if err != nil {
return fmt.Errorf("failed to check installation: %w", err)
}
if !installed {
return errors.New("pip installation verification failed")
}
// Check pip version
version, err := manager.GetVersion()
if err != nil {
return fmt.Errorf("failed to get version: %w", err)
}
fmt.Printf("Pip %s installed successfully\n", version)
// Test basic functionality
packages, err := manager.ListPackages()
if err != nil {
return fmt.Errorf("pip functionality test failed: %w", err)
}
fmt.Printf("Pip is working correctly (%d packages found)\n", len(packages))
return nil
}
Best Practices
Always check before installing:
goif installed, err := manager.IsInstalled(); err != nil || !installed { if err := manager.Install(); err != nil { return err } }
Handle platform differences gracefully:
goosType := pip.GetOSType() switch osType { case pip.OSWindows: // Windows-specific handling case pip.OSMacOS: // macOS-specific handling case pip.OSLinux: // Linux-specific handling }
Use appropriate timeouts:
goconfig := &pip.Config{ Timeout: 300 * time.Second, // 5 minutes for installation }
Verify installation success:
goif err := manager.Install(); err != nil { return err } // Verify the installation worked if err := verifyInstallation(manager); err != nil { return err }