[Tutor] Re: newbie question on sys.path.append()
Lee Harr
missive at hotmail.com
Thu Nov 6 22:42:24 EST 2003
as the program I am working on grows, I would like to store some of the
modules that are imported
>in the main program file in a separate subdirectory and add the
>subdirectory to sys.path.
>I used sys.path.append() for this at the very beginning of this file,
>like:
>
>#!/usr/bin/env python
>from Tkinter import *
>import tkMessageBox, tkSnack, sys, os, fileinput, Tix
>sys.path.append(os.path.join(sys.path[0], 'widgets'))
>
>so I can import the modules I stored in the "widgets" subdirectory.
>
>I am not sure now if this might cause problems, maybe there is a more
>proper way to import those
>modules.
>
Make your subdirectory a package by adding a file called __init__.py
Like this:
#foo/bar.py
x = 5
#baz.py
from foo import bar
print bar.x
$python baz.py
Traceback (most recent call last):
File "baz.py", line 1, in ?
from foo import bar
ImportError: No module named foo
$touch foo/__init__.py
$python baz.py
5
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
More information about the Tutor
mailing list