问题描述
我是 Python/编程的新手,所以也许我一直在为这个问题搜索错误的语法.
I'm new to python/programming, so maybe I have been searching the wrong syntax for this question.
我正在编写一些 Python 脚本.例如,在其中一个 python 脚本中,我正在抓取传感器数据,例如时间、高度和压力,我们称之为 getsensor.py
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
然后,我有另一个脚本需要来自getsensor.py"的数据,我们称之为buildstring.py"
Then, I have another script that needs the data from "getsensor.py", let's call this "buildstring.py"
所以,我想要一个简单的整体 python 脚本,它会调用getsensor.py"然后调用buildstring.py"
So, I wanted to have a simple overall python script, that would call "getsensor.py" and then call "buildstring.py"
我认为这样做的方法是在buildstring.py"的开头发出从 getsensor 导入时间、高度、压力",但这似乎不是我所期望的......任何建议或提示我指向正确的方向?尽量保持简单.
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.
似乎通过发出import X"来运行该脚本.所以当我这样做时,我的 X 脚本包含一些代码,这些代码导致我的脚本无限循环运行.我真正做过的唯一编程是汇编,所以我不确定如何保存可以在多个 python 脚本之间共享的数据
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
推荐答案
有很多方法可以做到这一点,一个简单的方法是创建一个集中的脚本并执行它来运行您的功能
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
这篇关于在python脚本之间传递变量的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!