forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
run modules plain if running as jupyter modules doesn't work #4763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
IanMatthewHuff
merged 1 commit into
microsoft:master
from
IanMatthewHuff:dev/ianhu/fixModuleCommands
Mar 14, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow Interactive Window to run commands as both `-m jupyter command` and as `-m command` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,12 @@ import { | |
| import { JupyterConnection, JupyterServerInfo } from './jupyterConnection'; | ||
| import { JupyterKernelSpec } from './jupyterKernelSpec'; | ||
|
|
||
| enum ModuleExistsResult { | ||
| NotFound, | ||
| FoundJupyter, | ||
| Found | ||
| } | ||
|
|
||
| export class JupyterExecutionBase implements IJupyterExecution { | ||
|
|
||
| private processServicePromise: Promise<IProcessService>; | ||
|
|
@@ -619,11 +625,14 @@ export class JupyterExecutionBase implements IJupyterExecution { | |
|
|
||
| private findInterpreterCommand = async (command: string, interpreter: PythonInterpreter, cancelToken?: CancellationToken): Promise<IJupyterCommand | undefined> => { | ||
| // If the module is found on this interpreter, then we found it. | ||
| if (interpreter && await this.doesModuleExist(command, interpreter, cancelToken) && !Cancellation.isCanceled(cancelToken)) { | ||
| if (interpreter && !Cancellation.isCanceled(cancelToken)) { | ||
| const exists = await this.doesModuleExist(command, interpreter, cancelToken); | ||
|
|
||
| // Our command args are different based on the command. ipykernel is not a jupyter command | ||
| const args = command === JupyterCommands.KernelCreateCommand ? ['-m', command] : ['-m', 'jupyter', command]; | ||
| return this.commandFactory.createInterpreterCommand(args, interpreter); | ||
| if (exists === ModuleExistsResult.FoundJupyter) { | ||
| return this.commandFactory.createInterpreterCommand(['-m', 'jupyter', command], interpreter); | ||
| } else if (exists === ModuleExistsResult.Found) { | ||
| return this.commandFactory.createInterpreterCommand(['-m', command], interpreter); | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
|
|
@@ -742,23 +751,41 @@ export class JupyterExecutionBase implements IJupyterExecution { | |
| return this.commands.hasOwnProperty(command) ? this.commands[command] : undefined; | ||
| } | ||
|
|
||
| private doesModuleExist = async (module: string, interpreter: PythonInterpreter, cancelToken?: CancellationToken): Promise<boolean> => { | ||
| private doesModuleExist = async (moduleName: string, interpreter: PythonInterpreter, cancelToken?: CancellationToken): Promise<ModuleExistsResult> => { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just swapped moduleName as module shows up as keyword coloring. |
||
| if (interpreter && interpreter !== null) { | ||
| const newOptions: SpawnOptions = { throwOnStdErr: true, encoding: 'utf8', token: cancelToken }; | ||
| const pythonService = await this.executionFactory.createActivatedEnvironment({ resource: undefined, interpreter, allowEnvironmentFetchExceptions: true }); | ||
| try { | ||
| // Special case for ipykernel | ||
| const actualModule = module === JupyterCommands.KernelCreateCommand ? module : 'jupyter'; | ||
| const args = module === JupyterCommands.KernelCreateCommand ? ['--version'] : [module, '--version']; | ||
|
|
||
| const result = await pythonService.execModule(actualModule, args, newOptions); | ||
| return !result.stderr; | ||
| // For commands not 'ipykernel' first try them as jupyter commands | ||
| if (moduleName !== JupyterCommands.KernelCreateCommand) { | ||
| try { | ||
| const result = await pythonService.execModule('jupyter', [moduleName, '--version'], newOptions); | ||
| if (!result.stderr) { | ||
| return ModuleExistsResult.FoundJupyter; | ||
| } else { | ||
| this.logger.logWarning(`${result.stderr} for ${interpreter.path}`); | ||
| } | ||
| } catch (err) { | ||
| this.logger.logWarning(`${err} for ${interpreter.path}`); | ||
| } | ||
| } | ||
|
|
||
| // After trying first as "-m jupyter <module> --version" then try "-m <module> --version" as this works in some cases | ||
| // for example if not running in an activated environment without script on the path | ||
| try { | ||
| const result = await pythonService.execModule(moduleName, ['--version'], newOptions); | ||
| if (!result.stderr) { | ||
| return ModuleExistsResult.Found; | ||
| } else { | ||
| this.logger.logWarning(`${result.stderr} for ${interpreter.path}`); | ||
| return ModuleExistsResult.NotFound; | ||
| } | ||
| } catch (err) { | ||
| this.logger.logWarning(`${err} for ${interpreter.path}`); | ||
| return false; | ||
| return ModuleExistsResult.NotFound; | ||
| } | ||
| } else { | ||
| return false; | ||
| return ModuleExistsResult.NotFound; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. This is way simpler than I thought it was going to be.