[IPython-dev] IPython.Extensions on sys.path
fperez.net at gmail.com
Sun Nov 5 13:25:31 CST 2006
On 11/3/06, Walter Dörwald <walter at livinglogic.de> wrote:
> Fernando Perez wrote:
> > [...]
> > I just added this code to the main ipython starter script:
> > # Start by cleaning up sys.path: Python automatically inserts the script's
> > # base directory into sys.path, at the front. This can lead to unpleasant
> > # surprises.
> > import sys
> > sys.path.pop(0)
> > Now, the front of sys.path is identical for a plain python and an
> > ipython shell. The only difference is that under ipython, the very
> > last two entries are:
> > '/home/fperez/usr/lib/python2.4/site-packages/IPython/Extensions',
> > '/home/fperez/.ipython']
> > which don't exist for plain Python. I don't see these as a big problem,
> > though.
> > If others disagree, let us know.
> This breaks running the start script directly. With the current version
> I get a sys.path that looks like this:
Mmh, I also re-checked, and found that plain python also adds that
directory to sys.path. I must have been mistaken before. I'll revert
this change now then, to avoid problems.
We should have a very clear policy on how we manipulate sys.path.
Currently (once I revert the sys.path.pop() change) we have:
1. At startup, ipython's sys.path matches a plain python shell, except
for the addition at the end of:
Since these two go at the very end, they don't worry me too much: they
are a convenience for interactive configuration and by being at the
end, they can't shadow anything in the default sys.path. I could see
removing the last one to be a bit more minimalistic, but I'm not sure
it's worth it.
2. There's a more delicate issue: sys.path changes from %run. In
order to mimic the running of scripts from a system prompt, %run used
to do this:
2384 # find things also in current directory
2385 dname = os.path.dirname(fname)
2386 if not sys.path.count(dname):
which I removed in r1874. I then realized that this is needed so that
scripts can find local things, but because ipython persists the state
of sys.path across runs (something that simply does not occur in a
plain python run from the command line), these changes 'contaminate'
sys.path for the rest of the session.
I'm going to add this back, but in a modified form that will try to
intelligently revert out any changes made to sys.path. I will leave
any changes actually made by the user to persist, in case people have
scripts they use to manipulate sys.path for further interactive use.
Any feedback you may have on this is welcome.
More information about the IPython-dev