如何在许多文件中导入常量 [英] How-to import constants in many files
问题描述
我有一个包含许多模块的包。每个模块使用我在每个文件中独立定义的常量。
但是,所有这些常量必须相互一致。所以我尝试在一个文件中定义它们并将其导入每个文件中。当我运行它时,我找不到常量的错误。
I have a package containing many modules. Each module uses constants that I have defined independently in each file. However, all these constants have to be constistent with each other. So I try to define them in a single file and import it in each file. When I run it I have errors for constants not found.
它们是一种干净的方式让一个文件被许多其他文件导入并包含常量吗?
Is their a clean way to have a single file imported by many others and containing constants ?
感谢您的帮助
推荐答案
您可以在一个文件中声明所有常量,比如说 constants.py
然后将它们导入其他人。以下是一个示例:
You can declare all your constants in one file, say constants.py
and then import them into others. Here is an example:
# constants.py
FOO = 'foo'
PI = 3.14
# main.py
import constants
print constants.PI
这篇关于如何在许多文件中导入常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!