0

From within the Canopy editor, how do I run a python script within the current interactive namespace?

Specifically, I have a large model object that takes a long time to load. I would like to instantiate the model in the interactive shell and then be able to run scripts that operate on the existing model. If I select the text in my script and click the "Run the selected text" button, everything works fine. Alternatively, if I type "run -i script.py" as suggested in this related question it also works fine. However, if I click the "Run the current file" button, the script appears to run in a clean namespace and cannot find any of the variables that already exist in the interactive shell.

This is the specific code that I am trying to use to only load the model on the first pass through the script.

# Only instantiating the model object if it does not already exist
if 'model' not in locals():
    model = TreeModel(filename)

1 Answer 1

1

Click "Run the current file", then when it fails to run as desired, press the up arrow to recall the run command, insert the -i, and press Enter to re-run in ipython's interactive mode.

In theory, Canopy could be provided with this as yet another Run variant, but we want to avoid cluttering the UI with seldom-used options.

Sign up to request clarification or add additional context in comments.

4 Comments

I noticed that there is an "Arguments" field in the "Run Configurations" dialogue box, but it appears to be for arguments that are passed to the script itself. Would this be a logical place to add configurability for the run command itself?
It could not be implemented reasonably in that field, would need to be in another similar field.
Sorry for the ambiguous "this". Either an additional arguments field (or simply a checkbox if this is one of only a few common tweaks) in the "Run Configurations" dialogue box is what I was imagining. Thanks for letting me know to stop searching for an integrated way of doing this and providing a workaround.
Got it. Submitted for feature request triage, in any case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.