envx
介绍
环境信息管理模块,适用于跨设备区分环境类型的情况
安装教程
- pip安装
pip3 install envx
- pip安装(使用淘宝镜像加速)
pip3 install envx -i https://mirrors.aliyun.com/pypi/simple
使用说明
- 环境文件目录(如果没有对应目录,请创建对应的目录)
-
Windows 系统
使用目录:C:\env\
-
macOS 系统
使用目录:/Users/env/
-
Linux 系统
使用目录:/env/
- 环境文件案例
- 文件名:local.any.env
- 文件内容:
HOST=192.168.0.1 PORT=6379
- 读取结果:
{ "HOST": "192.168.0.1", "PORT": "6379" }
- 项目中使用代码
import envx
local_any_env = envx.read('local.any.env')
- 建议使用环境类型标记文件:DEFAULT_ENV.env
一般用来描述当前的环境信息,可以用来标记当前的环境类型,
然后后续使用时先去读当前默认的环境类型,再根据设定选取不同的环境文件,
即,会先从当前环境中读取DEFAULT_ENV.env中设定的环境类型,再读取settings.ini的具体设定,
然后根据当前环境类型取对应的环境文件;
注意:settings.ini文件必须和使用此代码的文件在同一目录,若为其他目录,需修改文件路径
- settings.ini
[DEFAULT]
env=DEV
msg=在这里的env中设置将要使用哪个具体的环境
[DEV]
env_name=DEV-开发
env_file_name_mysql=dev.mysql.env
env_file_name_mongo=dev.mongo.env
silence_mysql=False
[TEST]
env_name=TEST-测试
env_file_name_mysql=test.mysql.env
env_file_name_mongo=test.mongo.env
silence_mysql=False
[PROD]
env_name=PROD-生产
env_file_name_mysql=prod.mysql.env
env_file_name_mongo=prod.mongo.env
silence_mysql=False
- 项目代码
# ----------------- 读取配置信息 -----------------
import configparser
import platform
import os
if platform.system() == 'Windows':
path_separator = '\\'
else:
path_separator = '/'
config_dir = (os.path.dirname(os.path.realpath(__file__))) + path_separator + 'settings.ini'
config = configparser.ConfigParser() # 类实例化
config.read(config_dir, encoding='utf-8')
default_config = config['DEFAULT']
default_env = default_config['env']
config_config = config[default_env]
# 从这里开始写需要读取为内容
env_file_name_mysql = config_config['env_file_name_mysql']
env_file_name_mongo = config_config['env_file_name_mongo']
silence_mysql = eval(config_config['silence_mysql'])
# ----------------- 读取配置信息 -----------------
Metadata
Release files for envx 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| envx-1.0.2.tar.gz | 5.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| envx-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / envx-1.0.2.tar.gz
| Download URL | envx-1.0.2.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1da846eec083f882d8cbf9c39e0c816553c68b854cf4962d3a57e1b7a7b5e6cc
|
|
BLAKE2b-256 checksum How to use checksums |
62ec90d816503e8b8e414d414068c6c0248bfedac16da25676a1adf0d4c35f31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.10
|
Release files / envx-1.0.2-py3-none-any.whl
| Download URL | envx-1.0.2-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5308d729730209fc11b41d6ef672fef7665977164613a4bfc9ad971b056f9a3
|
|
BLAKE2b-256 checksum How to use checksums |
299dc412d3efbeae14df9dc9d50f1db24bac27e0c473a5021a08e2cda520f51b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.10
|
Security for envx 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.