OT: Python Import question

John Arbash Meinel john at arbash-meinel.com
Mon Sep 3 17:24:13 BST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeremy Wilkins wrote:
> Hi,
> 
> I've been trying to tell one python script to import another, the
> problem is the other script is in a different non-child folder.
> Neither are in the default import path. I can't set PYTHONPATH before
> hand and when i set it inside a script it seems to have no effect, eg
> 
> import os
> os.environ['PYTHONPATH'] = '/folder/containing/secondscript'
> import script2
> 
> I'm stumped so any help much appreciated
> 
> thanks
> 
> jebw

By the time you are inside python you want to set sys.path

import sys

sys.path.append('/folder/containing/other/script')

or

sys.path.insert(0, '/folder/containing/other/script')

Just depending on whether you want python to search there first for
things (override if it is located elsewhere) or last (if it hasn't found
it, use it from there).

John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG3DUtJdeBCYSNAAMRAk0qAJ9cILFdFR6eHmPiq44kw1vB1VM6ggCg1sa0
hdrl4iINDz+ZltHKdM0NF30=
=e9cN
-----END PGP SIGNATURE-----



More information about the bazaar mailing list