[Solved] importing modules at IDLE startup
Le lundi 16 novembre 2009 à 16:09 -0800, Mark Voorhies a écrit :
> A good way to test the environment that python/idle is seeing is:
> import os
> os.environ["PYTHONSTARTUP"]
Here are the results:
>>> import os
>>> os.environ["PYTHONSTARTUP"]
'/home/fred/.pythonrc.py'
So for me the variable is correctly initialized at login.
> When you start a bash terminal session (e.g., via gnome-terminal) ~/.bashrc is
> sourced and the resulting environment is passed to any programs started from
> that terminal session. I believe that the environment seen by programs
> launched from the gnome menu is set up through some combination of your login
> manager (e.g., gdm) and Xsession, in a fairly distro dependent way. In my
> hands, this means that I can see my ~/.bashrc environment from python or idle
> launched from a bash shell, but not from the gnome menu.
OK, so in my case it seems that my .bashrc is executed at login and
known by gnome.
> If you don't want to muck around with Xsession config files, and you're
> hacking the idle-python2.5 script anyway, you can modify the environment from
> within the script:
>
> ----------------------------------------------------
> #! /usr/bin/python2.5 -Qnew
> import os
> os.environ["PYTHONSTARTUP"] = "/home/fred/.pythonrc.py"
> from idlelib.PyShell import main
> if __name__ == '__main__':
> main()
> -----------------------------------------------------
OK
So, now my problem is entirely solved under linux. I went into XP to see
if I could analogously solve it. Here are the things I did:
1) I created a C:\Python26\pythonrc.py file containing the two lines
from __future__ import division
from math import *
2) I created an environment variable PYTHONSTARTUP pointing to
pythonrc.py
3) I changed the program associated to .py files so that they are now
associated with the command
C:\Python26\pythonw.exe -Qnew C:\Python26\Lib\idlelib\idle.pyw -n -e -s
4) I created a C:\Python26\pythonw.bat file containing the line
start C:\Python26\pythonw.exe -Qnew C:\Python26\Lib\idlelib\idle.pyw -n
-e -s
5) I added a new link in menu Start-->Programs-->Python26 which points
to the .bat file (I renamed the name of the link and changed its icon)
Note that step 4) and 5) where only to give the user a possibility to
start IDLE "alone", without double-clicking (or right-clicking) on a
file. I'm not sure if there is a simpler way than steps 4)-5).
So globally my problem is solved.
By the way, I also asked myself whether I could also indicate, at IDLE
startup, whitch coding system is to be used, so that students never have
to add the line
# -*- coding: cp1252 -*-
at the top of their script file (on the PCs we have in my high school).
Do you know if I can do this (maybe with the os module)?
Thank's a lot for your precious help!
Gratefully,
Frédéric.
Reply to: