[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: importer des modules au démarrage d'IDLE



Hi! This is an English list, so please frame your queries in
English. But since I could translate your query, I'll try to answer
below, please translate if needed.

On Mon, Nov 16, 2009 at 05:43:09PM +0100, Frederic Baldit wrote:
> je travaille sous lenny et gnome avec python 2.5.2 et IDLE, que
> j'utilise pour une introduction à l'algorithmique au lycée (en maths).
> Voilà mon problème:
> 
> 1) J'ai créé un fichier .pythonrc.py file contenant les deux lignes
> 
> from __future__ import division
> from math import *
> 
> 2) Dans mon .bashrc j'ai rajouté
> 
> PYTHONSTARTUP = "/home/fred/.pythonrc.py"
> export PYTHONSTARTUP
> 
> 3) Quand je lance python de façon interactive dans une console tout va
> bien:
> 
> >>> 1/3
> 0.33333333333333331
> >>> sqrt(5)
> 2.2360679774997898
> 
> 4) J'ai écrit un petit script test.py qui contient
> 
> print sqrt(2)
> print 1/3
> 
> Quand j'édite/teste ce script avec IDLE en lancant IDLE en ligne de
> commande par

Sadly, python --help says this:

PYTHONSTARTUP: file executed on interactive startup (no default)

So, this implies that the file is executed ONLY if Python is started
interactively as:

python<Enter>

rather than Python <scriptname>

> idle-python2.5 -n -s test.py
> 
> et que j'exécute avec F5 je n'ai pas d'erreur mais la division n'est pas
> flottante:
> 
> IDLE 1.2.2      ==== No Subprocess ====
> >>> 
> >>> 
> 1.41421356237
> 0

I would guess the same thing would happen even in this case. The
reason why this is NOT possible in Python is because if you effect
such changes, the moment the script is run on another machine, it'll
break. So, other than a hackish solution which someone could suggest,
I can't think of a way to achieve this effect (elegantly).

> 5) Plus embêtant, si je lance IDLE par la souris en utilisant le menu
> gnome Applications-->Programmation-->IDLE, puis que j'ouvre mon fichier
> test.py et que je l'exécute (F5) j'ai une erreur avec sqrt(2) qui n'est
> pas connu (NameError: name 'sqrt'is not defined). Pourtant j'ai vérifié
> (Système-->Préférences-->Menu Principal) que l'application est lancée
> par la commande /usr/bin/idle-python2.5 -n -s qui devrait donc exécuter
> mon fichier de démarrage.
> 
> Quelqu'un peut-il m'aider?? Je sais que je pourrais (pour la division)
> faire 1./3, ou passer à la version 3. Mais de toute façon je veux aussi
> une solution permettant d'avoir par défaut accès aux fonctions
> mathématiques usuelles, sans que les élèves aient à écrire la ligne from
> math import * en tête de leur script.

Someone else might have better suggestions, but when I asked a Python
expert, I was told NOT to do this; the students should write the
import lines at the top; consider it something akin to including
headers in C programs! (Yes, I know I am not giving useful
suggestions, but just trying to explain why this "feature" isn't
really present in Python.).

Sorry for not being much help.

Kumar


Reply to: