Skip to content

fix(python): Relative path resolution for plugin libraries #21911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2025

Conversation

anath2
Copy link
Contributor

@anath2 anath2 commented Mar 24, 2025

Following up on #21675.
The relative path plugin cannot be resolved because the root .venv is not included in the calculated path. This fixes it by calculating the path relative to the parent of sys.prefix

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels Mar 24, 2025
Copy link

codecov bot commented Mar 24, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 80.48%. Comparing base (0ca485a) to head (002d2d6).
Report is 47 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-plan/src/dsl/function_expr/plugin.rs 30.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21911      +/-   ##
==========================================
- Coverage   80.48%   80.48%   -0.01%     
==========================================
  Files        1636     1636              
  Lines      236700   236709       +9     
  Branches     2693     2693              
==========================================
+ Hits       190504   190507       +3     
- Misses      45563    45568       +5     
- Partials      633      634       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@amotzop
Copy link
Contributor

amotzop commented Mar 24, 2025

I think this still assumes your virtual env is in your current working directory, which is not always the case

@anath2
Copy link
Contributor Author

anath2 commented Mar 25, 2025

Thanks for pointing that out. Probably the correct approach should to be to find the
[path to virtual environment from cwd] / [path to lib.so from .venv]

@anath2 anath2 marked this pull request as draft March 25, 2025 05:07
@anath2 anath2 force-pushed the fix-venv-relpath branch 2 times, most recently from 23697d4 to f8deb29 Compare March 25, 2025 10:34
@amotzop
Copy link
Contributor

amotzop commented Mar 25, 2025

Someone can still change their cwd. Maybe a more foolproof solution would be to keep the path relative to the virtual environment and have polars concat that relative path to the full path of the venv when it tries to load a plugin?

@anath2 anath2 force-pushed the fix-venv-relpath branch from d9e27bf to 0b9e4e0 Compare March 26, 2025 03:20
@anath2
Copy link
Contributor Author

anath2 commented Mar 26, 2025

What do you mean by change of cwd? os.path.relpath(path.resolve(), Path.cwd()) should still load correctly I think. At least on *nix systems

@anath2 anath2 marked this pull request as ready for review March 26, 2025 10:57
@amotzop
Copy link
Contributor

amotzop commented Mar 26, 2025

I think in the current state the following code won't work:

import os
import polars as pl
import polars_hash as plh
ldf = pl.LazyFrame({"a": ["1", "2", "3"]}).with_columns(h=plh.col("a").chash.sha2_256())
os.chdir("..")
df = ldf.collect()

because the relative path calculation happens when the LazyFrame is created but the actual loading of the plugin happens only when it's collected. This might seem a bit convoluted, but in #21643 they talk about serializing and deserializing LazyFrames, which is exactly where such problems can occur.

I propose to save the plugin's path relative to the venv when it's created (as in your previous PR), but to change the code that actually loads the plugin (in polars-plan/src/dsl/function_expr/plugin.rs) to append the current venv path to the relative path. I think this should work in almost all cases.

@anath2
Copy link
Contributor Author

anath2 commented Mar 27, 2025

Ah okay I see what you mean now. I did not consider that directory could change before collecting the LazyFrame

@anath2 anath2 marked this pull request as draft March 27, 2025 18:22
@anath2 anath2 force-pushed the fix-venv-relpath branch from bbb96fe to 002d2d6 Compare March 29, 2025 16:06
@anath2 anath2 marked this pull request as ready for review March 29, 2025 16:25
@anath2 anath2 requested a review from orlp as a code owner March 29, 2025 16:25
@mat-ej
Copy link

mat-ej commented Apr 2, 2025

we just tested this solution and it seems to work well. I was able to move graphs from one .venv and run them in another .venv. Do you think this could get some attention and perhaps get finally merged ? @ritchie46

@ritchie46
Copy link
Member

Alright. Thanks all!

@ritchie46 ritchie46 merged commit 4ce4d69 into pola-rs:main Apr 4, 2025
27 checks passed
@anath2 anath2 deleted the fix-venv-relpath branch April 4, 2025 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants