get-python-env.ts 938 B

123456789101112131415161718192021222324252627282930
  1. import * as vscode from "vscode"
  2. export async function getPythonEnvPath(): Promise<string | undefined> {
  3. const pythonExtension = vscode.extensions.getExtension("ms-python.python")
  4. if (!pythonExtension) {
  5. return undefined
  6. }
  7. // Ensure the Python extension is activated
  8. if (!pythonExtension.isActive) {
  9. // if the python extension is not active, we can assume the project is not a python project
  10. return undefined
  11. }
  12. // Access the Python extension API
  13. const pythonApi = pythonExtension.exports
  14. // Get the active environment path for the current workspace
  15. const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
  16. if (!workspaceFolder) {
  17. return undefined
  18. }
  19. // Get the active python environment path for the current workspace
  20. const pythonEnv = await pythonApi?.environments?.getActiveEnvironmentPath(workspaceFolder.uri)
  21. if (pythonEnv && pythonEnv.path) {
  22. return pythonEnv.path
  23. } else {
  24. return undefined
  25. }
  26. }