Skip to content

Commit 9ab72f7

Browse files
authored
Fix python sys.path problems (#2010)
* Fix unnecessary syspaths from relative imports * Fix subpath "site-package" checking
1 parent a2407f4 commit 9ab72f7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import kotlinx.coroutines.runBlocking
2525
import org.jetbrains.kotlin.idea.util.application.runWriteAction
2626
import org.jetbrains.kotlin.idea.util.module
2727
import org.jetbrains.kotlin.idea.util.projectStructure.sdk
28-
import org.jetbrains.kotlin.konan.file.File
2928
import org.utbot.common.PathUtil.toPath
3029
import org.utbot.common.appendHtmlLine
3130
import org.utbot.framework.UtSettings
@@ -335,7 +334,8 @@ fun getDirectoriesForSysPath(
335334
file.fromImports.forEach { importTarget ->
336335
importTarget.resolveImportSourceCandidates().forEach {
337336
val directory = it.parent
338-
if (directory is PsiDirectory ) {
337+
val isRelativeImport = importTarget.relativeLevel > 0 // If we have `from . import a` we don't need to add syspath
338+
if (directory is PsiDirectory && !isRelativeImport) {
339339
// If we have `from a.b.c import d` we need to add syspath to module `a` only
340340
val additionalLevel = importTarget.importSourceQName?.componentCount?.dec() ?: 0
341341
directory.topParent(additionalLevel)?.let { dir ->
@@ -347,7 +347,9 @@ fun getDirectoriesForSysPath(
347347

348348
// Select modules only from this project but not from installation directory
349349
importedPaths.forEach {
350-
if (it.isProjectSubmodule(ancestor) && !it.path.split(File.separator).contains("site-packages")) {
350+
val path = it.toNioPath()
351+
val hasSitePackages = (0 until(path.nameCount)).any { i -> path.subpath(i, i+1).toString() == "site-packages"}
352+
if (it.isProjectSubmodule(ancestor) && !hasSitePackages) {
351353
sources.add(it)
352354
}
353355
}

0 commit comments

Comments
 (0)