Skip to content

Implement copying python import path from opened file #25026

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

s-kai273
Copy link

@s-kai273 s-kai273 commented May 1, 2025

Summary

This PR implements a new feature that allows users to copy the Python import path of the currently opened file to the clipboard via the file tab context menu.

What was done

  • Add a context menu item to the Python file tab for copying the import path.
  • Implement python.copyImportPath command.
  • Implement test cases to verify the behavior under different path resolution scenarios:
    • When a sys.path entry matches the file path.
    • When no sys.path match is found but the file is within a workspace folder.
    • When neither match is found, using the filename as a fallback.

Example

For a file at /home/user/project/src/pkg/module.py and sys.path containing /home/user/project/src, the copied import path will be:

pkg.module

@s-kai273
Copy link
Author

s-kai273 commented May 1, 2025

@microsoft-github-policy-service agree

}

/**
* Resolves a Python import-style dotted path from an absolute file path.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As described here, the strategy for resolving the Python import path is as follows:

  1. If the file is located under any entry in sys.path, the path relative to that entry is used.
  2. If the file is located under the current workspace folder, the path relative to the workspace root is used.
  3. Otherwise, the import path falls back to the file name (without extension).

I believe this approach is reasonable, but if you have any suggestions for improvement, I’d love to hear them!

@@ -1126,6 +1131,11 @@
}
],
"keybindings": [
{
"command": "python.copyImportPath",
"key": "ctrl+alt+shift+i",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've set Ctrl+Alt+Shift+I as the default keybinding.
If you find it inconvenient or have a better suggestion, please let me know!

@s-kai273
Copy link
Author

s-kai273 commented May 4, 2025

Hi, just a quick note regarding the CI failure:

It appears that the error is not related to the changes in this PR.
The failure seems to be caused by a build issue with the testresources package, due to an incompatibility with the latest setuptools (related to the deprecation of easy_install.sys_executable).

Please let me know if anything needs to be changed on my end. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant