0
# Package Installation
1
2
Comprehensive package installation capabilities including dependency resolution, version control integration, and support for multiple package sources and formats.
3
4
## Capabilities
5
6
### Basic Installation
7
8
Install packages from PyPI with automatic dependency resolution.
9
10
```bash { .api }
11
# Install latest version
12
pip install package_name
13
14
# Install specific version
15
pip install package_name==1.2.3
16
17
# Install version with constraints
18
pip install "package_name>=1.0,<2.0"
19
pip install "package_name~=1.2.0" # Compatible release
20
21
# Install multiple packages
22
pip install package1 package2 package3
23
```
24
25
### Requirements File Installation
26
27
Install packages from requirements files with dependency locking and constraint handling.
28
29
```bash { .api }
30
# Install from requirements file
31
pip install -r requirements.txt
32
33
# Install from multiple requirements files
34
pip install -r requirements.txt -r dev-requirements.txt
35
36
# Install with constraints
37
pip install -r requirements.txt -c constraints.txt
38
```
39
40
Requirements file format:
41
```text
42
# Direct dependencies
43
requests==2.28.1
44
django>=4.0,<5.0
45
46
# VCS dependencies
47
git+https://github.com/user/repo.git@v1.0#egg=package_name
48
49
# Local dependencies
50
-e ./local_package
51
/path/to/local/package.tar.gz
52
53
# Index options
54
--index-url https://pypi.org/simple/
55
--extra-index-url https://test.pypi.org/simple/
56
57
# Installation options
58
--no-deps
59
--force-reinstall
60
```
61
62
### Version Control Installation
63
64
Install packages directly from version control repositories with branch, tag, and commit support.
65
66
```bash { .api }
67
# Git repositories
68
pip install git+https://github.com/user/repo.git
69
pip install git+https://github.com/user/repo.git@branch_name
70
pip install git+https://github.com/user/repo.git@tag_name
71
pip install git+https://github.com/user/repo.git@commit_hash
72
73
# With subdirectory
74
pip install "git+https://github.com/user/repo.git#subdirectory=pkg_dir"
75
76
# With specific name
77
pip install "git+https://github.com/user/repo.git#egg=package_name"
78
79
# SSH URLs
80
pip install git+ssh://git@github.com/user/repo.git
81
82
# Other VCS systems
83
pip install svn+https://svn.example.com/repo/trunk/
84
pip install hg+https://bitbucket.org/user/repo
85
pip install bzr+https://launchpad.net/user/repo
86
```
87
88
### Local Installation
89
90
Install packages from local files, directories, and development installations.
91
92
```bash { .api }
93
# Install from local directory
94
pip install /path/to/package/
95
96
# Install from archive
97
pip install /path/to/package.tar.gz
98
pip install /path/to/package.whl
99
100
# Editable/development installation
101
pip install -e /path/to/package/
102
pip install -e . # Current directory
103
104
# Install from URL
105
pip install https://example.com/package.tar.gz
106
```
107
108
### Advanced Installation Options
109
110
Control installation behavior with various options and flags.
111
112
```bash { .api }
113
# Installation modes
114
pip install --user package_name # User installation
115
pip install --system package_name # System installation
116
pip install --target /path/to/dir package_name # Target directory
117
118
# Dependency handling
119
pip install --no-deps package_name # Skip dependencies
120
pip install --force-reinstall package_name # Force reinstall
121
pip install --upgrade package_name # Upgrade to latest
122
pip install --upgrade-strategy eager package_name # Upgrade dependencies
123
124
# Build options
125
pip install --no-binary :all: package_name # Build from source
126
pip install --only-binary :all: package_name # Only use wheels
127
pip install --prefer-binary package_name # Prefer wheels
128
pip install --no-build-isolation package_name # Disable build isolation
129
130
# Index options
131
pip install --index-url https://pypi.org/simple/ package_name
132
pip install --extra-index-url https://test.pypi.org/simple/ package_name
133
pip install --trusted-host pypi.org package_name
134
pip install --find-links /path/to/dir package_name
135
136
# Caching
137
pip install --no-cache-dir package_name # Disable cache
138
pip install --cache-dir /path/to/cache package_name # Custom cache
139
140
# Verbosity and output
141
pip install --verbose package_name # Verbose output
142
pip install --quiet package_name # Minimal output
143
pip install --progress-bar off package_name # Disable progress bar
144
```
145
146
### Hash Verification
147
148
Install packages with cryptographic hash verification for security.
149
150
```bash { .api }
151
# Using requirements file with hashes
152
pip install --require-hashes -r requirements.txt
153
```
154
155
Requirements file with hashes:
156
```text
157
requests==2.28.1 \
158
--hash=sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97ddf \
159
--hash=sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349
160
```
161
162
### Environment Variables
163
164
Control pip behavior through environment variables.
165
166
```bash { .api }
167
# Index configuration
168
export PIP_INDEX_URL=https://pypi.org/simple/
169
export PIP_EXTRA_INDEX_URL=https://test.pypi.org/simple/
170
export PIP_TRUSTED_HOST=pypi.org
171
172
# Installation options
173
export PIP_USER=1 # User installation
174
export PIP_NO_CACHE_DIR=1 # Disable cache
175
export PIP_REQUIRE_VIRTUALENV=1 # Require virtual environment
176
177
# Timeouts and retries
178
export PIP_TIMEOUT=60
179
export PIP_RETRIES=3
180
181
# Proxy configuration
182
export PIP_PROXY=http://user:password@proxy.server:port
183
```
184
185
### Installation in Virtual Environments
186
187
Pip respects virtual environment activation and provides isolation.
188
189
```bash { .api }
190
# Using venv
191
python -m venv myenv
192
source myenv/bin/activate # On Windows: myenv\Scripts\activate
193
pip install package_name
194
195
# Using conda
196
conda create -n myenv python=3.9
197
conda activate myenv
198
pip install package_name
199
200
# Require virtual environment
201
pip install --require-virtualenv package_name
202
export PIP_REQUIRE_VIRTUALENV=1
203
```
204
205
### Programmatic Installation
206
207
Use subprocess for programmatic package installation (recommended approach).
208
209
```python { .api }
210
import subprocess
211
import sys
212
213
def install_package(package_name, upgrade=False, user=False):
214
"""Install a Python package using pip."""
215
cmd = [sys.executable, '-m', 'pip', 'install']
216
217
if upgrade:
218
cmd.append('--upgrade')
219
if user:
220
cmd.append('--user')
221
222
cmd.append(package_name)
223
224
try:
225
subprocess.check_call(cmd)
226
print(f"Successfully installed {package_name}")
227
except subprocess.CalledProcessError as e:
228
print(f"Failed to install {package_name}: {e}")
229
raise
230
231
def install_requirements(requirements_file):
232
"""Install packages from requirements file."""
233
cmd = [sys.executable, '-m', 'pip', 'install', '-r', requirements_file]
234
235
try:
236
subprocess.check_call(cmd)
237
print(f"Successfully installed from {requirements_file}")
238
except subprocess.CalledProcessError as e:
239
print(f"Failed to install from {requirements_file}: {e}")
240
raise
241
242
# Usage examples
243
install_package('requests')
244
install_package('django', upgrade=True)
245
install_requirements('requirements.txt')
246
```