如何将自己的模型导入myproject / alembic / env.py?

时间:2015-08-16 07:44:46

标签: python-3.x python-import alembic

我想将alembic revision --autogenerate与我自己的模型类一起使用。因此,我需要将myproject/alembic/env.py导入described in the docs。但即使我尝试了很多变化,这也行不通。

我不确定在哪种情况下(不知道这是否是正确的单词)alembic是否运行env.py。也许这会导致一些错误。

这是我使用的目录和文件结构。

myproject/
    common/
        __init__.py
        model.py
    alembic/
        env.py

错误就是那种

    from .common import model
SystemError: Parent module '' not loaded, cannot perform relative import

myproject本身只是一个存储库/工作目录。它不是已安装进入系统(使用pip3apt-geteasyinstall或其他任何内容。

4 个答案:

答案 0 :(得分:12)

您可以设置PYTHONPATH环境变量来控制python看到的顶级文件夹,例如。如果您在项目的根文件夹中:

Warning: node.js detection failed, sbt will use the Rhino based Trireme        JavaScript engine instead to run JavaScript assets  compilation,    which in some cases may be orders of magnitude slower than using node.js.
            [error] n.s.e.Cache - Unable to set localhost. This prevents      creation of a GUID. Cause was: linux-78et.suse: linux-  78et.suse: unknown error
          java.net.UnknownHostException: linux-78et.suse: linux-78et.suse: unknown error
    at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
    at net.sf.ehcache.Cache.<clinit>(Cache.java:214)
    at     net.sf.ehcache.config.ConfigurationHelper.createCache(ConfigurationHelper.java:296)
    at    net.sf.ehcache.config.ConfigurationHelper.createDefaultCache(ConfigurationHelper.java:219)
       at net.sf.ehcache.CacheManager.configure(CacheManager.java:722)
       at net.sf.ehcache.CacheManager.doInit(CacheManager.java:439)
      at net.sf.ehcache.CacheManager.init(CacheManager.java:377)
      at net.sf.ehcache.CacheManager.<init>(CacheManager.java:259)
       at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:1037)
      at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:936)
    Caused by: java.net.UnknownHostException: linux-78et.suse: unknown error
      at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
      at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
      at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
      at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
      at net.sf.ehcache.Cache.<clinit>(Cache.java:214)
      at net.sf.ehcache.config.ConfigurationHelper.createCache(ConfigurationHelper.java:296)
      at   net.sf.ehcache.config.ConfigurationHelper.createDefaultCache(ConfigurationHelper.java:219)
      at net.sf.ehcache.CacheManager.configure(CacheManager.java:722)
      at net.sf.ehcache.CacheManager.doInit(CacheManager.java:439)
      at net.sf.ehcache.CacheManager.init(CacheManager.java:377)
      [info] application - ApplicationTimer demo: Starting application at 2016-05-22T19:21:48.074Z.

然后你可以使用&#34;普通&#34;在示例中,相对于根文件夹,在您的alembic env.py中导入:

PYTHONPATH=. alembic revision -m "..."

答案 1 :(得分:8)

在同样的问题上摆弄了几个小时,我发现了一个解决方案。首先,这是我现在的结构:

. ← That's the root directory of my project
├── alembic.ini
├── dev-requirements.txt
├── requirements.txt
├── runtime.txt
├── setup.cfg
├── src
│   └── models
│       ├── base.py
│       ...
│       └── migrations
│           ├── env.py
│           ├── README
│           ├── script.py.mako
│           └── versions
│          
└── tests

env.py我只是这样做了:

import sys
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(dirname(abspath(__file__))))) # Insert <.>/src
import models # now it can be imported
target_metadata = models.base.Base.metadata

希望你觉得这很有用! :)

编辑:然后我做了我的第一次修订,数据库为空(没有表格),alembic自动填充upgrade()downgrade()的所有内容。我这样做是因为并非我的所有表都是由alembic检测到的自动

答案 2 :(得分:1)

将此内容放入您的env.py中,将工作目录放入Python路径:

import sys
import os

sys.path.insert(0, os.getcwd())

答案 3 :(得分:0)

对于 alembic 1.5.5 及更高版本,将以下内容添加到您的 alembic.ini

prepend_sys_path = .

来自 alembic documentation:如果存在,这将被添加到 sys.path,默认为当前工作目录。