Json 部署后如何集成配置文件?
我有以下文件夹结构:Json 部署后如何集成配置文件?,json,python-3.x,configuration,config,Json,Python 3.x,Configuration,Config,我有以下文件夹结构: helloworld / │ ├── helloworld.py ├── conf.json ├── setup.py ├── parameterManager.py 功能非常基本- My helloworld.py从conf.json读取一个参数并打印它: from parameterManger import return_params as pm what_to_print = pm("print")
helloworld /
│
├── helloworld.py
├── conf.json
├── setup.py
├── parameterManager.py
功能非常基本-
My helloworld.py从conf.json读取一个参数并打印它:
from parameterManger import return_params as pm
what_to_print = pm("print")
print(what_to_print)
My parameterManager.py帮助我阅读json,如下所示:
import os
import josn
def return_params (ParameterName=None, conf_file_name='/conf.json',):
try:
ConfFolder = os.path.dirname(__file__)
ConfFile=ConfFolder + conf_file_name
with open(ConfFile) as json_data_file:
Data = json.load(json_data_file)
if ParameterName is None:
return Data
ParamterValue=Data[ParameterName]
return ParamterValue
except Exception as e:
print(e)
在我“部署”之前,它工作得非常完美
在另一个项目中,我使用git+
但我总是犯错误
'NoneType' object is not subscriptable
[Errno 2] No such file or directory: 'D:\\path_to_new_project\\venv\\lib\\site-packages\\conf.json'
我可以考虑“肮脏”的解决方案,但我100%确信有一种“pythonic”的方式在项目之间共享文件。一些人能告诉我正确的方式是什么吗?首先,我认为您应该将
helloworld
打包为一个包,比如:
helloworld
├── helloworld
│ ├── __init__.py
│ ├── config.json
│ ├── helloworld.py
│ └── parameterManager.py
└── setup.py
然后,将config.json
添加到setup.py
中的package\u数据中
从设置工具导入设置
...
包裹=[
“helloworld”
]
包_数据={
'helloworld':['config.json']
}
...
设置(
...
包裹=包裹,
包_数据=包_数据,
...
)
首先,我认为您应该将helloworld
打包为一个包,比如:
helloworld
├── helloworld
│ ├── __init__.py
│ ├── config.json
│ ├── helloworld.py
│ └── parameterManager.py
└── setup.py
然后,将config.json
添加到setup.py
中的package\u数据中
从设置工具导入设置
...
包裹=[
“helloworld”
]
包_数据={
'helloworld':['config.json']
}
...
设置(
...
包裹=包裹,
包_数据=包_数据,
...
)
您如何“部署”它?您能提供更多信息吗?在deploy中,我的意思是-与我的团队/同事共享项目,他们可以“pip安装helloworld”(不是以pypi方式,而是通过git)。这就是setup.py如何“部署”它的原因?您能提供更多信息吗?在deploy中,我的意思是-与我的团队/同事共享项目,他们可以“pip安装helloworld”(不是以pypi方式,而是通过git)。这就是setup.py的原因