0

I am calling a python file from a Visual C# form. Click of a button in the C# will call a Python script. The python script will use the config file to connect to the database and provide the results.

If I call the Python program on it's own, it works perfectly. When I call from a Visual C#, the problem occurs. My Python code is like this:

def db_Connection():
    logger.info(':Entering db_Connection().....')
    logger.info(':Config file read.....')
    config = ConfigParser.RawConfigParser()
    config.read('config.cfg')
    logger.info(':Config file read complete.....')
    USER = config.get('DB_Connector','db.user_name' )
    logger.info(':USER acquired.....')
    PASSWORD = config.get('DB_Connector','db.password' )
    SID = config.get('DB_Connector','db.SID' )
    IP = config.get('DB_Connector','db.IP')
    PORT = config.get('DB_Connector','db.Port')
    engine = create_engine('oracle://{user}:{pwd}@{ip}:{port}/{sid}'.format(user=USER, pwd=PASSWORD, ip=IP, port=PORT, sid=SID), echo=False)
    connection = engine.connect()
    logger.info(':Connected to DB.....')
    p = engine.execute("SELECT * from VAM.ASSET where ASSET_ID = '{}'".format(asset_id))

    logger.info(':Calculating for any records.....')
    if len(p.fetchall())!=0:
        print 'Asset Exists'

Looking at the logs, the last line is

2015-04-28 15:40:47,361 INFO :Config file read complete.....

How can I solve this here?

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
Roshan r
  • 522
  • 2
  • 11
  • 30

1 Answers1

1

Depending on how you launch you C# program and how it than launches the Python script, the current directory may not be what you expect.

In that use case, you should use the full path instead of a relative one for config.cfg :

...
config.read('/full/path/to/config.cfg')
...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • It worked. Python will write the value into a text file. C# will take the value from the text file after the Python file is processed. Now i can see C# is taking the old value and then the Python is processed. Every time i run the program, it takes the previous value. I added a timer also in C# but still it didn't work. Is there any way to return the value from Python to C#? – Roshan r Apr 29 '15 at 09:02
  • @Roshanr : There are ways to return a value from a child program to its parent, an an external text file is one among others and it should work. As I assume you are refering to [this other question](http://stackoverflow.com/q/29700094/3545273), I'll have a look at it. But could you accept this answer if it helped to notify others that you no longer need help *for that part* ? – Serge Ballesta Apr 29 '15 at 09:44