-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Resolving Python path using CONDA_PREFIX variable to support Conda and Pixi
#18267
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
Conversation
|
CONDA_PREFIX variable to support Conda and PixiCONDA_PREFIX variable to support Conda and Pixi
| .map(PythonPath::from_virtual_env_var) | ||
| }) | ||
| .unwrap_or_else(|| PythonPath::Discover(project_root.to_path_buf())), | ||
| .or_else(|| Some(PythonPath::Discover(project_root.to_path_buf()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone can help me out since I am new to Rust, this is the solution I came up with to deal with the compiler errors. Is it fine to wrap PythonPath::Discover(project_root.to_path_buf()) in Some(.) and is it fine to simply unwrap std::env::var("CONDA_PREFIX").ok().map(PythonPath::from_conda_prefix_var) ?
MichaReiser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this. I left an inline comment. I hope the rest of the team agrees with my proposed change but that should simplify your life
| .map(PythonPath::from_virtual_env_var) | ||
| }) | ||
| .unwrap_or_else(|| PythonPath::Discover(project_root.to_path_buf())), | ||
| .or_else(|| Some(PythonPath::Discover(project_root.to_path_buf()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may not work as intended (unless I'm misreading the code here).
The .unwrap_or_else will only look at the CONDA_PREFIX if the or_else on line 177 returned None. However, this can never be the case, because the or_else on line 177 always.
Given that CONDA_PREFIX is more explicit than an existing .venv directory, I think I'm leaning towards changing the order to
VIRTUAL_ENVif it is setCONDA_PREFIX- Fallback to inspecting the project directory structure
That should also simplify your live because all you need to do is add an or_else similar to the VIRTUAL_ENV branch before the unwrap_or_else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that
CONDA_PREFIXis more explicit than an existing.venvdirectory
What worries me here is that it's possible (though not necessarily recommended) to create a virtual environment inside a conda environment. If the user has done that, it might be pretty surprising if we require them to explicitly activate the virtual environment in order for us to recognise it (it goes against the general "Astral philosophy" of not requiring users to imperatively activate their virtual environments).
I say "activate a virtual environment" because I think almost no one sets the VIRTUAL_ENV environment variable manually: it's usually set as part of the virtual-environment activation (source .venv/bin/activate in the user's shell)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. Implementing this with this specific discovery ordering would be much more complicated because it can't be implemented here. Instead, it would need to happen inside the search path setting resolution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(it goes against the general "Astral philosophy" of not requiring users to imperatively activate their virtual environments).
Fair, but it would be activate if we have a tigher uv integration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt people who use conda environments are going to want to run ty via uv — pixi or conda are the project managers you probably want to use if you're using conda environments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I'm confused. if you use conda, then changing the order wouldn't matter? And I think it's fine to ask for explicit configuration if you have a project where you have an activated CONDA env and a nested venv. It's just the best information that the user has given us and a environment variable is, IMO, always more explicit than using some heuristic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, yes, that's fair enough. If the user is using a non-conda virtual environment inside their conda environment, I guess in 2025 they might be using a non-conda project manager to run commands in their project and relying on that non-conda project manager to keep that virtual environment activated for the duration of those commands. I guess I'm wrapping my head round this paradigm too — I haven't used conda since before uv was a thing 😆
In that case, I'm OK with what you suggest: preferring an explicitly activated conda environment over a virtual environment unless the virtual environment has also been explicitly activated (either by the user or by a non-conda project manager such as uv/hatch/poetry/pdm).
We should make sure we write up the rationale for this in a comment, though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also very open to changing this if it proves problematic. The benefit of still being in preview :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The .unwrap_or_else will only look at the CONDA_PREFIX if the or_else on line 177 returned None. However, this can never be the case, because the or_else on line 177 always." so from what I understand, you are saying that even if no .venv is found, we will never reach line unwrap_or_else in line 178. From my testing, if no .venv directory is found then ty will set the python path using CONDA_PREFIX which means that unwrap_or_else in line 178 is used (It's my first time writing Rust so please correct me).
Also from what I understand from your conversations is that I should revert the changes of my last commit and have the following check order :
VIRTUAL_ENVif it is setCONDA_PREFIXif it is set- Fallback to inspecting the project directory structure
Thank you for reviewing my PR!
|
I was looking for a way to write tests by setting/unsetting the |
|
our markdown tests don't support setting environment variables. The best you can try is to write a CLI test in Line 11 in d098118
|
|
I'll convert this to draft. @lipefree you can convert it back to "ready for review" once your PR is ready for another round. This makes it easier for me to know when I should take a look at your PR |
I tried my best for the CLI test. I don't know if it is enough for you. |
MichaReiser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great and nice write up in the test!
|
Oh no, the tests don't work on Windows because the venv path is slightly different. Do you want to take a look? |
I don't have a Windows machine so it is hard for me to look at how it works. I guess I just need to figure out how Conda environments are structured on Windows but then I don't know how to have OS dependent tests run on my mac (apart from letting the CI do it for me which is quite slow). |
|
I also don't have a windows machine ;( Here's an example on how you can have custom logic based on whether you're on windows. Lines 957 to 961 in d098118
|
crates/ty/tests/cli.rs
Outdated
| "#, | ||
| ), | ||
| ( | ||
| "conda-env/lib/python3.13/site-packages/package1/__init__.py", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on Windows the path needs to be conda-env/Lib/site-packages/package1/__init__.py rather than conda-env/lib/python3.13/site-packages/package1/__init__.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does "conda-env/Lib/site-packages/package1/init.py" works instead of "conda-env/Lib/site-packages/package1/init.py" ? I am just trusting my copy/paste skill at this point since I can't test locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok it's just markdown doing some formatting. I got it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mistyped my message originally -- sorry! If you refresh your browser, the edited version of my message should be displayed :-)
It does indeed need to be __init__.py rather than init.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok it's just markdown doing some formatting. I got it.
yeah, exactly 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope it will pass now and that I did not do any typo !
|
Perfect! ty |
|
@lipefree congrats on your first open-source contribution!! This is a fantastic feature for us to have 😃 |
|
Thank you for your help @MichaReiser @AlexWaygood on my first contribution ! |
Summary
Implement what was discussed in astral-sh/ty#265. Pixi and Conda set the
CONDA_PREFIXenvironment variable and notVIRTUAL_ENV. With this PR, if no--python flaghas been passed andVIRTUAL_ENVhas not been set then we checkCONDA_PREFIXand resolve the path.I am still not sure about how to implement:
CONDA_PREFIXis checked only if there's no.venvdirectory in the project root.My guess is it should be something like this but the
.unwrap_or_else()is still confusing to me and I can't figure out how to check forCONDA_PREFIXafter the.unwrap_or_else().Closes astral-sh/ty#265
Test Plan
I reproduced the error from #265, and now there is no more
lint:unresolved-importerror. I am still not familar with the markdown tests but I will do more commits to provide tests.This is my first opensource contribution and I am still a student with 0 experience in Rust, please give me constructive feedbacks! I will try to learn as quickly as I can!