[BangPypers] How to run *.py scripts which interfaces with modules

steve steve at lonetwin.net
Thu Jul 15 00:30:14 CEST 2010


Hi,

On 07/14/2010 07:43 PM,  स्वक्ष  wrote:
> On Wed, Jul 14, 2010 at 18:04, Naresh Khalasi<nkhalasi at vayana.in>  wrote:
>>  Hi,
>>
>>  The root directory (where your package starts) should be added to PYTHONPATH
>>  environment variable.
>>  e.g. if your root directory is /home/<username>/projects/mailman
>>  export PYTHONPATH=$PYTHONPATH:/home/<username>/projects/mailman
>>
> [...snip...]
> This results in the "ImportError" (mentioned earlier).
>
> Besides "from Mailman import Mailbox" in turn refers to email and its
> path is: sys.path.append("/home/<$user>/mailman-2.1.10/misc/email-2.58/email/").
>
> So if I set this path inside each of of these *.pyfiles,  using   ---
> sys.path.append("/home/<$user>/mailman-2.1.10/Mailman/")  command,
> etc...<==  I'm able to test-run it. But without the path setting,
> the program fails throwing errors.

That is what Naresh was trying to explain. If you want to set the path which the 
python interpreter would look at 'outside' the python modules, you can set the 
environment variable PYTHONPATH.

eg: instead of doing a sys.path.append('/home/<$user>/...') do a:

$ export PYTHONPATH="/home/<$user>/...:/some/other/path:..."

...before executing your python script/application.

If you've ever used a Unix based system you'll recognize that the $PYTHONPATH 
variable is similar to the $PATH variable, in that the interpreter (sh or 
python) looks at this variable to find the command/script/module to execute/import.

There is another way to fix this issue -- the .pth file/site.py. More 
information about this issue at:

http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH

http://docs.python.org/library/site.html
http://bob.pythonmac.org/archives/2005/02/06/using-pth-files-for-python-development/

cheers,
- steve
-- 
random spiel: http://lonetwin.net/
what i'm stumbling into: http://lonetwin.stumbleupon.com/


More information about the BangPypers mailing list