2

Often when I am using the IDLE shell I import the pickle module. Is it possible to make it automatically import pickle when I start it?

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
H3dgehog
  • 23
  • 4
  • @Burhan Khalid: Not a duplicate, since that solution is for the Python shell, not idle. These solutions don't work for idle. – Martin Tournoij Sep 01 '14 at 10:49
  • 1
    @Burhan Khalid: I am a CPython core developer and currently the main Idle developer. Carpetsmoker is correct; this is not a duplicate. User code is *not* executed in the __main__ module. That is why the normal python solutions to execute code in the __main__ module are useless. Indeed, they could even be dangerous in the sense of disabling Idle. User code is instead exec-ed in a separate namespace designed to simulate __main__. So separate means are required to affect the environment for user code. – Terry Jan Reedy Sep 15 '14 at 05:11

1 Answers1

1

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.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 1
    According to my test using -c "import itertools", the effect disappears when the shell is restarted, as when a file is run from the editor. I have an idea to partly fix this, by running files in a separate process and leaving the interactive shell alone, but that is at best in the future. – Terry Jan Reedy Sep 15 '14 at 05:18