[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Handling global variables (Newbie

David S

3/13/2008 4:25:00 AM

Hi,

I have an error occurring at
self.build_root = os.path.abspath(os.path.split(__file__)[0])

The error states 'NameError: global name '__file__' is not defined'

In Python 2.5 I ran my script as a module in IDLE gui. How does _file_ get
defined?

Yours,
David


3 Answers

MRAB

3/14/2008 7:19:00 PM

0

On Mar 13, 4:25 am, "David S" <dav...@evertech.com.au> wrote:
> Hi,
>
> I have an error occurring at
> self.build_root = os.path.abspath(os.path.split(__file__)[0])
>
> The error states 'NameError: global name '__file__' is not defined'
>
> In Python 2.5 I ran my script as a module in IDLE gui. How does _file_ get
> defined?
>
Do you perhaps mean '__name__'?

Marc 'BlackJack' Rintsch

3/15/2008 6:04:00 AM

0

On Fri, 14 Mar 2008 12:19:18 -0700, MRAB wrote:

> On Mar 13, 4:25 am, "David S" <dav...@evertech.com.au> wrote:
>> Hi,
>>
>> I have an error occurring at
>> self.build_root = os.path.abspath(os.path.split(__file__)[0])
>>
>> The error states 'NameError: global name '__file__' is not defined'
>>
>> In Python 2.5 I ran my script as a module in IDLE gui. How does _file_ get
>> defined?
>>
> Do you perhaps mean '__name__'?

I guess not. It makes more sense to apply path functions to `__file__`
than to `__name__`.

Ciao,
Marc 'BlackJack' Rintsch

Gabriel Genellina

3/15/2008 8:26:00 AM

0

En Thu, 13 Mar 2008 02:25:12 -0200, David S <davids@evertech.com.au>
escribi�:

> I have an error occurring at
> self.build_root = os.path.abspath(os.path.split(__file__)[0])
>
> The error states 'NameError: global name '__file__' is not defined'
>
> In Python 2.5 I ran my script as a module in IDLE gui. How does _file_
> get
> defined?

If you type your code directly into IDLE (or the commmand line Python
interpreter) __file__ doesn't even exist; it only has any sense when the
code is read from an actual file.
Save your code on disk, let's say you name it example.py
Then you can execute it using
> python example.py
from the command line; from inside IDLE, go to File | Open, locate
example.py, press F5

--
Gabriel Genellina