Skip to content

Commit 65277a5

Browse files
authored
include user site-packages in default Python path (#81)
* include user site-packages in default Python path In many (most?) Python installations, this is where e.g. `pip install`'d packages end up. Signed-off-by: Joel Dice <joel.dice@fermyon.com> * skip non-existent directories in Python path Signed-off-by: Joel Dice <joel.dice@fermyon.com> * bump version to 0.13.2 Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 35bf29e commit 65277a5

10 files changed

Lines changed: 41 additions & 28 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "componentize-py"
3-
version = "0.13.1"
3+
version = "0.13.2"
44
edition = "2021"
55
exclude = ["cpython"]
66

examples/cli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-cli] `command` world.
1010
## Prerequisites
1111

1212
* `Wasmtime` 18.0.0 or later
13-
* `componentize-py` 0.13.1
13+
* `componentize-py` 0.13.2
1414

1515
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
1616
you don't have `cargo`, you can download and install from
1717
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
1818

1919
```
2020
cargo install --version 18.0.0 wasmtime-cli
21-
pip install componentize-py==0.13.1
21+
pip install componentize-py==0.13.2
2222
```
2323

2424
## Running the demo

examples/http/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-http] `proxy` world.
1010
## Prerequisites
1111

1212
* `Wasmtime` 18.0.0 or later
13-
* `componentize-py` 0.13.1
13+
* `componentize-py` 0.13.2
1414

1515
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
1616
you don't have `cargo`, you can download and install from
1717
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
1818

1919
```
2020
cargo install --version 18.0.0 wasmtime-cli
21-
pip install componentize-py==0.13.1
21+
pip install componentize-py==0.13.2
2222
```
2323

2424
## Running the demo

examples/matrix-math/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ within a guest component.
1111
## Prerequisites
1212

1313
* `wasmtime` 18.0.0 or later
14-
* `componentize-py` 0.13.1
14+
* `componentize-py` 0.13.2
1515
* `NumPy`, built for WASI
1616

1717
Note that we use an unofficial build of NumPy since the upstream project does
@@ -23,7 +23,7 @@ https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
2323

2424
```
2525
cargo install --version 18.0.0 wasmtime-cli
26-
pip install componentize-py==0.13.1
26+
pip install componentize-py==0.13.2
2727
curl -OL https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz
2828
tar xf numpy-wasi.tar.gz
2929
```

examples/sandbox/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ sandboxed Python code snippets from within a Python app.
88
## Prerequisites
99

1010
* `wasmtime-py` 18.0.0 or later
11-
* `componentize-py` 0.13.1
11+
* `componentize-py` 0.13.2
1212

1313
```
14-
pip install componentize-py==0.13.1 wasmtime==18.0.2
14+
pip install componentize-py==0.13.2 wasmtime==18.0.2
1515
```
1616

1717
## Running the demo

examples/tcp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ making an outbound TCP request using `wasi-sockets`.
1111
## Prerequisites
1212

1313
* `Wasmtime` 18.0.0 or later
14-
* `componentize-py` 0.13.1
14+
* `componentize-py` 0.13.2
1515

1616
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
1717
you don't have `cargo`, you can download and install from
1818
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.
1919

2020
```
2121
cargo install --version 18.0.0 wasmtime-cli
22-
pip install componentize-py==0.13.1
22+
pip install componentize-py==0.13.2
2323
```
2424

2525
## Running the demo

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ features = ["pyo3/extension-module"]
77

88
[project]
99
name = "componentize-py"
10-
version = "0.13.1"
10+
version = "0.13.2"
1111
description = "Tool to package Python applications as WebAssembly components"
1212
readme = "README.md"
1313
license = { file = "LICENSE" }

src/command.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn generate_bindings(common: Common, bindings: Bindings) -> Result<()> {
136136
fn componentize(common: Common, componentize: Componentize) -> Result<()> {
137137
let mut python_path = componentize.python_path;
138138

139-
if let Some(site_packages) = find_site_packages()? {
139+
for site_packages in find_site_packages()? {
140140
python_path.push(
141141
site_packages
142142
.to_str()
@@ -168,18 +168,18 @@ fn componentize(common: Common, componentize: Componentize) -> Result<()> {
168168
Ok(())
169169
}
170170

171-
fn find_site_packages() -> Result<Option<PathBuf>> {
171+
fn find_site_packages() -> Result<Vec<PathBuf>> {
172172
Ok(if let Ok(env) = env::var("VIRTUAL_ENV") {
173173
let dir = Path::new(&env).join("lib");
174174

175175
if let Some(site_packages) = find_dir("site-packages", &dir)? {
176-
Some(site_packages)
176+
vec![site_packages]
177177
} else {
178178
eprintln!(
179179
"warning: site-packages directory not found under {}",
180180
dir.display()
181181
);
182-
None
182+
Vec::new()
183183
}
184184
} else {
185185
let pipenv_packages = match process::Command::new("pipenv").arg("--venv").output() {
@@ -188,38 +188,45 @@ fn find_site_packages() -> Result<Option<PathBuf>> {
188188
let dir = Path::new(str::from_utf8(&output.stdout)?.trim()).join("lib");
189189

190190
if let Some(site_packages) = find_dir("site-packages", &dir)? {
191-
Some(site_packages)
191+
vec![site_packages]
192192
} else {
193193
eprintln!(
194194
"warning: site-packages directory not found under {}",
195195
dir.display()
196196
);
197-
None
197+
Vec::new()
198198
}
199199
} else {
200200
// `pipenv` is in `$PATH`, but this app does not appear to be using it
201-
None
201+
Vec::new()
202202
}
203203
}
204204
Err(_) => {
205205
// `pipenv` is not in `$PATH -- assume this app isn't using it
206-
None
206+
Vec::new()
207207
}
208208
};
209209

210-
if pipenv_packages.is_some() {
210+
if !pipenv_packages.is_empty() {
211211
pipenv_packages
212212
} else {
213213
// Get site packages location using the `site` module in python
214214
match process::Command::new("python3")
215-
.args(["-c", "import site; print(site.getsitepackages()[0])"])
215+
.args([
216+
"-c",
217+
"import site; \
218+
list = site.getsitepackages(); \
219+
list.insert(0, site.getusersitepackages()); \
220+
print(';'.join(list))",
221+
])
216222
.output()
217223
{
218-
Ok(output) => {
219-
let path = Path::new(str::from_utf8(&output.stdout)?.trim()).to_path_buf();
220-
Some(path)
221-
}
222-
Err(_) => None,
224+
Ok(output) => str::from_utf8(&output.stdout)?
225+
.trim()
226+
.split(';')
227+
.map(|p| Path::new(p).to_path_buf())
228+
.collect(),
229+
Err(_) => Vec::new(),
223230
}
224231
}
225232
})

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ pub async fn componentize(
221221
isyswasfa: Option<&str>,
222222
stub_wasi: bool,
223223
) -> Result<()> {
224+
// Remove non-existent elements from `python_path` so we don't choke on them later:
225+
let python_path = &python_path
226+
.iter()
227+
.filter_map(|&s| Path::new(s).exists().then_some(s))
228+
.collect::<Vec<_>>();
229+
224230
// Untar the embedded copy of the Python standard library into a temporary directory
225231
let stdlib = tempfile::tempdir()?;
226232

0 commit comments

Comments
 (0)