3

I am on Windows 7. When I launch Python IDLE, I want it to pre-load: pandas, numpy and matplotlib without me having to type out the import statements. I import them very frequently.

I have waded through several posts:

This one has to do with iPython, not IDLE-specific

This one has to do with running a script in IDLE

This one talks about PYTHONSTARTUP for interactive sessions

From these posts, I have determined that there is a distinct difference between Windows command-prompt python interactive shell and IDLE's interactive shell.

For example, I created defaultimports.py and put it in this location:

C:\Python34\Lib\site-packages\jaradspythonstartup

That script contains the following:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
print('pd, np and plt imported')

Next, on my machine, I went to Start > Computer > right-clicked Properties > Advanced system settings > environment variables > clicked New... and added a new variable named PYTHONSTARTUP, then put my path C:\Python34\Lib\site-packages\jaradspythonstartup\defaultimports.py

pythonstartup environment variable

However, this seems to only work in Windows Command prompt when I open command prompt and type python. I do see it loads my print statement.

Imported Successfully in command prompt

When I launch IDLE, I don't see the message: pd, np, and plt imported print statement. While in IDLE, if I import os and os.environ['PYTHONSTARTUP'], I do see my environment variable defined but don't know if that's important to note or not.

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.environ['PYTHONSTARTUP']
'C:\\Python34\\Lib\\site-packages\\jaradspythonstartup\\defaultimports.py'
>>> 

My Question

How can I pre-load modules in IDLE on startup?

Community
  • 1
  • 1
Jarad
  • 17,409
  • 19
  • 95
  • 154

1 Answers1

4

python -m idlelib -h on a command line, where python runs recent 3.x, will display startup commands for IDLE; use idlelib.idle for 2.x. It says that

idle idlelib -s "runs $IDLESTARTUP or $PYTHONSTARTUP before anything else".

If that does not work, try python -m idlelib -s.

Currently, when you restart the shell, which happens when you run a file from the editor, ....STARTUP does not get re-run. I hope to fix that.

EDIT: How to start IDLE with arguments from the desktop instead of command line.

  1. Make a properly labelled IDLE desktop icon. Go to Start Menu > Python x.y > IDLE... and copy to Desktop. Control-Left Mouse Button drag or (Win 7, at least) Right click, Send to > Desktop (create shortcut). If for Python before 3.4, right click icon, select Rename, and label with the version number. (We recently got complaint "I installed 3.5 and (pre-existing) desktop icon opens 2.7.).

  2. Make icon open IDLE with arguments. Right-click icon, select properties, click end (you may or may not have to click box first). Cursor should be at end of line with ...pythonw.exe...idlew.py. Space and add arguments. I tried -c "print('hello')" to test. Add -s for ...STARTUP. Consider renaming to indicate addition, such as IDLE 3.5 64 bit STARTUP, in case you want another IDLE desktop icon.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • I determined that the target for desktop icons can be edited to include startup arguments. See edit. – Terry Jan Reedy Oct 14 '15 at 20:55
  • Edit the box called `target`. It has the equivalent of a command line ('start idlew.py with pythonw.exe') and that is where to add arguments. `Start in` is the directory. For my icon, this is blank and should be left blank. – Terry Jan Reedy Oct 15 '15 at 14:50