Look at the source code.
As with most spaces in the blender UI the code is available. In the case of the python console the UI in
bl_ui/space_console.py and the nuts and bolts of the python console in
A code snippet from which, where the convenience imports and variables are added.
namespace["__builtins__"] = sys.modules["builtins"]
namespace["bpy"] = bpy
# weak! - but highly convenient
namespace["C"] = bpy.context
namespace["D"] = bpy.data
console = InteractiveConsole(locals=namespace,
console.push("from mathutils import *")
console.push("from math import *")
An option (but a bad slippery-slope don't do it option) is to edit this to suit, however
a lesser evil could be to copy, rename and put in our user
For example sake have called it
Purpose of the languages selector in the Python Console
and have one or a number of different options available via the menu. Which does involve having to select the language (... in this case python with different imports...) at least once per console
Add locals to a console.
Will find a number of run script in console addons, a couple of rippers that come to mind are by @CodemanX and @pinkvertex (the links escape me atm).
The main gist is using the module in conjunction with code as displayed in the script stub template. To run a script from the text editor and have it print to and available to inspect in the console.
Here is a script which when run adds locals to consoles in same screen.
for area in bpy.context.screen.areas:
if area.type == 'CONSOLE':
region = area.regions[-1]
id = hash(region)
con, stdout, stderr = console_python.get_console(id)
# add to locals
con.locals['bmesh'] = bmesh
# update with locals from this script
# push in some locals
con.push("foo = 3")
and in any console on the same screen as the text editor.
<module 'itertools' (built-in)>
<module 'bmesh' (built-in)>
The concept of the text editor is to import all requirements as if it were any other module on disk. We can however write an operator based on the
Text Editor > Templates > Python > External Script Stub
exec(compile(file.read(), filepath, 'exec'), global_namespace)
text_block.as_string() and add other members to