Interesting Problem

Stephen Hansen apt.shansen at gmail.com
Wed Jan 20 11:30:07 EST 2010


On Wed, Jan 20, 2010 at 7:19 AM, Victor Subervi <victorsubervi at gmail.com>wrote:

> Hi;
> I think I finally have an interesting problem for y'all. I need to import a
> script from a lower dir, forcing me to change dirs:
>

Don't do that. If you must, then the correct way to do it is to adjust your
sys.path and not change directory. "sys.path.append('..')" or whatever.

But I really would just re-organize your code so you don't need such things.
Have a single top-level directory that's on your path, put a blank
__init__.py in all your directories, and use absolute imports everywhere.

from myapp.templateFrame import top, bottom
from myapp.some_directory.some_file import this, that

etc.



> Now, apparently because of python's problem with globals, when I call "id"
> as follows:
>
>
Python does not have problems with globals. You are repeatedly re-using the
same variable names, causing confusion and errors; when you say 'id' in that
line of code, Python doesn't think you are talking about the "id" that is
global. It thinks you are talking about the "id" that is local-- that you
define one line beneath.

Local has precedence over global. Don't shadow global variables (and 'id' is
a bad name, as it shadows a builtin variable)


--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100120/352f5f9e/attachment-0001.html>


More information about the Python-list mailing list