-
Notifications
You must be signed in to change notification settings - Fork 18
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
deptry does not work when installed globally #92
Comments
Added a warning for now: #93 |
Just to confirm, is it impossible to perform static code analysis with pyproject.toml, poetry.lock and .venv directory? |
No, it's definitely possible to scan a project with pyproject.toml, poetry.lock and .venv. when added to the project with But it will not work to install it globally with |
Yes, this issue is regarding global installation of |
On Reddit, someone offered this as a potential starting point for adding the functionality; https://stackoverflow.com/a/14792407 Not sure if we should add this kind of solution to the codebase though. |
@mkniewallner suggested using |
Tried this out, but also unsuccessful. To reproduce:
In my case, this returns:
And a list of warnings since However, when running the following steps:
The output is:
So then it does find the correct |
Hi 👋🏻 I have a very naïve question: from the If I'm completely off-topic (which I fear 😅 ) I'll be glad to have some pointers to the codebase that might help me understand the problem better! |
@kwentine Thanks for the suggestion. That's no naive question, don't be afraid to ask! I am not an expert at this subject myself either. The issue lies in this part of the code. Here, we try to get the metadata of a package using Your idea of using An issue I can think of with this solution; how do we detect if it's necessary to perform this recursive search? |
@fpgmaas thanks for encouragements and this enlightening entry point 🙂 I'd like to share an idea based on First, suppose we have a way of reliably detecting if def running_in_virtualenv() -> bool:
# See https://docs.python.org/3/library/sys.html?highlight=sys#sys.base_prefix for this strategy
return sys.prefix != sys.base_prefix Then, suppose we have a few heuristics to guess a project's virtualenv def find_virtualenv_site_packages() -> Path | None:
project_dir: Path = current_project_dir()
site_packages = None
possible_roots = [
os.environ.get("VIRTUAL_ENV"),
project_dir / ".venv",
Path("~/.virtualenvs") / project_dir.name,
]
while not site_packages and possible_roots:
site_packages = find_site_packages_below(possible_roots.pop())
return site_packages Then we could implement and install a from importlib.metadata import DistributionFinder
class VirtualenvDistributionFinder(DistributionFinder):
@classmethod
def find_distributions(cls, context):
if not running_in_virtualenv():
site_packages = find_virtualenv_site_packages()
if site_packages:
path = [site_packages, *sys.path]
context = DistributionFinder.Context(name=context.name, path=path)
return super().find_distributions(context) Let me know if I need to make the idea clearer. If you think this might be a way to go, I'll work on a PR 🙂 |
Well I realize that implementation would be highly inefficient since it would call if not running_in_virtualenv():
site_packages = find_virtualenv_site_packages(project_dir)
sys.meta_path.insert(0, VritualenvDistributionFinder(site_packages=site_packages)) |
I think this is the most promising and detailed starting point until now, better than what I could think of 😄 So if you think it's worth a shot, I look forward to reviewing the PR that implements this. |
Late to the party, but it's probably worth taking a look at how pipdeptree added support for arbitrary virtualenvs: https://github.com/tox-dev/pipdeptree/blob/28bf158e98e95109a426aad8a0ac3b1ea2044d4a/src/pipdeptree/_non_host.py#L16 |
By setting the Seems like https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.DistributionFinder might be the way to go for implementing in deptry (you can see in the docs that the |
Describe the bug
Whenever deptry is installed globally, it does not have access to the metadata of the packages in the virtual environment, even if that virtual environment is activated.
I will see if I can either
To Reproduce
install globally with
pip install deptry
outside of the virtualenv. Then activate a virtualenv and rundeptry .
The text was updated successfully, but these errors were encountered: