[Tutor] Working Directory

dman dman@dman.ddts.net
Sat, 4 May 2002 12:04:08 -0500


--98e8jtXdkpgskNou
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, May 04, 2002 at 11:51:21AM +0100, Russell Bungay wrote:
| Lloyd Kvam wrote:
| >  >>> import mx.DateTime
| >  >>> mx.DateTime.__file__
| > 'F:\\Python22\\Lib\\site-packages\\mx\\DateTime\\__init__.pyc'
|=20
| > I hope this fits the bill.  I believe that if the module comes
| > from your current directory, it simply returns the file name.
|=20
| Thankyou, my code now looks like:
|=20
| ### gofish.py
|=20
| import bmdutils,ConfigParser,os,os.path,sys
|=20
| gofishdir =3D os.path.dirname(__file__)
| sys.path.append(gofishdir)
|=20
| import cards #module in same directory as gofish.py

When importing a module, python first looks in the same directory as
the module doing the importing.  You really don't need to be futzing
with sys.path for that.

Eg:

$ pwd
/home/dman

$ ls /tmp/foo
mod1.py
mod2.py

$ cat /tmp/foo/mod1.py

import mod2

print "mod1 done"

print
import sys
print sys.path

$ cat /tmp/foo/mod2.py

print "mod2 done"


$ python -S /tmp/foo/mod1.py

mod2 done
mod1 done

['/tmp/foo', '/usr/lib/python2.1/', '/usr/lib/python2.1/plat-linux2',
'/usr/lib/python2.1/lib-tk', '/usr/lib/python2.1/lib-dynload']

$


As you can see, the directory the initial module was in (/tmp/foo) was
included as the first thing in sys.path.

-D

--=20

Yes, Java is so bulletproofed that to a C programmer it feels like being in=
 a
straightjacket, but it's a really comfy and warm straightjacket, and the wo=
rld
would be a safer place if everyone was straightjacketed most of the time.
                                                      -- Mark 'Kamikaze' Hu=
ghes
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--98e8jtXdkpgskNou
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzUFIgACgkQO8l8XBKTpRSCygCfVK+RIoblV8fNyxZ7Vq2UM8vO
Te8AoLq3lRQX0HvuiUyANLymyMWnAfe/
=uzZT
-----END PGP SIGNATURE-----

--98e8jtXdkpgskNou--