Re: __file__ is sometimes absolute, sometimes relative

2010-10-01 Thread Antoine Pitrou
On Fri, 1 Oct 2010 21:00:02 +0200 Sébastien Barthélemy wrote: > Hi, > > Arnaud, Christian, thank you for your help. > > I'll use abspath, it's shorter. > > Any idea why it's sometimes absolute, sometimes not? AFAICT, that's because sys.path contains some absolute paths and some relative ones.

Re: __file__ is sometimes absolute, sometimes relative

2010-10-01 Thread Sébastien Barthélemy
Hi, Arnaud, Christian, thank you for your help. I'll use abspath, it's shorter. Any idea why it's sometimes absolute, sometimes not? -- http://mail.python.org/mailman/listinfo/python-list

Re: __file__ is sometimes absolute, sometimes relative

2010-10-01 Thread Arnaud Delobelle
Sébastien Barthélemy writes: > Hello, Hi! > I use a data file that lies on disk in the same directory that the > module which makes use of it. > > The data file is checked in the repository and gets installed by > the distutils ``package_data`` directive, so it is in place both > during develop

Re: __file__ is sometimes absolute, sometimes relative

2010-10-01 Thread Christian Heimes
Am 01.10.2010 13:00, schrieb Sébastien Barthélemy: > Hello, > > I use a data file that lies on disk in the same directory that the > module which makes use of it. > > The data file is checked in the repository and gets installed by > the distutils ``package_data`` directive, so it is in place bot

Re: __file__ access extremely slow

2009-06-06 Thread Zac Burns
I think I have figured this out, thanks for your input. The time comes from lazy modules related to e-mail importing on attribute access, which is acceptable. Hence of course why ImportError was sometime raised. I originally was thinking that accessing __file__ was triggering some mechanism that

Re: __file__ access extremely slow

2009-06-05 Thread Gabriel Genellina
En Fri, 05 Jun 2009 00:12:25 -0300, John Machin escribió: > (2) This will stop processing on the first object in sys.modules that > doesn't have a __file__ attribute. Since these objects aren't > *guaranteed* to be modules, Definitely not guaranteed to be modules. Python itself drops non-mo

Re: __file__ access extremely slow

2009-06-04 Thread John Machin
Steven D'Aprano REMOVE.THIS.cybersource.com.au> writes: > > On Fri, 05 Jun 2009 02:21:07 +, Steven D'Aprano wrote: > > > You corrected this to: > > > > for module in sys.modules.itervalues(): > >try: > >path = module.__file__ > >except (AttributeError, Impor

Re: __file__ access extremely slow

2009-06-04 Thread Terry Reedy
Zac Burns wrote: The section of code below, which simply gets the __file__ attribute of the imported modules, takes more than 1/3 of the total startup time. Given that many modules are complicated and even have dynamic population this figure seems very high to me. it would seem very high if one j

Re: __file__ access extremely slow

2009-06-04 Thread Steven D'Aprano
On Fri, 05 Jun 2009 02:21:07 +, Steven D'Aprano wrote: > You corrected this to: > > for module in sys.modules.itervalues(): >try: >path = module.__file__ >except (AttributeError, ImportError): >return > > (1) You're not importing anything insid

Re: __file__ access extremely slow

2009-06-04 Thread Gabriel Genellina
En Thu, 04 Jun 2009 22:24:48 -0300, Zac Burns escribió: The section of code below, which simply gets the __file__ attribute of the imported modules, takes more than 1/3 of the total startup time. Given that many modules are complicated and even have dynamic population this figure seems very hig

Re: __file__ access extremely slow

2009-06-04 Thread Steven D'Aprano
On Thu, 04 Jun 2009 18:24:48 -0700, Zac Burns wrote: > The section of code below, which simply gets the __file__ attribute of > the imported modules, takes more than 1/3 of the total startup time. How do you know? What are you using to time it? [...] > From once python starts and loads the main

Re: __file__ access extremely slow

2009-06-04 Thread Zac Burns
Sorry, there is a typo. The code should read as below to repro the problem: for module in sys.modules.itervalues(): try: path = module.__file__ except (AttributeError, ImportError): return

Re: __file__ vs __FILE__

2007-11-05 Thread Matimus
On Nov 5, 1:07 am, sandipm <[EMAIL PROTECTED]> wrote: > interestingly... > I wanted to reuse this code so i wrote function in a file > > def getParentDir(): > import os > return os.path.dirname(os.path.abspath(__file__)) > > and called this function, in another file, its giving me parent >

Re: __file__ vs __FILE__

2007-11-05 Thread sandipm
interestingly... I wanted to reuse this code so i wrote function in a file def getParentDir(): import os return os.path.dirname(os.path.abspath(__file__)) and called this function, in another file, its giving me parent directory of file where this function is defined.? how to reuse this

Re: __file__ vs __FILE__

2007-11-04 Thread Giampaolo Rodola'
On 3 Nov, 15:46, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Sat, 03 Nov 2007 10:07:10 -0300, Giampaolo Rodola' <[EMAIL PROTECTED]> > escribió: > > > On 3 Nov, 04:21, klenwell <[EMAIL PROTECTED]> wrote: > >> In PHP you have the __FILE__ constant which gives you the value of the > >> absol

Re: __file__ vs __FILE__

2007-11-03 Thread Bjoern Schliessmann
klenwell wrote: > Bjoern Schliessmann wrote: >> I use os.path.dirname(os.path.abspath(__file__)) > > That makes sense, as it is almost a literal translation of what > I'm used to using in PHP. Thanks for pointing this out. You're welcome, happy coding :) Regards, Björn -- BOFH excuse #286:

Re: __file__ vs __FILE__

2007-11-03 Thread klenwell
On Nov 3, 4:18 am, Bjoern Schliessmann wrote: > Jeff McNeil wrote: > > I've used the 'os.path.realpath(os.path.pardir)' construct in a > > couple of scripts myself. > > In Windows? Using Linux, this gives me "..". > > I use os.path.dirname(os.path.abspath(__file__)) > > > That ought to work within

Re: __file__ vs __FILE__

2007-11-03 Thread Jeff McNeil
I'm using Mac OS X, and it get: Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getcwd() '/Users/jeff' >>> os.path.realpath(os.path.pardir) '/Users'

Re: __file__ vs __FILE__

2007-11-03 Thread Gabriel Genellina
En Sat, 03 Nov 2007 10:07:10 -0300, Giampaolo Rodola' <[EMAIL PROTECTED]> escribió: > On 3 Nov, 04:21, klenwell <[EMAIL PROTECTED]> wrote: >> In PHP you have the __FILE__ constant which gives you the value of the >> absolute path of the file you're in (as opposed to the main script >> file.) >

Re: __file__ vs __FILE__

2007-11-03 Thread Giampaolo Rodola'
On 3 Nov, 04:21, klenwell <[EMAIL PROTECTED]> wrote: > I apologize in advance for coming at this from this angle but... > > In PHP you have the __FILE__ constant which gives you the value of the > absolute path of the file you're in (as opposed to the main script > file.) With the function dirname

Re: __file__ vs __FILE__

2007-11-03 Thread Bjoern Schliessmann
Jeff McNeil wrote: > I've used the 'os.path.realpath(os.path.pardir)' construct in a > couple of scripts myself. In Windows? Using Linux, this gives me "..". I use os.path.dirname(os.path.abspath(__file__)) > That ought to work within the interactive interpreter. Why do you also enter that cod

Re: __file__ vs __FILE__

2007-11-02 Thread Jeff McNeil
The __file__ attribute is present when you run a script from a file. If you run from the interactive interpreter, it will raise a NameError. Likewise, I believe that in earlier versions of Python (2.1? Pre 2.2?) it was only set within imported modules. I've used the 'os.path.realpath(os.pat

Re: __file__

2007-04-11 Thread 7stud
On Apr 11, 6:55 am, "John Machin" <[EMAIL PROTECTED]> wrote: > On Apr 11, 8:03 pm, "7stud" <[EMAIL PROTECTED]> wrote: > > > > > Hi, > > > Thanks for the response. > > > On Apr 11, 12:49 am, "Gabriel Genellina" <[EMAIL PROTECTED]> > > wrote: > > > > __file__ corresponds to the filename used to locat

Re: __file__

2007-04-11 Thread John Machin
On Apr 11, 8:03 pm, "7stud" <[EMAIL PROTECTED]> wrote: > Hi, > > Thanks for the response. > > On Apr 11, 12:49 am, "Gabriel Genellina" <[EMAIL PROTECTED]> > wrote: > > > > > __file__ corresponds to the filename used to locate and load the module, > > whatever it is. When the module is found on the

Re: __file__

2007-04-11 Thread 7stud
Hi, Thanks for the response. On Apr 11, 12:49 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > > __file__ corresponds to the filename used to locate and load the module, > whatever it is. When the module is found on the current directory > (corresponding to '' in sys.path), you get just t

Re: __file__

2007-04-10 Thread Gabriel Genellina
En Tue, 10 Apr 2007 21:20:51 -0300, 7stud <[EMAIL PROTECTED]> escribió: > I'm having trouble understanding what the definition of __file__ is. > With this program: > > -- > #data.py: > > def show(): > print __file__ > > if __name__ == "__main__": > show() > --- > > if I run data.