[Tutor] Accessing variables from other modules
Chip Wachob
wachobc at gmail.com
Tue Sep 4 11:10:36 EDT 2018
Hello,
Hoping that this comes through as text only. Not sure how to force
that with Gmail.
Very new to Python and trying to follow the instructions I've read on
the tutorial and other places. But, I'm not meeting with any success.
I have a feeling this is something simple but a search of the archives
didn't provide any useful results. Perhaps I just don't know the
'lingo' yet. So I apologize for the likelihood that this is a
duplicate.
I have a background in C and other asm languages so that should give
you some idea of my current understanding related to programming.
Forgive any incorrect use of terms..
I started writing code in Python for a little project I'm working on.
I used examples from the tutorial provided by the DIY board (Adafruit
FT232H Breakout). Those instructions were the basis for my working
code.
As time went on, I had a _whole_ lot of code and it was getting
unmanageable. So I looked into breaking it into separate modules
(like I would do in C). I then used the import statement to 'include'
them into the main.py file.
I ran into a problem in that the code which was moved to the module
could no longer 'see' the ft232h variable (object?). After many
attempts, I figured out that the best solution seemed to be to put the
ft232h setup code into yet another file. I then imported that file
into both my main.py and foo.py files. And, that seemed to work.. on
Friday.
This morning, I came back to continue working on the code, and now the
ft232h variable can no longer be 'seen' by my modules...
When I was running the code on Friday it was being run from a command
line each time. So, I'm assuming that the dictionary disappears and
that there is a new 'fresh' start each time I execute. Is this
correct?
I'm running Python 2.7 and it is running on a native Ubuntu machine at
16.04 LTS.
I'll refrain from posting a bunch of code, but here is the 5000 foot view:
main.py - calls various functions from other *.py files (foo.py, etc).
It also creates a console-based menu selection which determines which
of the functions to call.
foo.py (and others) - contain the actual functions for the different
work that I want to get done.
spi.py - contains the setup and initialization code for the Adafruit
board and configures it to function as a SPI peripheral via USB.
execution looks like this:
$ sudo python main.py
So, the important questions are:
- Was I mislead by the fact that there was a power cycle on the
machine and it has now forgotten something that was staying resident
when I tested the code on Friday? Or, does each 'run' of the script
start fresh?
- What approach do I need to use to be able to initialize the
interface in spi.py and have it be something that is accessible to all
the other modules in my project?
Thank you in advance for your time.
More information about the Tutor
mailing list