Accessing a MySQL database with python

On 18/01/2015 at 10:35, xxxxxxxx wrote:

Is it possible?
Are there any libraries available?
My MySQL database is online.

On 19/01/2015 at 01:16, xxxxxxxx wrote:

Have you tried this? : https://pypi.python.org/pypi/MySQL-python/1.2.5
Haven't tried it but is supposed to be ok.

-b

On 19/01/2015 at 01:36, xxxxxxxx wrote:

I found that but to include that in my plugins I would have to force users to install that, isn't it?

On 23/01/2015 at 17:44, xxxxxxxx wrote:

Hi Rui,

Check out Niklas' C4D Tools

There he mentions this bit of code:

# Store the old module configuration.   
old_modules = sys.modules.copy()   
  
# Import your stuff, for example:   
lib_path = os.path.join(os.path.dirname(__file__), 'lib')   
sys.path.insert(0, lib_path)   
try:   
    import c4dtools   
finally:   
    sys.path.pop(0)   
  
# Restore the previous module configuration making sure to not   
# remove modules not loaded from the local libraries folder.   
for k, v in sys.modules.items() :   
    if k not in old_modules and hasattr(v, '__file__') or not v:   
        if not v or v.__file__.startswith(lib_path) :   
            sys.modules.pop(k)   
    else:   
        sys.modules[k] = v   
  
res, _ = c4dtools.prepare(__file__, __res__)

You should be able to put the MySQL library in a lib folder next to your source file, and import it using something like the above code.

On 23/01/2015 at 17:55, xxxxxxxx wrote:

Hey,

C4dtools is old, I should update it some time. 🙂 Today, I prefer to paste this snippet into my plugin:

>
> import os, sys, glob
>
>
> class localimport(object) :
>
>
> _modulecache = []
>
>
> _eggs = staticmethod(lambda x: glob.glob(os.path.join(x, '*.egg')))
>
>
> def __init__(self, libpath, autoeggs=False, isolate=False, path=os.path) :
>
>
> if not path.isabs(libpath) :
>
>
> libpath = path.join(path.dirname(path.abspath(__file__)), libpath)
>
>
> self.libpath = libpath; self.autoeggs = autoeggs; self.isolate = isolate
>
>
> def __enter__(self) :
>
>
> self._path, self._mpath = list(sys.path), list(sys.meta_path)
>
>
> self._mods = frozenset(sys.modules.keys())
>
>
> sys.path.append(self.libpath)
>
>
> sys.path.extend(self._eggs(self.libpath) if self.autoeggs else [])
>
>
> def __exit__(self, *args) :
>
>
> sys.path[:] = self._path; sys.meta_path[:] = self._mpath
>
>
> for key in sys.modules.keys() :
>
>
> if key not in self._mods and self._islocal(sys.modules[key]) :
>
>
> localimport._modulecache.append(sys.modules.pop(key))
>
>
> def _islocal(self, mod) :
>
>
> if self.isolate: return True
>
>
> filename = getattr(mod, '__file__', None)
>
>
> if filename:
>
>
> try: s = os.path.relpath(filename, self.libpath)
>
>
> except ValueError: return False
>
>
> else: return s == os.curdir or not s.startswith(os.pardir)
>
>
> else: return False

And then use it like this:

>
> with localimport('relative/path/to/lib/folder') :
>
>
> import some_package

You can always find the newest version in this gist.

On 24/01/2015 at 03:36, xxxxxxxx wrote:

So, with this code I could import the mysql library that would reside in a folder inside my plugin folder?

On 24/01/2015 at 07:38, xxxxxxxx wrote:

Yes, if you put the mysql lib in a libs folder at the same level as your plugin's code you should be able to import the MySql library using the above code.