多Python脚本模块导入、跨脚本变量访问及流程设计咨询
Hey there! Let's break this down into two parts: the right ways to share those var1 to var100 variables, and the common missteps in your execution flow that might be blocking you.
Correct Ways to Access setup.py Variables
First, treat setup.py as a configuration module (not just a script) so other files can import its variables directly. Here's how to do it properly:
1. Structure setup.py for Module Import
Make sure your variables are defined at the global level in setup.py, and wrap any runnable code in an if __name__ == "__main__": block (so it only runs when you execute setup.py directly, not when importing it). Example:
# setup.py # Global configuration variables var1 = "database_url" var2 = 5000 var3 = {"debug": True, "log_level": "INFO"} # ... continue to var100 # Only run this code if setup.py is executed directly (not imported) if __name__ == "__main__": print("Running setup initialization directly") # Optional: Add setup tasks here (like creating directories)
2. Import Variables in Other Scripts
In master.py, core1.py, or core2.py, import the variables you need explicitly:
# master.py # Import specific variables (recommended for clarity) from setup import var1, var2, var3 # Or import the entire module to access variables via setup.varX import setup def main(): print(f"Master using var1: {var1}") print(f"Master using var100: {setup.var100}") if __name__ == "__main__": main()
For core1.py/core2.py, the pattern is identical:
# core1.py from setup import var4, var5 def process_data(): print(f"Core1 using var4: {var4}")
If you have 100 variables, you can group them into a single dictionary to avoid messy imports:
# setup.py config = { "var1": "value1", "var2": 123, # ... up to var100 } # Then import the dictionary elsewhere from setup import config print(config["var1"])
Common Execution Flow Mistakes to Fix
Here are the most likely errors in your current setup that are preventing variable access:
Mistake 1: Running setup.py as a separate process instead of importing it
If you're usingsubprocess.run()oros.system()to executesetup.pyfrommaster.py, you're creating a separate process—variables from that process won't be available in your main script. Always importsetup.pyas a module instead.Mistake 2: Defining variables inside functions (not globally)
If yourvar1-var100are defined inside a function insetup.py(e.g.,def load_vars(): var1 = "x"), they're not accessible outside that function. Move them to the global scope, or return them from the function and assign them in your importing scripts.Mistake 3: Circular imports
Ifsetup.pyimportscore1.pyormaster.py, and those files importsetup.py, you'll get anImportError. Avoid circular dependencies by makingsetup.pya pure config module with no imports of your other scripts.Mistake 4: setup.py isn't in Python's path
If your scripts are in different directories, Python might not findsetup.pywhen you try to import it. Fix this by adding the directory containingsetup.pytosys.pathbefore importing:# master.py (if setup.py is in a parent directory) import sys from pathlib import Path sys.path.append(str(Path(__file__).parent.parent)) import setupMistake 5: Variable name collisions
If any of your other scripts define a variable with the same name as one insetup.py, it will overwrite the imported variable. Use unique names (e.g., prefix setup variables withsetup_likesetup_var1) or use the dictionary approach to avoid conflicts.
内容的提问来源于stack exchange,提问作者abc

