@@ -6,6 +6,7 @@ import { IClipboard, ICommandManager, IWorkspaceService } from '../../common/app
66import { IExtensionSingleActivationService } from '../../activation/types' ;
77import { Commands } from '../../common/constants' ;
88import { getSysPath } from '../../common/utils/pythonUtils' ;
9+ import { IInterpreterPathService } from '../../common/types' ;
910
1011@injectable ( )
1112export class CopyImportPathCommand implements IExtensionSingleActivationService {
@@ -15,6 +16,7 @@ export class CopyImportPathCommand implements IExtensionSingleActivationService
1516 @inject ( ICommandManager ) private readonly commands : ICommandManager ,
1617 @inject ( IWorkspaceService ) private readonly workspace : IWorkspaceService ,
1718 @inject ( IClipboard ) private readonly clipboard : IClipboard ,
19+ @inject ( IInterpreterPathService ) private readonly interpreterPathService : IInterpreterPathService ,
1820 ) { }
1921
2022 async activate ( ) : Promise < void > {
@@ -28,7 +30,9 @@ export class CopyImportPathCommand implements IExtensionSingleActivationService
2830 return ;
2931 }
3032
31- const importPath = this . resolveImportPath ( uri . fsPath ) ;
33+ const resource : vscode . Uri | undefined = uri ?? this . workspace . workspaceFolders ?. [ 0 ] ?. uri ;
34+ const pythonPath = this . interpreterPathService . get ( resource ) ;
35+ const importPath = this . resolveImportPath ( uri . fsPath , pythonPath ) ;
3236 await this . clipboard . writeText ( importPath ) ;
3337 void vscode . window . showInformationMessage ( `Copied: ${ importPath } ` ) ;
3438 }
@@ -45,9 +49,9 @@ export class CopyImportPathCommand implements IExtensionSingleActivationService
4549 * @param absPath - The absolute path to a `.py` file.
4650 * @returns The resolved import path in dotted notation (e.g., 'pkg.module').
4751 */
48- private resolveImportPath ( absPath : string ) : string {
52+ private resolveImportPath ( absPath : string , pythonPath ?: string ) : string {
4953 // ---------- ① sys.path ----------
50- for ( const sysRoot of getSysPath ( ) ) {
54+ for ( const sysRoot of getSysPath ( pythonPath ) ) {
5155 if ( sysRoot && absPath . startsWith ( sysRoot ) ) {
5256 return CopyImportPathCommand . toDotted ( path . relative ( sysRoot , absPath ) ) ;
5357 }
0 commit comments