0

I'm new to python/programming, so maybe I have been searching the wrong syntax for this question.

I'm working on some python scripts. For example, in one of the python scripts I am grabbing sensor data such as time, altitude, and pressure, let's call this getsensor.py

Then, I have another script that needs the data from "getsensor.py", let's call this "buildstring.py"

So, I wanted to have a simple overall python script, that would call "getsensor.py" and then call "buildstring.py"

I thought the way to do this would be to issue a "from getsensor import time, altitude, pressure" at the beginning of "buildstring.py" but this seems to not be what I am expecting...any advice or tips to point me in the correct direction? Trying to keep it pretty simple.

Edit: Seems as though by issuing an "import X" it is running that script. So when I do this, my X script contains some code which causes my scripts to just run in an indefinite loop. The only programming I've really done is assembly, so I'm not sure how to save data that can be shared among multiple python scripts

2
  • You should use getsensor.py as a module and include it in buildstring.py. You can then invoke the functions used in getsensor.py to obtain sensor readings in buildstring. Commented Mar 14, 2013 at 16:49
  • 1
    what do you mean with "this seems not to be what I am expecting"? errors? some code? Commented Mar 14, 2013 at 16:49

3 Answers 3

2

there are many ways to do this, an easy one is creating a centralized script and execute it to run your functions

you can import the functions necessary just like you listed in your questiong

#main.py    

from getsensor import time, altitude
from buildstring import your_function    

if __name__ == '__main__':
   result = altitude()
   your_function(result)

As you can see there are lots of ways to accomplish what you are tryign to do, It is most important to give the structure of your program and the relationships between modules much thought so that your program is extensible and maintainable

Sign up to request clarification or add additional context in comments.

5 Comments

So what is technically happening, is when you issue a "from getsensor import time,altitude" it is running that script and then returning to the next line of code...correct?
@user2170780 nope, it is importing those functions into the current namespace, not running them
So if it is not running them, then the values in those variables will be...what?
@user2170780, do you need to collect and store data? If so you could set up a python function to collect data from your sensors and persist it to a database like sqlite. You can run that script on interval. Then you can write functions independent of the collection process, that reach into the database and pull out the appropriate values?
Yes. I will be pulling data from sensors and a camera at a certain interval. Then I'll have to pass these values to a radio. I was hoping to implement it all in python, though, so to keep it simple for a beginner.
0

Normally its custom to have an entry point in each module, then you can do something like

from buildstring import app
from getsensor import main

result = app(1)
main(result)

Comments

0

Depends how/where you are storing the variables.

If you have them as global variables, see this solution.

However, I would try passing the variables from getsensor.py to buildstring.py via a method call.

For example, in buildstring.py write a method that takes the variables as an arguement:

def getVars(time, altitude, pressure):
    # Do stuff with variables here, or pass them to other local methods

And in getsensor.py:

x = 100
y = 2
z = 8

def passVars():
    import buildstring.py
    buildstring.getVars(x, y, z)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.