Skip to content

Autoimport package source of modules in an in-project .venv folder is wrong determined as PROJECT #813

@smheidrich

Description

@smheidrich

Describe the bug
When Rope's autoimport finds packages that are installed in a virtualenv placed within the project folder (typically named .venv by convention), it assigns them the package source PROJECT, even though SITE_PACKAGES would be correct because such venvs contain dependencies rather than files belonging to the project itself.

To Reproduce

Steps to reproduce the behavior:

  1. Create a virtualenv folder .venv in the current directory, e.g. python3 -m venv .venv.
  2. Activate the venv: . .venv/bin/activate
  3. Install pytest as an example external dependency: pip install pytest
  4. Run this script which calls autoimport's get_package_source to try and find pytest's package source type:
from rope.base.project import Project
from rope.contrib.autoimport import utils

project = Project(".")
resource = project.find_module("pytest")

source = utils.get_package_source(resource.pathlib, project, "pytest")
print(source)

Expected result

Source.SITE_PACKAGES because it's an external dependency installed in a .venv folder (and even has site-packages in its full path).

Actual result

Source.PROJECT

Screenshots
Not needed.

Editor information (please complete the following information):
No editor needed, Rope 1.13 or master, doesn't matter.

Additional context
Tools like python-lsp-server that use Rope's autoimport functionality factor in the package type when sorting the suggestions, as people normally want results from within the project itself show up higher in the list. This bug makes those results much worse, of course.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnexpected or incorrect user-visible behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions