2

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
  • 14,949
  • 19
  • 83
  • 132

1 Answers1

3

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
  • 17,284
  • 1
  • 38
  • 50
  • Thanks for these tips. I didn't know about any of these capabilities before. `python -m idlelib -s` from Command Prompt did launch IDLE and imported the modules as I had hoped. This approach though requires me to open Command Prompt to launch IDLE which is not my typical workflow. What I'm really trying to figure out is... when I click the IDLE shortcut in my taskbar or on my desktop, when IDLE launches, I want it to also load specific modules on startup. One solution I have come up with is to turn jarad\defaults.py into a module and import that. One import instead of 3 – Jarad Oct 14 '15 at 15:34
  • 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
  • If I understand right, synopsis from your steps are 1. Add shortcut of Python IDLE to desktop. 2. Right-click > properties > in 'Start in:', add command line arguments. After I add a shortcut to my desktop and go to properties, 'Start in:' shows `C:\Python34\`. When I change it to `C:\Python34\ -s` > Apply > OK, then open it back up, it looks like this: `"C:\Python34\ -s"`. When I try to change that to `"C:\Python34\" -s`, it changes it to `""C:\Python34\" -s"`. I did try the same process with `C:\Python34\Lib\idlelib\idle.py` too. Any suggestions what exactly to put in "Start in:"? – Jarad Oct 15 '15 at 07:31
  • 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
  • Got it! I think your #1 step above was the confusing part for be because I was going to Start > All Programs > Python 3.4 > IDLE and adding that shortcut to desktop which kept Shortcut Target text field grayed out. What I did now was went to `C:\Python34\Lib\idlelib\idle.pyw` and added that as shortcut to desktop. Then right-click Icon > properties > I can now edit `Target:` and entered `C:\Python34\Lib\idlelib\idle.pyw -s`. When I run it now, my print message loads `pd, np and plt imported`! Thanks so much Terry Jan Reedy. Didn't know you could add arguments to icons! – Jarad Oct 15 '15 at 15:02