Python config module install

Welcome to python-configuration’s documentation!¶

This library is intended as a helper mechanism to load configuration files hierarchically. Current format types are:

  • Python files
  • Dictionaries
  • Environment variables
  • Filesystem paths
  • JSON files
  • INI files
  • dotenv type files
  • YAML files
  • TOML files
  • Azure Key Vault credentials
  • AWS Secrets Manager credentials
  • GCP Secret Manager credentials

Installing¶

$ pip install python-configuration

To include the optional TOML and/or YAML loaders, install the optional dependencies toml and yaml . For example,

$ pip install python-configuration[toml,yaml] 

Getting started¶

This library converts the config types above into dictionaries with dotted-based keys. That is, given a config cfg from the structure

we are able to refer to the parameter above as any of

and extract specific data types such as dictionaries:

This is particularly useful in order to isolate group parameters. For example, with the JSON configuration

 "database.host": "something", "database.port": 12345, "database.driver": "name", "app.debug": true, "app.environment": "development", "app.secrets": "super secret", "logging":  "service": "service", "token": "token", "tags": "tags" > > 

one can retrieve the dictionaries as

cfg.database.as_dict() cfg.app.as_dict() cfg.logging.as_dict() 
dict(cfg.database) dict(cfg.app) dict(cfg.logging) 

Configuration¶

There are two general types of objects in this library. The first one is the Configuration , which represents a single config source. The second is a ConfigurationSet that allows for multiple Configuration objects to be specified.

Single Config¶

Python Files¶

To load a configuration from a Python module, the config_from_python() can be used. The first parameter must be a Python module and can be specified as an absolute path to the Python file or as an importable module.

Optional parameters are the prefix and separator . The following call

config_from_python('foo.bar', prefix='CONFIG', separator='__') 

will read every variable in the foo.bar module that starts with CONFIG__ and replace every occurrence of __ with a . . For example,

# foo.bar CONFIG__AA__BB_C = 1 CONFIG__AA__BB__D = 2 CONF__AA__BB__D = 3 

would result in the configuration

Note that the single underscore in BB_C is not replaced and the last line is not prefixed by CONFIG .

Dictionaries¶

Dictionaries are loaded with config_from_dict() and are converted internally to a flattened dict .

Environment Variables¶

Environment variables starting with prefix can be read with config_from_env() :

config_from_env(prefix, separator='_') 

Filesystem Paths¶

Folders with files named as xxx.yyy.zzz can be loaded with the config_from_path() function. This format is useful to load mounted Kubernetes ConfigMaps. or Secrets.

JSON, INI, .env, YAML, TOML¶

JSON, INI, YAML, TOML files are loaded respectively with config_from_json() , config_from_ini() , config_from_dotenv() , config_from_yaml() , and config_from_toml() . The parameter read_from_file controls whether a string should be interpreted as a filename.

Caveats¶

In order for Configuration objects to act as dict and allow the syntax dict(cfg) , the keys() method is implemented as the typical dict keys. If keys is an element in the configuration cfg then the dict(cfg) call will fail. In that case, it’s necessary to use the cfg.as_dict() method to retrieve the dict representation for the Configuration object.

The same applies to the methods values() and items() .

Configuration Sets¶

Configuration sets are used to hierarchically load configurations and merge settings. Sets can be loaded by constructing a ConfigurationSet object directly or using the simplified config() function.

To construct a ConfigurationSet , pass in as many of the simple Configuration objects as needed:

cfg = ConfigurationSet( config_from_env(prefix=PREFIX), config_from_json(path, read_from_file=True), config_from_dict(DICT), ) 

The example above will read first from Environment variables prefixed with PREFIX , and fallback first to the JSON file at path , and finally use the dictionary DICT .

The config() function simplifies loading sets by assuming some defaults. The example above can also be obtained by

cfg = config( ('env', PREFIX), ('json', path, True), ('dict', DICT), ) 

or, even simpler if path points to a file with a .json suffix:

cfg = config('env', path, DICT, prefix=PREFIX) 

The config() function automatically detects the following:

  • extension .py for python modules
  • dot-separated python identifiers as a python module (e.g. foo.bar )
  • extension .json for JSON files
  • extension .yaml for YAML files
  • extension .toml for TOML files
  • extension .ini for INI files
  • extension .env for dotenv type files
  • filesystem folders as Filesystem Paths
  • the strings env or environment for Environment Variables

Merging Values¶

ConfigurationSet instances are constructed by inspecting each configuration source, taking into account nested dictionaries, and merging at the most granular level. For example, the instance obtained from cfg = config(d1, d2) for the dictionaries below

d1 = 'sub': 'a': 1, 'b': 4>> d2 = 'sub': 'b': 2, 'c': 3>> 

is such that cfg[‘sub’] equals

Note that the nested dictionaries of ‘sub’ in each of d1 and d2 do not overwrite each other, but are merged into a single dictionary with keys from both d1 and d2 , giving priority to the values of d1 over those from d2 .

Caveats¶

As long as the data types are consistent across all the configurations that are part of a ConfigurationSet , the behavior should be straightforward. When different configuration objects are specified with competing data types, the first configuration to define the elements sets its datatype. For example, if in the example above element is interpreted as a dict from environment variables, but the JSON file specifies it as anything else besides a mapping, then the JSON value will be dropped automatically.

Other Features¶

String Interpolation¶

When setting the interpolate parameter in any Configuration instance, the library will perform a string interpolation step using the str.format syntax. In particular, this allows to format configuration values automatically:

Extras¶

The contrib package contains extra implementations of the Configuration class used for special cases. Currently the following are implemented:

    AzureKeyVaultConfiguration in azure , which takes Azure Key Vault credentials into a Configuration -compatible instance. To install the needed dependencies execute

pip install python-configuration[azure] 
pip install python-configuration[aws] 
pip install python-configuration[gcp] 

Developing¶

To develop this library, download the source code and install a local version.

Features¶

  • Load multiple configuration types
  • Hierarchical configuration
  • Ability to override with environment variables
  • Merge parameters from different configuration types

Contributing¶

If you’d like to contribute, please fork the repository and use a feature branch. Pull requests are welcome.

Licensing¶

The code in this project is licensed under MIT license.

Источник

Modulenotfounderror: no module named ‘config’

Modulenotfounderror no module named

The ModuleNotFoundError is an error that will occur if the python interpreter cannot find the installed module in your computer.

When you are encountered this error no module named config.

Then in this tutorial you’ll be able to know the solutions to solve the error modulenotfounderror no module named ‘config’

What is config module in Python?

The config module in Python can be used to create a custom-built Python.

For example, the Python configuration can be used in your environment variables and the command line arguments.

The Confined Configuration will be able to used to install Python into an application system.

Why modulenotfounderror no module named ‘config’ error occurs?

  • It is either the spelling is incorrect or the capitalization of the module name.
  • The config package it is hasn’t been installed in your python library.
  • Make sure to check the spelling name before installing the module.
  • The system cannot find the python path.
  • Assured that the config module is installed correctly in the virtual environment.
  • The config module is installed but it is installed at the incorrect path environment path.

How to solve the no module named config?

To solve the no module named config, here are the solutions we will provide to solve the error no module name config.

    Solution 1: Install config module in Python 2

This is the command to install the config module in Python 2:

pip install config

After you type the command above, it will show the following output below:

pip installed in Modulenotfounderror no module named

Solution 2: Install config module in Python 3

The following command below is for the installation in config module for Python 3:

pip3 install config

Conclusion

In conclusion, we have given the best way of solution to solve the error Modulenotfounderror: no module named ‘config’. If you encountered the no module named config the above solutions will solve it.

I hope this tutorial was helpful to you to solve your problem with the error. Contact us for extra assistance if you continue to experience this error.

Leave a Comment Cancel reply

You must be logged in to post a comment.

Источник

Modulenotfounderror no module named ‘config’ ( Solved )

Importerror cannot import name qtwidgets

ModuleNotFoundError is an error you get when the python interpreter is unable to find the module in your system. If you are getting the error no module named config then in this post you will know all the solutions that can solve the error modulenotfounderror no module named ‘config’ error.

Why modulenotfounderror no module named ‘config’ error occurs?

The reasons for getting this error can be many. But the following are the main reason that gives you an error,

1. The config module is not installed in your system.

The first reason that leads to this error is that your system has not found the config module. You will get the modulenotfounderror when you try to import the config module.

Modulenotfounderror no module named 'config'

To solve this issue you have to install the config module in your system.

For python 3. xx

For python 2. xx

2. Typo error

The second reason you may get the error is that there must be a spelling error in the module. Most developers write conf instead of config and it leads to modulenotfounderror.

To solve this error you must ensure that you are correctly using the word config. Don’t import conf instead import config.

3. The system is unable to find the python path

The third reason for getting this Modulenotfounderror no module named ‘config’ error is that the python path is not properly set. And due to this config module is not set in your path environment variables.

Open your terminal or command prompt and type the following command to set the path of the environment variable PYTHONPATH.

config module pypi website

Conclusion

The config module is used to store configuration information for applications. Most of the developer name the config file config.py. This makes it very easy to import settings when you are building a large application. If you are getting the Modulenotfounderror no module named ‘config’ error then the above methods will solve it.

I hope you have liked this tutorial. If you are still getting this error then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Источник

Читайте также:  Java convert binary string to string
Оцените статью