[Tutor] Fwd: Accessing variables from other modules

Alan Gauld alan.gauld at yahoo.co.uk
Wed Sep 5 04:53:30 EDT 2018


On 05/09/18 04:12, Chip Wachob wrote:

> # module RSI.py
> def write(byte):
>    spi.write(byte)

You don't have any import statements here.
You need to import spi to use it.

> # toggle the latch signal
>    ft232h.output(5, GPIO.LOW)
>    ft232h.output(5, GPIO.HIGH)

And the same for ft232h

> 
> # module main.py
> import RSI
> import AdafruitInit.py

Note you do NOT use the .py extension when
importing, just the base name.

> ft232.setup(5, GPIO.OUT)

But again you have not imported ft232
Also I note that you use ft232 here but ft232h elsewhere.
Is that correct?

You must import any external names that
you intend to use.

> write(0xAA)

And here you need to prefix with RSI:

import RSI

....

RSI.write(....)

A Python import is very different to a C include.
In C you actually include the source text in your
file so everything inside the file becomes visible.
In a Python import you add names to a dictionary.
In this case the only name added is RSI. The code
inside the RSI module is effectively invisible to
your main.py, only the name of the module is seen.
So you must prefix the RSI contents before you use it.


> "write()" tells me that
> 
> "global name 'ft232h' is not defined"

Please always post full error texts, never summarize.

> Regarding permissions.  I'm not sure about the need for sudo, but that
> was used in the example.  

I suspect it's needed because you are accessing
privileged IO ports. It would not normally be
needed to run a Python script.

> I had been working most of the day on Friday and the scripts were
> running fine.  

That is the real mystery since the above code
should not have worked.

> Does some of the FTDI (AdafruitInit.py) remain resident

No, it will be deleted.

-- 
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




More information about the Tutor mailing list