newbie - How do I import automatically?

B

bobueland

When I start Python Shell I can see that some names or loaded
automatically, ready for me to use
['__builtins__', '__doc__', '__name__']

I have written some functions in a file called btools.py. I would like
to import them automatically when I start up Python shell. Today I must
do it by hand like this
from btools import *
dir() ['__builtins__', '__doc__', '__name__', 'func1', 'func2', 'func3']

Is there a way to do this automatically so that I always have 'func1',
'func2' and so on available, without me having to do it by hand?

Bob
 
B

Brett Hoerner

I have written some functions in a file called btools.py. I would like
to import them automatically when I start up Python shell. Today I must
do it by hand like this
from btools import *
dir() ['__builtins__', '__doc__', '__name__', 'func1', 'func2', 'func3']

Is there a way to do this automatically so that I always have 'func1',
'func2' and so on available, without me having to do it by hand?

This isn't _exactly_ what you wanted, but you might want to try using
IPython, a much improved Python shell,

http://ipython.scipy.org/

You can easily configure it to automatically import whatever kind of
modules you want on start, etc... and it has many other features, I
just can't picture touching the normal Python shell anymore.
 
M

Mike Meyer

I have written some functions in a file called btools.py. I would like
to import them automatically when I start up Python shell. Today I must
do it by hand like this
from btools import *
dir() ['__builtins__', '__doc__', '__name__', 'func1', 'func2', 'func3']

Is there a way to do this automatically so that I always have 'func1',
'func2' and so on available, without me having to do it by hand?

Set the PYTHONSTARTUP environment variable to the name of a python
file that does the "from btools import *".

<mike
 
B

bobueland

I've tried as you said but it doesn't work. I'm working with Windows
XP. I right click at my computer go to Advanced, choose Environment
Variables and set PYTHONSTARTUP variable to C:\Python24\binit.py. It
looks like this

# binit.py
from btools import *

I've restarted the computer and started IDLE but I only get

IDLE 1.1.1
dir() ['__builtins__', '__doc__', '__name__']

What could be wrong?
 
B

bobueland

I also checked in command prompt, and there it works!, but not in IDLE.
And it's in IDLE that I work all the time. Can anything be done to get
it to work there?

Bob
 
M

Mike Meyer

I've tried as you said but it doesn't work. I'm working with Windows
XP. I right click at my computer go to Advanced, choose Environment
Variables and set PYTHONSTARTUP variable to C:\Python24\binit.py. It
looks like this

# binit.py
from btools import *

I've restarted the computer and started IDLE but I only get

IDLE 1.1.1
dir() ['__builtins__', '__doc__', '__name__']

What could be wrong?

The problem is idle. PYTHONSTARTUP only works for interactive Python
session, and apparently idle doesn't honor it. You'll have to get
someone who uses idle to explain how to get that functionality.

<mike
 
C

Claudio Grondi

Just edit the first lines of %SystemDrive%\Python24\Lib\idlelib\PyShell.py
yourself (not tested, but I have customized Idle intitial message using it
and
it worked ok - after new Python installation I overwrite this file with my
own
version to keep this customization.)

Hope this helps.

Claudio
 
B

bobueland

I tried to put the line

from btools import *

in several places in PyShell.py

but to now avail. It does not work, IDLE does not execute it???

Bob
 
B

bobueland

Where do I put

def runsource(self, source):
if(source == ''):
source = 'from btools import *'
"Extend base class method: Stuff the source in the line cache
first"
filename = self.stuffsource(source)

Do I put it in Pyshell.py or somewhere else?

Bob
 
C

Claudio Grondi

I tried to put the line
from btools import *
in several places in PyShell.py
but to now avail. It does not work, IDLE does not execute it???
Bob

I have to give up here :-( .

The best solution I am able to come up with is:

def runsource(self, source):

if(source == ''):
source = 'from btools import *'

"Extend base class method: Stuff the source in the line cache first"
filename = self.stuffsource(source)

which performs 'from btools import *' each time you hit return on the
command line without typing anything in the interpreter.

Maybe someone knowledgable in IDLE can share here a better way how to
initialize IDLE with any code valid in the interpreter itself.
Inbetween the above can maybe serve as workaround.

Claudio
 
C

Claudio Grondi

Probably you have inbetween already found the 'def runsource(' line in the
PyShell.py , but maybe you still wait for a reply, so here it is:

yes, you put the two lines at the beginning of in PyShell.py existing
runsource() method of the class ModifiedInterpreter(InteractiveInterpreter)

If in my Windows IDLE version the things are different from yours, sorry,
I can't help in that case having no Linux installation available.

Claudio
 
S

Steve M

The file C:\Python24\Lib\sitecustomize.py (which I think doesn't exist
by default) executes every time Python starts. (This means not just
your IDLE session but every time you run any Python script.)

One use for this file is to invoke sys.setdefaultencoding because that
name gets deleted during initialization and so is unavailable by the
time your main script starts executing.

You can also put in any other code, such as "from btools import *".

You can also put your btools.py file in C:\Python24\Lib\site-packages
folder and it will be available from any program (as opposed to
requiring it in the current directory).
 
M

Mikael Olofsson

I tried to put the line
from btools import *
in several places in PyShell.py
but to now avail. It does not work, IDLE does not execute it???

IDLE definitely executes PyShell.py upon startup, since IDLE does not
even appear on the screen if there is an error in that file. The
following seems to work for me, in PyShell.py. After importing both sys
and os,

sys.modules['__main__'].__dict__['os'] = os

Then, if I start IDLE, the module os is directly available.

HTH
/MiO
 
S

Serge Orlov

I also checked in command prompt, and there it works!, but not in IDLE.
And it's in IDLE that I work all the time. Can anything be done to get
it to work there?

According to command line help
(run C:\Python24\Lib\idlelib>idle.py -h )
"idle -s" is what you're looking for.
 
B

bobueland

I tried to do it on my computer (win XP). I put an extra line in
PyShell.py

#! /usr/bin/env python

import os
import os.path
import sys
import string
import getopt
import re
import socket
import time
import threading
import traceback
import types
import exceptions

# test
sys.modules['__main__'].__dict__['os'] = os

import linecache

Then when I start idle I get

IDLE 1.1.1
dir() ['__builtins__', '__doc__', '__name__']

So I don't see 'os'

Do you see 'os' on your computer. If yes, what could be the difference?

Bob
 
M

Mikael Olofsson

I tried to do it on my computer (win XP). I put an extra line in
PyShell.py
[snip]
# test
sys.modules['__main__'].__dict__['os'] = os
[snip]
Then when I start idle I get

IDLE 1.1.1

['__builtins__', '__doc__', '__name__']


So I don't see 'os'

Do you see 'os' on your computer. If yes, what could be the difference?

Yes, I do. The following is with IDLE 1.0.3 and Python 2.3.4 on WinXP.

Without the line in PyShell.py:

IDLE 1.0.3 ==== No Subprocess ==== ['__builtins__', '__doc__', '__file__', '__name__', 'main']

With the line in PyShell.py:

IDLE 1.0.3 ==== No Subprocess ====
>>> dir() ['__builtins__', '__doc__', '__file__', '__name__', 'main', 'os']
>>> os
<module 'os' from 'C:\Python23\lib\os.pyc'>

I know nothing about the details of how IDLE work. All I know is what I
have reported here. I was simply inspired by the following remark from
Claudio Grondi up-thread:
> edit the first lines of %SystemDrive%\Python24\Lib\idlelib\PyShell.py

I guess that PyShell.py is imported as a module by IDLE at startup.

One thought, though: Du you have more than one Python installed? If so:
Could it be that you are editing the wrong PyShell.py?

Regards
/MiO
 
B

bobueland

There is one C:\Python24\Lib\site-packages\sitecustomize.py but no
C:\Python24\Lib\sitecustomize.py

However I created one as you suggested but then I started IDLE nothing
showed up. Have you tested that it works on your computer?

Bob
 
C

Claudio Grondi

Thanks to "Serge Orlov" for the hint:
"run C:\Python24\Lib\idlelib>idle.py -h"

Following works for me now
if I start IDLE by following console command:

C:\Python24\pythonw.exe C:\Python24\Lib\idlelib\idle.py -c "import os;from
path import path"

The IDLE console prompt appears in the second line
(so -c "import os;from path import path"
probably fakes execution of an input line)
and os and path modules are available for usage.

To be honest I don't like this solution and the solution with setting any
environment variable.
I would be much more happy to understand how I can modify PyShell.py or
another script file IDLE uses in order to achieve same effect.

Claudio

When I start Python Shell I can see that some names or loaded
automatically, ready for me to use
['__builtins__', '__doc__', '__name__']

I have written some functions in a file called btools.py. I would like
to import them automatically when I start up Python shell. Today I must
do it by hand like this
from btools import *
dir() ['__builtins__', '__doc__', '__name__', 'func1', 'func2', 'func3']

Is there a way to do this automatically so that I always have 'func1',
'func2' and so on available, without me having to do it by hand?

Bob
 
C

Claudio Grondi

Mikael Olofsson said:
I tried to put the line
from btools import *
in several places in PyShell.py
but to now avail. It does not work, IDLE does not execute it???

IDLE definitely executes PyShell.py upon startup, since IDLE does not
even appear on the screen if there is an error in that file. The
following seems to work for me, in PyShell.py. After importing both sys
and os,

sys.modules['__main__'].__dict__['os'] = os

Then, if I start IDLE, the module os is directly available.

HTH
/MiO


I have put
sys.modules['__main__'].__dict__['os'] = os
at the beginning of PyShell.py :
"
#! /usr/bin/env python

import os
import os.path
import sys

sys.modules['__main__'].__dict__['os'] = os
"

and also here:
"
class ModifiedInterpreter(InteractiveInterpreter):

def __init__(self, tkconsole):
self.tkconsole = tkconsole

sys.modules['__main__'].__dict__['os'] = os
locals = sys.modules['__main__'].__dict__
"

but it does not work ... (why ???)

What happens if you remove the line
sys.modules['__main__'].__dict__['os'] = os
from your PyShell.py?
Will os be no more available or is it still there?

Claudio
 
M

Mikael Olofsson

I tried to do it on my computer (win XP). I put an extra line in
PyShell.py
[snip]
# test
sys.modules['__main__'].__dict__['os'] = os
[snip]
Then when I start idle I get

IDLE 1.1.1


['__builtins__', '__doc__', '__name__']


So I don't see 'os'

Do you see 'os' on your computer. If yes, what could be the difference?

I answered:
Yes, I do. [snip details]

I have to step back a bit. IDLE seems to work differently depending on
how it is started. My comments above hold when I start IDLE by
right-clicking on a python-file and choosing "Edit with IDLE".

If I instead start IDLE itself (from the windows start-menu) I get the
same behaviour as you do, namely no os around. Another difference, which
I've noted before is that shell restart is only available when IDLE has
been started this way.

All this seems to be handled in PyShell.py, but I do not have the time
to trace that in any detail.

Regards
/MiO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,270
Messages
2,571,352
Members
48,034
Latest member
BettinaArn

Latest Threads

Top