0

Below is my config.py file

ABC_TEST01_JJ = {"username" : 'NONE',"password" : 'NINU', "dsn" : 'ABC_TEST01_JJ', "port" : 1512, "encoding" : 'UTF-8'}

and this my python test.py script

import cx_Oracle
import config



connection = None
try:
    env = input("Enter Environment name : ")
    connection = cx_Oracle.connect(
        config.format(env)["username"],
        config.format(env)["password"],
        config.format(env)["dsn"],
        encoding=config.format(env)["encoding"])

    # show the version of the Oracle Database
    print(connection.version)
except cx_Oracle.Error as error:
    print(error)
finally:
    # release the connection
    if connection:
        connection.close()

I want to provide the env name as a prompt value and that value will go to config.env["username"] and it will pull all values from the config file but the issue is it is not taking the value of env name in config.format(env)["username"]. Not sure how to pass the value of variable env to there?

unknown
  • 1,815
  • 3
  • 26
  • 51
  • what Python version do you have ? and which version of config ? I am trying to replicate your program but my config has no module format . – Roberto Hernandez Aug 01 '20 at 09:35
  • I m using Python 3.8 – unknown Aug 01 '20 at 11:29
  • did you install config as a pip module ? `pip install config` I have the same version as you, I installed config but I can't run the module format. It says it does not exist. I wonder, why did you load the environment variables that way ? I mean, did you think in using configparser or yaml ? or even jxon ? – Roberto Hernandez Aug 01 '20 at 13:13
  • @RobertoHernandez no I didn't use pip to install config and I didn't use the configparser or yaml yet – unknown Aug 01 '20 at 13:43
  • I could help you with configparser or json or yaml, but I can't make the config library work. I don't even know how you could install it even, if not with pip. – Roberto Hernandez Aug 01 '20 at 14:46
  • @RobertoHernandez can you suggest me how to use configparser where you have multiple db for dev environment like ABC_DEV01_PP,ABC_DEV02_PP,ABC_DEV03_PP,DEF_DEV01_PP,DEF_DEV02_PP and sometime you need to call sepcific one and sometime you want to do the operation on all DEV dbs – unknown Aug 01 '20 at 14:53

1 Answers1

0

Import isn't for text files, see importing external ".txt" file in python

You could try something like:

conf.ini:

[Dev Site]
username = NONE
password = NINU
dsn = ABC_TEST01_JJ
port = 1512
encoding = UTF-8

[CJ]
username = cj
password = cj
dsn = localhost/orclpdb1
port = 1521
encoding = UTF-8

and use it like this in Python 3 (the module names are different in Python 2):

import cx_Oracle
from configparser import ConfigParser

parser = ConfigParser()
parser.read('conf.ini')

try:
    env = input("Enter Environment name : ") or 'Dev Site'
    print(env)

    username = parser.get(env, 'username')
    password = parser.get(env, 'password')
    dsn = parser.get(env, 'dsn')
    encoding = parser.get(env, 'encoding')

    connection = cx_Oracle.connect(username, password, dsn, encoding=encoding)

    # show the version of the Oracle Database
    print(connection.version)
except cx_Oracle.Error as error:
    print(error)
finally:
    # release the connection
    if connection:
        connection.close()

Output is like:

$ python test.py 
Enter Environment name : CJ
CJ
19.3.0.0.0

If you want to access environment variables instead of using a config file, you can do something like:

import os
connection = cx_Oracle.Connection(os.environ.get("PYTHON_USERNAME"),
      os.environ.get("PYTHON_PASSWORD"), os.environ.get("PYTHON_CONNECTSTRING"))
Christopher Jones
  • 9,449
  • 3
  • 24
  • 48