0

I have a third-party module (cx_Oracle) that I'd like to import whose location is unknown from environment to environment. I am currently using pythons configparser so I thought it would be a neat trick to set the location of the module within the config parser, append that location to path, and then import the third-party module from there.

This worked all fine and dandy until I began to refactor my code and started to split out logic into their own class/methods:

class Database:

    def __init__(self, config):
        self.CONFIG=config
        sys.path.append(self.CONFIG.cx_oracle_path)
        from cx_Oracle import cx_Oracle

        self.open()

    def open(self):
        self.CONNECTION = cx_Oracle.Connection(self.CONFIG.username,
        self.CONFIG.password,
        self.CONFIG.db_sid)
        self.CURSOR = self.CONNECTION.cursor()
     ....
     ....
     ....

Of course, the open method does not know what to do because cx_Oracle was defined in init and so the open method cannot see it.

I can't picture the proper way to do this, so I'm assuming I am over thinking this. What should I do instead so that open (and all other methods within the Database class) can see the imported module?

Thank you.

Bach
  • 6,145
  • 7
  • 36
  • 61
Patrick Bateman
  • 271
  • 2
  • 5
  • 14
  • It looks like you confuse importing a module, opening a file and/or connecting to a database. It is generally very bad design not having the module to import at known location, you shall have it under control. It is not clear to me, what you expect from `self.open() – Jan Vlcinsky Apr 29 '14 at 19:48
  • Your `import` is only going to have scope within `__init__` if I'm not mistaken. I think you are looking for `__import__()`. – 2rs2ts Apr 29 '14 at 19:52
  • @JanVlcinsky I'm importing the cx_Oracle module so that I can communicate with an oracle database. Then, I open the connection to that database within the self.open() method. I understand that modules should generally be in known locations, and maybe I should speak to my IT team with getting the module installed at the correct location. Generally, there is less friction among our various teams to just have "everything you need" in a single folder. The problem I am having is calling the program from a different location, and maybe that is where I over working myself. – Patrick Bateman Apr 29 '14 at 19:53
  • @PatrickBateman I was probably too quick in my comment. With Oracle it might be sometime difficult. May be this SO answer helps you: http://stackoverflow.com/a/4784441/346478 – Jan Vlcinsky Apr 29 '14 at 19:58

1 Answers1

1

If you only need to use cx_Oracle within that class, you can just set it as an attribute on this instance, for example:

class Database:

    def __init__(self, config):
        self.CONFIG=config
        sys.path.append(self.CONFIG.cx_oracle_path)
        from cx_Oracle import cx_Oracle
        self.cx_Oracle = cx_Oracle
        self.open()

    def open(self):
        self.CONNECTION = self.cx_Oracle.Connection(self.CONFIG.username,
                                                    self.CONFIG.password,
                                                    self.CONFIG.db_sid)
        self.CURSOR = self.CONNECTION.cursor()

As a side note, if you are creating multiple Database instances this is an odd approach, since you would end up adding multiple identical entries to sys.path.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • The database instance would be called once. Inside the class there is an insert statement that I would call anytime I need it. This is a very simple program that is just logging some activity. Your answer is what I was looking for; thank you. – Patrick Bateman Apr 29 '14 at 19:57