[IPython-user] Can "import" module, then "run" cannot find it

Thomas Baker tbaker@tbaker...
Mon Feb 22 00:13:58 CST 2010


Hi Jörgen,

> The reason for your confusion is that sys.path is the path used for the 
> python import mechanism but the run magic of ipython only looks in the 
> current directory.

Okay - that does explain it - thank you!

So if I want to run code, within ipython, but from a directory
other than the directory where the code is located, I cannot use
%run.  But I guess if I %edit the code, %edit will find it wherever
it is in my path, and when I exit the editor, the code will be 
executed.  Would that be any different than using %run to execute
the code?

Many thanks,
Tom Baker


On Mon, Feb 22, 2010 at 08:05:13PM +0100, Jörgen Stenarson wrote:
> > This is what happens:
> >
> >      /home/tbaker/shawkle/testdata>  ipython      # I start IPython
> >      Python 2.5.2. ... blah blah...
> >
> >      In [2]: import sys
> >
> >      In [3]: print sys.path
> >      ['', '/usr/bin', ... '/home/tbaker/shawkle']  # '/home/tbaker/shawkle' is in search path
> >
> >      In [4]: pwd                                   # I am indeed in '/home/tbaker/shawkle/testdata'
> >      Out[4]: '/home/tbaker/shawkle/testdata'
> >
> >      In [5]: import shawkle             # "import" finds 'shawkle.py' with no problem
> >
> >      In [6]: edit shawkle                       # editing 'shawkle.py' works fine
> >      Editing... done. Executing edited code...  # vim shows me I am editing '~/shawkle/shawkle.py'
> >
> >      In [7]: edit shawkle.datashuffle           # editing a particular function works fine
> >      Editing... done. Executing edited code...  # vim says I am editing ~/shawkle/shawkle.py'
> >
> >      In [8]: run shawkle
> >      ERROR: File 'shawkle.py' not found.        # So why can it now not find 'shawkle.py'??
> >
> >      In [9]: run ../shawkle.py                  # Giving a relative reference seems to work...
> >
> > My question is: how is it that IPython can find shawkle.py to import it,
> > edit it, and execute the edited code, but is unable to find the same file
> > in order to "run" it?
> >
> > I have tried adding '~/shawkle' to the search path as well
> > (using sys.path.append), but that does not fix the problem.
> >
> > I have also tried putting a special PYTHONPATH in my bash
> > profile, but that also does not seem to make a difference, and I
> > have commented it out (leaving just PATH).
> >
> > I use python 2.5.2-1 and cygwin 1.7.1-1 with Windows XP.

> _______________________________________________
> IPython-user mailing list
> IPython-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user

-- 
Tom Baker <tbaker@tbaker.de>


More information about the IPython-user mailing list