简体   繁体   中英

Using variables defined in main python script in imported script

I'm writing an automated internet speed testing program and I've setup a secondary script, config.py, to make it simpler for the user to edit the configuration.

The program can send tweets when the internet speed results falls below a certain point and I want to give the users the ability to edit the tweet. However the user wil likely want to include the results in the tweet which will be defined in the script within which config.py is called.

How can I use the variables from the main script in config.py?

Edit: Should've mentioned the variables in the main script are also in functions.

如果变量未封装到函数中,则可以from main_script import variable执行操作。

__main__ provides the namespace of the initial starting script:

# config.py
import __main__
print __main__.some_script_variable

Or you could use an extra module, eg cfgdata.py , which you import from main script and config.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM