how to import local module to upgrade module?

690 views
Skip to first unread message

Michal Nowikowski

unread,
Nov 14, 2013, 4:24:40 AM11/14/13
to sqlalchem...@googlegroups.com
Hello,

I observed that I repeat myself in upgrade modules.
So I concluded that it would be good to put that code into separate python module and then just import it in upgrade modules.
I put my code in alembic/common.py just beside env.py.
Unfortunately it is not so easy.
Current working directory is 2 levels up regarding alembic/versions and to import anything from alembic/common.py
I have to tweak sys,path and I cannot just add "../.." due to compelxity of my project.
I wanted to do it using:
   from alembic import context
   sys.path.append(context.config.get_main_option("script_location"))
   from common import add_ref, drop_ref
But it does not work:
$ alembic history
Traceback (most recent call last):
  File "/usr/bin/alembic", line 9, in <module>
    load_entry_point('alembic==0.4.1', 'console_scripts', 'alembic')()
  File "/usr/lib/python2.7/dist-packages/alembic/config.py", line 255, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/usr/lib/python2.7/dist-packages/alembic/config.py", line 250, in main
    self.run_cmd(cfg, options)
  File "/usr/lib/python2.7/dist-packages/alembic/config.py", line 241, in run_cmd
    **dict((k, getattr(options, k)) for k in kwarg)
  File "/usr/lib/python2.7/dist-packages/alembic/command.py", line 156, in history
    for sc in script.walk_revisions():
  File "/usr/lib/python2.7/dist-packages/alembic/script.py", line 72, in walk_revisions
    heads = set(self.get_heads())
  File "/usr/lib/python2.7/dist-packages/alembic/script.py", line 269, in get_heads
    for script in self._revision_map.values():
  File "/usr/lib/python2.7/dist-packages/alembic/util.py", line 220, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/usr/lib/python2.7/dist-packages/alembic/script.py", line 201, in _revision_map
    script = Script._from_filename(self.versions, file_)
  File "/usr/lib/python2.7/dist-packages/alembic/script.py", line 422, in _from_filename
    module = util.load_python_file(dir_, filename)
  File "/usr/lib/python2.7/dist-packages/alembic/util.py", line 186, in load_python_file
    module = imp.load_source(module_id, path, open(path, 'rb'))
  File "alembic/versions/31802014a5db_create_product_recor.py", line 18, in <module>
    sys.path.append(context.config.get_main_option("script_location"))
AttributeError: 'module' object has no attribute 'config'

Do you now how to handle this?

Regards,
Godfryd

Maxime Lévesque

unread,
Apr 21, 2014, 3:36:47 PM4/21/14
to sqlalchem...@googlegroups.com

Having the same need, I'd like to know if there is a way to do this.

Thanks

Michael Bayer

unread,
Apr 21, 2014, 3:48:14 PM4/21/14
to sqlalchem...@googlegroups.com
put dependency models into your main application, not the alembic directory.  the “env.py” file is loaded through a special per-file loader, so it isn’t really part of a Python “package” per se.  the other things that env.py needs, have them import from your normal application, e.g. “from myapp.migrations import x, y, z”.




-- 
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages