JuliaPy / pyjuliapkg

Manage your Julia dependencies from Python
MIT License
49 stars 13 forks source link

Refactor identifying whether in a virtualenv #30

Closed ma-sadeghi closed 5 months ago

ma-sadeghi commented 5 months ago

Would you consider a PR refactoring the way to identify if in a virtualenv? Currently, it's by inspecting VIRTUAL_ENV environment variable, which is not reliable.

cjdoris commented 5 months ago

Sure. How is it not reliable? What would be better?

ma-sadeghi commented 5 months ago

VIRTUAL_ENV is not necessarily exported (for instance here). The documented way is to compare sys.prefix and sys.base_prefix. Replacing

https://github.com/JuliaPy/pyjuliapkg/blob/311d1063fda8c32318b01881ca4e922b3fa0669f/src/juliapkg/state.py#L61

with:

vprefix = sys.prefix if sys.prefix != sys.base_prefix else None

Fixes the issue.

Source: https://stackoverflow.com/a/1883251/3438239

cjdoris commented 5 months ago

Ah cool, yeah a PR for that sounds good. Please include a link to the docs in a comment.

This can also resolve the ambiguity which currently causes and error if both VIRTUAL_ENV and CONDA_PREFIX are set - with this method we are sure that the virtual environment takes precedence (i.e. have created a virtual environment inside a Conda environment and not the other way around).

Just need to double check that the test doesn't pass in a Conda environment.