Re: [Tutor] problem importing class

2006-10-30 Thread Danny Yoo
> This only works for code that is in a package, when you want to import a
> module in the same package. It doesn't work for code that is not in a
> package. For example,
>
> F:\Tutor>cat site.py
> print 'imported site.py'
>
> F:\Tutor>cat siteimporter.py
> from __future__ import absolute_import
> from . import site
>
> F:\Tutor>py siteimporter.py
>
> F:\Tutor>C:\Python25\python siteimporter.py
> Traceback (most recent call last):
>   File "siteimporter.py", line 2, in 
> from . import site
> ValueError: Attempted relative import in non-package

Hi Kent,


Yikes!  Really?

Thank you for the correction; I completely misread the intention of PEP 
328 then.


But now I'm very miffed and unhappy.  Darn it, then that means this issue 
isn't repaired after all.  Most beginner programmers won't use packages, 
and so we'll still see the same problems pop up all the time.  Why is this 
restricted just to packages?  Let me see...

 http://mail.python.org/pipermail/python-list/2006-October/367267.html

Hmmm...  Is that the issue then?  It being the main module?  I'm now 
reading PEP 338 and looking at the section "Import Statements and the Main 
module":

 http://www.python.org/dev/peps/pep-0338/

... ok.  Good grief.  Then the current state of affairs still doesn't 
address the common case problem that we see on Tutor on module imports. 
The PEPs maintain that this is the situation for Python 2.5, emphasizing 
that this might be revisited in 2.6, which gives me some fleeting hope 
that this might get fixed in the future.

I apologize about giving the bad advice.  I was really hoping never to see 
another import problem again.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-30 Thread Kent Johnson
Danny Yoo wrote:
>>> This isn't the first time this kind of problem has hit people.  This
>>> problem is well known and is the subject of a Python Enhancement Proposal
>>> (PEP 328).  If you're using Python 2.5, you can use a new feature called
>>> 'absolute_import' to avoid the import problem.  See:
>>>
>>>  http://docs.python.org/whatsnew/pep-328.html
>>>
>>> for more details.
>> Hmm, I don't see how that would help since AFAICT shawn's site.py is not 
>> in a package. Or maybe I don't understand what you are suggesting?
> 
> Hi Kent,
> 
> I don't think I replied back to you on this one!  Python loads up 
> 'site.py' on startup if called without the '-S' option:
> 
>  http://docs.python.org/lib/module-site.html
> 
> So imagine if we're a program, and we run Python on it.  For some reason 
> --- at least on startup --- Python places precendence on the modules 
> located in Python's library directory.  So we pick up the Standard 
> Library's site.py, which is then cached in sys.modules like any other 
> module import.
> 
> Later on, if we try to 'import site', Python reuses the already-loaded 
> site.py (because it's already cached in sys.modules) , regardless if 
> there's a 'site.py' module in the current working directory.  The reason 
> PEP 328 can be a solution here is because, if the absolute_import feature 
> were on, we'd be able to do:
> 
>  from . import site
> 
> which would pick up the correct relative path.

This only works for code that is in a package, when you want to import a 
module in the same package. It doesn't work for code that is not in a 
package. For example,

F:\Tutor>cat site.py
print 'imported site.py'

F:\Tutor>cat siteimporter.py
from __future__ import absolute_import
from . import site

F:\Tutor>py siteimporter.py

F:\Tutor>C:\Python25\python siteimporter.py
Traceback (most recent call last):
   File "siteimporter.py", line 2, in 
 from . import site
ValueError: Attempted relative import in non-package

> 
> 
> Concretely, here's what probably happened with the original poster:
> 
> ##
> mumak:~ dyoo$ echo "print 'hi'" > site.py
> mumak:~ dyoo$ python
> Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
 import site
 print site.__file__
> /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site.pyc
> ###
> 
> Note that the 'site.py' in the current directory was not used.  I have no 
> idea why.  I suspect that this has some relation to the way that site.py 
> is handled by a special case in the Python interpreter, but I have really 
> no clue.

site.py is a special case just in being loaded automatically. Other than 
that this is standard behaviour. If you import a module that has already 
been imported, you will get a reference to the already-imported module, 
sys.path is not searched again. Since the library module site has 
already been imported when your program starts, 'import site' will 
always return a reference to the library module.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-27 Thread Danny Yoo
>> It looks like people have identified the problem, that your site.py 
>> module isn't being found because it conflicts with something from 
>> Python's Standard Library.
>>
>> This isn't the first time this kind of problem has hit people.  This
>> problem is well known and is the subject of a Python Enhancement Proposal
>> (PEP 328).  If you're using Python 2.5, you can use a new feature called
>> 'absolute_import' to avoid the import problem.  See:
>>
>>  http://docs.python.org/whatsnew/pep-328.html
>>
>> for more details.
>
> Hmm, I don't see how that would help since AFAICT shawn's site.py is not 
> in a package. Or maybe I don't understand what you are suggesting?

Hi Kent,

I don't think I replied back to you on this one!  Python loads up 
'site.py' on startup if called without the '-S' option:

 http://docs.python.org/lib/module-site.html

So imagine if we're a program, and we run Python on it.  For some reason 
--- at least on startup --- Python places precendence on the modules 
located in Python's library directory.  So we pick up the Standard 
Library's site.py, which is then cached in sys.modules like any other 
module import.

Later on, if we try to 'import site', Python reuses the already-loaded 
site.py (because it's already cached in sys.modules) , regardless if 
there's a 'site.py' module in the current working directory.  The reason 
PEP 328 can be a solution here is because, if the absolute_import feature 
were on, we'd be able to do:

 from . import site

which would pick up the correct relative path.


Concretely, here's what probably happened with the original poster:

##
mumak:~ dyoo$ echo "print 'hi'" > site.py
mumak:~ dyoo$ python
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> print site.__file__
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site.pyc
###

Note that the 'site.py' in the current directory was not used.  I have no 
idea why.  I suspect that this has some relation to the way that site.py 
is handled by a special case in the Python interpreter, but I have really 
no clue.


Python's module import lookup was handled in an implicit way which goes 
against its traditional "explicit instead of implicit" philosophy. 
That's why PEP 328 is so critically important: the name-collision problem 
gets much worse as either Standard Library or our own programs grow.  I'm 
amazed that we've been able to tolerate this situation for so long. 
*grin*
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Kent Johnson
Danny Yoo wrote:
> Hi Shawn,
> 
> It looks like people have identified the problem, that your site.py module 
> isn't being found because it conflicts with something from Python's 
> Standard Library.
> 
> This isn't the first time this kind of problem has hit people.  This 
> problem is well known and is the subject of a Python Enhancement Proposal 
> (PEP 328).  If you're using Python 2.5, you can use a new feature called 
> 'absolute_import' to avoid the import problem.  See:
> 
>  http://docs.python.org/whatsnew/pep-328.html
> 
> for more details.

Hmm, I don't see how that would help since AFAICT shawn's site.py is not 
in a package. Or maybe I don't understand what you are suggesting?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread shawn bright
Hey thanks for the help, gents,i renamed site.py to site_obj.py and my import and statement.everything is working now. Thank you guys very very much.shawnOn 10/26/06, 
Danny Yoo <[EMAIL PROTECTED]> wrote:
Hi Shawn,It looks like people have identified the problem, that your site.py moduleisn't being found because it conflicts with something from Python'sStandard Library.This isn't the first time this kind of problem has hit people.  This
problem is well known and is the subject of a Python Enhancement Proposal(PEP 328).  If you're using Python 2.5, you can use a new feature called'absolute_import' to avoid the import problem.  See: 
http://docs.python.org/whatsnew/pep-328.htmlfor more details.In the meantime, if you can't depend on using Python 2.5 yet, just renameyour site.py
 module to something else to avoid the collision betweenPython's own site.py module and your own.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Danny Yoo

Hi Shawn,

It looks like people have identified the problem, that your site.py module 
isn't being found because it conflicts with something from Python's 
Standard Library.

This isn't the first time this kind of problem has hit people.  This 
problem is well known and is the subject of a Python Enhancement Proposal 
(PEP 328).  If you're using Python 2.5, you can use a new feature called 
'absolute_import' to avoid the import problem.  See:

 http://docs.python.org/whatsnew/pep-328.html

for more details.

In the meantime, if you can't depend on using Python 2.5 yet, just rename 
your site.py module to something else to avoid the collision between 
Python's own site.py module and your own.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Kent Johnson
shawn bright wrote:
> hey there
> 
> i have written a module called site.py

Use a different name, there is a library module called site that is 
automatically imported when Python starts up. So when you 'import site' 
you are getting the already-imported library module.

Kent

> in the file i have this:
> 
> import DbConnector
> import sensor
> 
> class Site(object):
> "site object"
>
> def __init__(self, id):
> self.id  = id
> self.con = DbConnector.DbConnector()   
> id = str(self.id )
> stats = self.con.getOne("selct `group_id`, `name`, `ivr_code`, \
> `site_type`, `notes`, `sensor_id` \
> from `sites` where `id` = '"+str(id)+"' ")
> self.group_id = stats[0]
> self.name  = stats[1]
> self.ivr_code = stats[2]
> self.site_type = stats[3]
> self.notes = stats[4]
> self.sensor_id = stats[5]
>
> def get_sensor(self):
> self.sensor = sensor.Sensor (self.sensor_id)
> return self.sensor
> 
> ok, in the program i am trying to run i have these lines:
> import site
> new_site = site.Site(id_number)
> 
> and this is what my output is:
> Traceback (most recent call last):
>   File "./new_camp.py", line 2014, in on_site_tree_view_row_activated
> acitve_site = site.Site(id_number)
> AttributeError: 'module' object has no attribute 'Site'
> 
> It looks like the same code works for a different module i have ;
> i don't get what i could be missing here, any tips?
> 
> if you have read this far, thanks for your time.
> sk
> 
> 
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Jason Massey
Shawn,Python already has a module called site.  From http://docs.python.org/lib/module-site.html :This module is automatically imported during initialization.

The automatic import can be suppressed using the interpreter's
-S option.


Importing this module will append site-specific paths to the module
search path.Easiest thing would be to rename your module.On 10/26/06, shawn bright <[EMAIL PROTECTED]
> wrote:hey therei have written a module called site.pyin the file i have this:
import DbConnectorimport sensorclass Site(object):    "site object"        def __init__(self, id):
    self.id = id    self.con = DbConnector.DbConnector()        id = str(
self.id)    stats = self.con.getOne("selct `group_id`, `name`, `ivr_code`, \
    `site_type`, `notes`, `sensor_id` \    from `sites` where `id` = '"+str(id)+"' ")    self.group_id = stats[0]    
self.name = stats[1]
    self.ivr_code = stats[2]    self.site_type = stats[3]    self.notes = stats[4]    self.sensor_id = stats[5]        def get_sensor(self):    self.sensor = sensor.Sensor

(self.sensor_id)    return self.sensorok, in the program i am trying to run i have these lines:import sitenew_site = site.Site(id_number)and this is what my output is:Traceback (most recent call last):
  File "./new_camp.py", line 2014, in on_site_tree_view_row_activated    acitve_site = site.Site(id_number)AttributeError: 'module' object has no attribute 'Site'It looks like the same code works for a different module i have ;
i don't get what i could be missing here, any tips?if you have read this far, thanks for your time.sk

___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Simon Brunning
On 10/26/06, shawn bright <[EMAIL PROTECTED]> wrote:
> import site

Try this for a clue:

print site.__file__

-- 
Cheers,
Simon B
[EMAIL PROTECTED]
http://www.brunningonline.net/simon/blog/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] problem importing class

2006-10-26 Thread shawn bright
hey therei have written a module called site.pyin the file i have this:import DbConnectorimport sensorclass Site(object):    "site object"        def __init__(self, id):
    self.id = id    self.con = DbConnector.DbConnector()        id = str(self.id)    stats = self.con.getOne("selct `group_id`, `name`, `ivr_code`, \
    `site_type`, `notes`, `sensor_id` \    from `sites` where `id` = '"+str(id)+"' ")    self.group_id = stats[0]    self.name = stats[1]
    self.ivr_code = stats[2]    self.site_type = stats[3]    self.notes = stats[4]    self.sensor_id = stats[5]        def get_sensor(self):    self.sensor = sensor.Sensor
(self.sensor_id)    return self.sensorok, in the program i am trying to run i have these lines:import sitenew_site = site.Site(id_number)and this is what my output is:Traceback (most recent call last):
  File "./new_camp.py", line 2014, in on_site_tree_view_row_activated    acitve_site = site.Site(id_number)AttributeError: 'module' object has no attribute 'Site'It looks like the same code works for a different module i have ;
i don't get what i could be missing here, any tips?if you have read this far, thanks for your time.sk
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor