Choosing the Right IPython Console Execution Mode in Spyder
Learn how to choose between 'Current Console' and 'Dedicated Console' in Spyder to balance iteration speed with environment isolation and avoid ghost variables.
05 Dec 2025, 15:04 UTC

The Conflict: Execution Speed vs. State Isolation
When running scripts in Spyder, you face a recurring trade‑off: do you want your code to run as quickly as possible by leveraging existing memory, or do you need to ensure that your script works in a clean environment? Choosing the wrong execution mode often leads to \"ghost variables\"—situations where a script runs successfully only because a variable was defined in a previous, unrelated session, causing the code to fail when deployed to production.
Execution Mode Comparison
Spyder provides two primary methods for handling the IPython console backend via the Run menu. The choice depends on whether you are in the exploratory phase (testing snippets) or the validation phase (finalizing a script).
| Feature | Execute in Current Console | Execute in Dedicated Console |
|---|---|---|
| Namespace | Shared (Persistent) | Isolated (Fresh) |
| Startup Speed | Fast (No kernel restart) | Slower (New kernel launch) |
| Variable Leakage | High risk of ghost variables | Zero risk |
| Memory Usage | Cumulative until restart | Reset per execution |
Trade-offs and Engineering Risks
The Case for the Current Console: This mode is ideal for data analysis and machine learning. If you have loaded a 2GB CSV file into a Pandas DataFrame, you do not want to wait 30 seconds for that file to reload every time you tweak a plotting parameter. By preserving the state, you iterate on the logic without repeating the expensive data‑loading phase.
The Case for Dedicated Consoles: This mode is essential for library development and API design. It ensures that all imports are explicitly stated in the file. If you forget to add import numpy as np to your script but have it loaded in your current session, the \"Current Console\" mode will hide this error, leading to an ImportError when the code is shared with others.
Implementation: Configuring Execution Behavior
You can set these preferences globally or on a per‑file basis. To configure a specific script:
- Navigate to the Run menu in the top toolbar.
- Select Configuration per file (or press
F6). - Under the General settings section, toggle between:
Execute in current consoleExecute in dedicated console
- Click OK.
Validation: Testing for State Leakage
To verify which mode is active and identify if your environment is contaminated with ghost variables, perform the following diagnostic test:
# Step 1: Run this in the IPython console manually
leak_test_var = \\"I am a ghost\\"
# Step 2: Create a new script file (test.py) with ONLY this content:
print(leak_test_var)
# Step 3: Run test.py using your chosen configuration
Expected Results:
- If using \\"Current Console\\": The console prints
I am a ghost. This confirms the namespace is shared and persistent. - If using \\"Dedicated Console\\": The console throws a
NameError: name 'leak_test_var' is not defined. This confirms the environment is isolated.
Limitations and Memory Management
Regardless of the mode, Spyder's Variable Explorer tracks all objects in the current namespace. In long‑running sessions using the \\"Current Console\\" mode, memory usage will climb as you create new versions of large arrays. To mitigate this without restarting the kernel, use the del command on large objects or use the Remove all variables (broom icon) in the Variable Explorer pane.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.