VirtualFile (@NotNull Sdk sdk) { VirtualFile[] classVFiles = ReadAction.compute(() -> sdk.getRootProvider().getFiles(OrderRootType.CLASSES)); String homePath = sdk.getHomePath(); if (homePath != null) { File root = PythonSdkUtil.getVirtualEnvRoot(homePath); if (root != null) { File libRoot = new File(root, "lib"); File[] versionRoots = libRoot.listFiles(); if (versionRoots != null && !SystemInfo.isWindows) { final File versionRoot = ContainerUtil.find(versionRoots, file -> file.isDirectory() && file.getName().startsWith("python")); if (versionRoot != null) { libRoot = versionRoot; } } // Empty in case of a temporary empty SDK created to install package management if (classVFiles.length == 0) { return LocalFileSystem.getInstance().findFileByIoFile(libRoot); } final String libRootPath = libRoot.getPath(); for (VirtualFile file : classVFiles) { if (FileUtil.pathsEqual(file.getPath(), libRootPath)) { return file; } // venv module doesn't add virtualenv's lib/pythonX.Y directory itself in sys.path final VirtualFile parent = file.getParent(); if (PyNames.SITE_PACKAGES.equals(file.getName()) && FileUtil.pathsEqual(parent.getPath(), libRootPath)) { return parent; } } } } return null; }