Automatically Import Modules When Entering the Python or Ipython Interpreter

Automatically import modules when entering the python or ipython interpreter

Use the environment variable PYTHONSTARTUP. From the official documentation:

If this is the name of a readable file, the Python commands in that
file are executed before the first prompt is displayed in interactive
mode. The file is executed in the same namespace where interactive
commands are executed so that objects defined or imported in it can be
used without qualification in the interactive session.

So, just create a python script with the import statement and point the environment variable to it. Having said that, remember that 'Explicit is always better than implicit', so don't rely on this behavior for production scripts.

For Ipython, see this tutorial on how to make a ipython_config file

Auto-Loading a module on IPython startup

i've found a solution to this one, in your IPython profile directory (by default - .ipython\profile_default), edit the file ipython_config.py (create it with ipython profile create if it does not exist) with the following lines:

# loads the root config object
c=get_config()

# executes the line in brackets on program launch
c.InteractiveShellApp.exec_lines = ['from __future__ import division']

Why can't you reference modules that appear to be automatically loaded by the interpreter without an additional `import` statement?

On a fresh interpreter startup, sys.modules will contain those modules that were loaded automatically by Python because they perform roles necessary for the Python interpreter to run. For example, Py_InitializeEx in Python/pythonrun.c includes lines like

bimod = _PyBuiltin_Init();

which initializes the __builtin__ module, where builtins like int and open live, and

sysmod = _PySys_Init();

which initializes the sys module.

As for what namespace these modules are loaded into, modules aren't really loaded into namespaces. The import statement loads a module and assigns the module or items from the module into a variable or variables in the current namespace, but the module loading and the assignment are independent steps. Calls like _PySys_Init don't assign the module into any particular namespace. However, the import machinery will record in sys.modules any module loaded by any code in the current Python execution, so further imports don't re-execute the module and create a new copy.

How to make IDLE automatically import modules at start?

You can use the -c or -r argument:

From idle -h:

-c cmd     run the command in a shell, or
-r file run script from file

For example:

idle -c 'import pickle, sys'

Or:

idle -r ~/my_startup.py

Where my_startup.py might contain:

import pickle, sys

You can either create a shell alias to always use this, or create a separate script; the procedure for this differs depending on your OS and shell.

Autoreload of modules in IPython

For IPython version 3.1, 4.x, and 5.x

%load_ext autoreload
%autoreload 2

Then your module will be auto-reloaded by default. This is the doc:

File:       ...my/python/path/lib/python2.7/site-packages/IPython/extensions/autoreload.py

Docstring:
``autoreload`` is an IPython extension that reloads modules
automatically before executing the line of code typed.

This makes for example the following workflow possible:

.. sourcecode:: ipython

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: from foo import some_function

In [4]: some_function()
Out[4]: 42

In [5]: # open foo.py in an editor and change some_function to return 43

In [6]: some_function()
Out[6]: 43

The module was reloaded without reloading it explicitly, and the
object imported with ``from foo import ...`` was also updated.

There is a trick: when you forget all of the above when using ipython, just try:

import autoreload
?autoreload
# Then you get all the above

Make IPython Import What I Mean

NOTE: This is now being maintained on Github. Download the latest version of the script from there!

I developed a script that binds to IPython's exception handling through set_custom_exc. If there's a NameError, it uses a regex to find what module you tried to use and then attempt to import it. It then runs the function you tried to call again. Here is the code:

import sys, IPython, colorama # <-- colorama must be "pip install"-ed

colorama.init()

def custom_exc(shell, etype, evalue, tb, tb_offset=None):
pre = colorama.Fore.CYAN + colorama.Style.BRIGHT + "AutoImport: " + colorama.Style.NORMAL + colorama.Fore.WHITE
if etype == NameError:
shell.showtraceback((etype, evalue, tb), tb_offset) # Show the normal traceback
import re
try:
# Get the name of the module you tried to import
results = re.match("name '(.*)' is not defined", str(evalue))
name = results.group(1)

try:
__import__(name)
except:
print(pre + "{} isn't a module".format(name))
return

# Import the module
IPython.get_ipython().run_code("import {}".format(name))
print(pre + "Imported referenced module \"{}\", will retry".format(name))
except Exception as e:
print(pre + "Attempted to import \"{}\" but an exception occured".format(name))

try:
# Run the failed line again
res = IPython.get_ipython().run_cell(list(get_ipython().history_manager.get_range())[-1][-1])
except Exception as e:
print(pre + "Another exception occured while retrying")
shell.showtraceback((type(e), e, None), None)
else:
shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)

# Bind the function we created to IPython's exception handler
IPython.get_ipython().set_custom_exc((Exception,), custom_exc)

You can make this run automatically when you start an IPython prompt by saving it somewhere and then setting an environment variable called PYTHONSTARTUP to the path to this file. You set environment variables differently depending on your OS (remember to alter the paths):

  • Windows: setx PYTHONSTARTUP C:\startup.py in command prompt
  • Mac/Linux (bash): Put export PYTHONSTARTUP=$HOME/startup.py into your ~/.bashrc

Here's a demo of the script in action:

Demo

Import pprint for every breakpoint()

Make a file like this, put it into an installable package if you want, or just drop it somewhere on the PYTHONPATH:

$ cat mybreakpoint.py 
import pdb

def pprint_breakpoint():
import pprint
pdb.set_trace()

Now you can use the env var PYTHONBREAKPOINT to customize the debugger scope like that:

$ PYTHONBREAKPOINT=mybreakpoint.pprint_breakpoint python3
Python 3.11.0b1 (main, May 18 2022, 12:50:35) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> breakpoint()
--Return--
> .../mybreakpoint.py(5)pprint_breakpoint()->None
-> pdb.set_trace()
(Pdb) pprint.pprint({"k2":"v2", "k1":"v1"})
{'k1': 'v1', 'k2': 'v2'}

Personally, I always install IPython into my development environments, so that I can use PYTHONBREAKPOINT=IPython.embed and get a more full-featured REPL in breakpoints by default, including IPython's pretty printer.

run python command line interpreter with imports loaded automatically

You can create a script with the code you wish to run automatically, then use python -i to run it. For example, create a script (let's call it script.py) with this:

import foo
import baz
l = [1,2,3,4]

Then run the script

$ python -i script.py
>>> print l
[1, 2, 3, 4]

After the script has completed running, python leaves you in an interactive session with the results of the script still around.

If you really want some things done every time you run python, you can set the environment variable PYTHONSTARTUP to a script which will be run every time you start python. See the documentation on the interactive startup file.



Related Topics



Leave a reply



Submit