().state
+ val runConfigState = MiseRunConfigurationSettingsEditor.getMiseRunConfigurationState(runParams)
+
+ val (workDir, configEnvironment) =
+ when {
+ projectState.useMiseDirEnv -> project.basePath to projectState.miseConfigEnvironment
+ runConfigState?.useMiseDirEnv == true -> {
+ val pythonWorkDir = runParams.workingDirectory ?: runParams.projectPathOnTarget
+ (pythonWorkDir ?: runParams.project.basePath) to runConfigState.miseConfigEnvironment
+ }
+ else -> null to null
+ }
+
+ val envVars =
+ MiseCommandLineHelper
+ .getEnvVars(workDir, configEnvironment)
+ .fold(
+ onSuccess = { envVars -> envVars },
+ onFailure = {
+ if (it !is MiseCommandLineNotFoundException) {
+ MiseNotificationServiceUtils.notifyException("Failed to load environment variables", it)
+ }
+ emptyMap()
+ },
+ )
+
+ envVars.forEach { (key, value) ->
+ pythonExecution.addEnvironmentVariable(key, value)
+ }
+ }
+ }
+}
diff --git a/modules/products/pycharm/src/main/kotlin/com/github/l34130/mise/python/run/PythonRunConfigurationExtension.kt b/modules/products/pycharm/src/main/kotlin/com/github/l34130/mise/python/run/PythonRunConfigurationExtension.kt
new file mode 100644
index 00000000..82ba1735
--- /dev/null
+++ b/modules/products/pycharm/src/main/kotlin/com/github/l34130/mise/python/run/PythonRunConfigurationExtension.kt
@@ -0,0 +1,78 @@
+package com.github.l34130.mise.python.run
+
+import com.github.l34130.mise.core.command.MiseCommandLineHelper
+import com.github.l34130.mise.core.command.MiseCommandLineNotFoundException
+import com.github.l34130.mise.core.notification.MiseNotificationServiceUtils
+import com.github.l34130.mise.core.run.MiseRunConfigurationSettingsEditor
+import com.github.l34130.mise.core.setting.MiseSettings
+import com.intellij.execution.configurations.GeneralCommandLine
+import com.intellij.execution.configurations.RunnerSettings
+import com.intellij.openapi.components.service
+import com.intellij.openapi.options.SettingsEditor
+import com.jetbrains.python.run.AbstractPythonRunConfiguration
+import com.jetbrains.python.run.PythonRunConfigurationExtension
+import org.jdom.Element
+
+class PythonRunConfigurationExtension : PythonRunConfigurationExtension() {
+ override fun isApplicableFor(runConfiguration: AbstractPythonRunConfiguration<*>): Boolean = true
+
+ override fun isEnabledFor(
+ runConfiguration: AbstractPythonRunConfiguration<*>,
+ runnerSettings: RunnerSettings?,
+ ): Boolean = true
+
+ override fun writeExternal(
+ runConfiguration: AbstractPythonRunConfiguration<*>,
+ element: Element,
+ ) {
+ MiseRunConfigurationSettingsEditor.writeExternal(runConfiguration, element)
+ }
+
+ override fun readExternal(
+ runConfiguration: AbstractPythonRunConfiguration<*>,
+ element: Element,
+ ) {
+ MiseRunConfigurationSettingsEditor.readExternal(runConfiguration, element)
+ }
+
+ override fun getEditorTitle(): String = MiseRunConfigurationSettingsEditor.EDITOR_TITLE
+
+ override fun > createEditor(configuration: P): SettingsEditor
=
+ MiseRunConfigurationSettingsEditor(configuration.project)
+
+ override fun patchCommandLine(
+ runConfiguration: AbstractPythonRunConfiguration<*>,
+ runnerSettings: RunnerSettings?,
+ cmdLine: GeneralCommandLine,
+ runnerId: String,
+ ) {
+ val project = runConfiguration.project
+ val projectState = project.service().state
+ val runConfigState = MiseRunConfigurationSettingsEditor.getMiseRunConfigurationState(runConfiguration)
+
+ val (workDir, configEnvironment) =
+ when {
+ projectState.useMiseDirEnv -> project.basePath to projectState.miseConfigEnvironment
+ runConfigState?.useMiseDirEnv == true -> {
+ val pythonWorkDir = runConfiguration.workingDirectory ?: runConfiguration.projectPathOnTarget
+ (pythonWorkDir ?: runConfiguration.project.basePath) to runConfigState.miseConfigEnvironment
+ }
+ else -> null to null
+ }
+
+ val envVars =
+ MiseCommandLineHelper
+ .getEnvVars(workDir, configEnvironment)
+ .fold(
+ onSuccess = { envVars -> envVars },
+ onFailure = {
+ if (it !is MiseCommandLineNotFoundException) {
+ MiseNotificationServiceUtils.notifyException("Failed to load environment variables", it)
+ }
+ emptyMap()
+ },
+ )
+
+ cmdLine.withEnvironment(envVars)
+ }
+}
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 9cb3f6dc..9d6af07c 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -22,6 +22,7 @@ include(
"modules/products/gradle",
"modules/products/idea",
"modules/products/nodejs",
+ "modules/products/pycharm",
"modules/products/rider",
"modules/products/toml",
)
diff --git a/src/main/resources/META-INF/mise-pycharm.xml b/src/main/resources/META-INF/mise-pycharm.xml
new file mode 100644
index 00000000..728265dc
--- /dev/null
+++ b/src/main/resources/META-INF/mise-pycharm.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 15d810c9..f69737e7 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -37,6 +37,7 @@
com.intellij.gradle
com.intellij.java
JavaScript
+ com.intellij.modules.python
com.intellij.modules.rider
org.toml.lang