[Tutor] How Do I Make Imports Work
bob gailer
bgailer at alum.rpi.edu
Thu Dec 27 15:55:52 CET 2007
james.homme at highmark.com wrote:
> Hi,
> Here is the offending code and error.
>
> # elevator_system.py
> # By Jim Homme
> # Simulate the behavior of a group of elevators.
> # Try to import and get error.
> # The module and this file are in the same directory.
> import elevator
> # The elevator module
> # This would be in a separate file.
> # When I remove comments, it works in this file.
>
>
> # class Elevator(object):
> # """Simulate the behavior of an elevator"""
> # def show_state(self):
> # """Show what floor the elevator is on"""
> # print "I am on floor 1"
> #
>
> e = Elevator()
>
Objects in an imported module must be qualified with the module name. So
replace the previous line with:
e = elevator.Elevator()
As an alternative you can
from elevator import *
which brings the objects into the main namespace and then
e = Elevator()
works but that usage is usually undesirable for several very good reasons.
> e.show_state()
>
> raw_input("\n\nPress the enter key to exit.")
>
> ====================
>
> Error Message
>
> Traceback (most recent call last):
>
> File "c:\scripts\ELEVAT~1.PY", line 19, in <module>
>
> e = Elevator()
>
> NameError: name 'Elevator' is not defined
>
> James D Homme, , Usability Engineering, Highmark Inc.,
> james.homme at highmark.com, 412-544-1810
>
> "Never doubt that a thoughtful group of committed citizens can change the
> world. Indeed, it is the only thing that ever has." -- Margaret Mead
>
>
>
>
> "Rob Andrews"
> <rob.andrews at gmai
> l.com> To
> Sent by: "Python Tutor" <tutor at python.org>
> tutor-bounces at pyt cc
> hon.org
> Subject
> Re: [Tutor] How Do I Make Imports
> 12/27/2007 09:04 Work
> AM
>
>
>
>
>
>
>
>
>
> Can you show us an example of the code where you're attempting to
> import and the error you get?
>
> -Rob A.
>
> On Dec 27, 2007 7:40 AM, <james.homme at highmark.com> wrote:
>
>> Hi,
>> I am just starting to learn about making classes, so I wanted to put some
>> code into a module and use it. I know the code works because when I put
>>
> it
>
>> in the same file that calls it, it does what it's supposed to do. When I
>> move it into its own file and try to use an import statement to bring it
>> in, I get errors that say that methods don't exist. I looked at a Windows
>> tutorial that instructs me to edit my registry. The tutorial seemed to be
>> talking about the version of Python from python.org. I have ActiveState
>> Python. I'm OK with editing the registry, but I'd rather not do it. If I
>> have to, is there documentation somewhere that helps me work with
>> ActiveState Python to do this. If I don't have to, where can I find
>> documentation that helps me make this work?
>>
>> Thanks lots.
>>
>> Jim
>>
>> James D Homme, , Usability Engineering, Highmark Inc.,
>> james.homme at highmark.com, 412-544-1810
>>
>> "Never doubt that a thoughtful group of committed citizens can change the
>> world. Indeed, it is the only thing that ever has." -- Margaret Mead
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>
>
> --
> "Quise ahogar mis penas, pero las muy putas flotan"
> SabidurĂa popular
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list