Thank you! Okay, I think I'm starting to get a handle on the visibility of things. As you said, much different than C.
Just to confirm that I'm understanding correctly: Even through the import Adafruit_GPIO as GPIO line exists in the AdafruitInit.py file, which is imported by the import AdafruitInit line in main.py, main.py does not automatically inherit the Adafruit_GPIO. Which is why you indicate that I need to also import it in main.py On Wed, Sep 5, 2018 at 9:42 AM, Alan Gauld <alan.ga...@yahoo.co.uk> wrote: > On 05/09/18 14:05, Chip Wachob wrote: >> # module AdafruitInit.py >> # from the Adafruit tutorials.. >> import Adafruit_GPIO.FT232H as FT232H >> import Adafruit_GPIO as GPIO >> >> FT232H.use_FT232H() >> >> ft232h = FT232H.FT232H() >> >> # config settings for the SPI 'machine' >> spi = FT232.SPI(ft232h, 4, 20000, 2, FT232H.MSBFIRST) >> > > That last line created a new variable inside the AdafruitInit module. > It is not visible anywhere else. > >> # module RSI.py >> import AdafruitInit >> >> def write(byte): >> spi.write(byte) > > You use spi but it is not visible. It is inside AdafriuitInit > so you need to prefix the name (I'd suggest importing > as an alias!) > > AdafruitInit.spi.write(byte) > >> # toggle the latch signal >> ft232h.output(5, GPIO.LOW) >> ft232h.output(5, GPIO.HIGH) > > And the same here. ft232h is a variable inside AdafriotInit, > so you must prefix it. > > BUT GPIO is defined in some other module which > you will also need to import that.... > > import Adafruit_GPIO as GPIO > > adafruitInit.ft232h.output(5, GPIO.LOW) > > > Any name that you try to use must be > - defined in this module or > - imported or > - prefixed with the name of a module that you imported. > > If you do not define it in this module you must import > or prefix. > > >> ---------------module separator ------------------------ >> >> # module main.py >> import RSI >> import AdafruitInit >> >> ft232h.setup(5, GPIO.OUT) >> >> write(0xAA) > > Again you need to use the prefixes (and import GPIO) > > AdafruitInit.ft232h.setup(...) > RSI.write(...) > >> The actual error message for the ft232h... line is: >> >> NameError: global name 'ft232h' is not defined > > Please send the full error text, they contain a lot of useful > information. In this case the summary line is enough but in > future we really need the full text. > > The name error is because your main module cannpt see > ft232h defined anywhere - it is inside the other module. > > So, my write line should have read? >> RSI.write(0xAA) >> >> Does this mean that I need to call the ft232h like this? >> >> AdafruitInit.ft232h.setup(5, GPIO.OUT) > > Exactly. And import GPIO too. > >> Earlier in the thread you mentioned that there are several ways to >> import. Obviously there's small differences between the methods. If >> you know of a good resource I can use to get a better understanding / > > Since you know C already you should probably just read the official > tutorial. > It covers all the different styles. > > The only guidance i'll add is to avoid the > > from foo import * > > style since it easily leads to hard to spot name collisions. > Only use it for experiments in the >>> prompt. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor