【问题标题】:python package initializing a db to nowherepython包将数据库初始化为无处
【发布时间】:2018-12-31 05:38:37
【问题描述】:

我正在使用这些说明来创建包:

https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html

我在包中有这样的设置:

mypackage/bin
mypackage/bin/initialize_sqlite.sh
mypackage/mypackage/__init__.py
mypackage/mypackage/a_bunch_of_python.py
mypackage/mypackage/tests/__init__.py
mypackage/mypackage/tests/test_setup.py
mypackage/mypackage/maketables.sql
mypackage/setup.py

initialize_sqlite.sh 在哪里

#!/usr/bin/env bash

sqlite3 my.db < ../mypackage/maketables.sql # creates a Users table

setup.py 是

from setuptools import setup

setup(name='mypackage,
      packages=['mypackage'],
      scripts=['bin/initialize_sqlite.sh'],
      install_requires=[
          'pysqlite3'
      ],
      test_suite='nose.collector',
      tests_require=['nose'],      
      zip_safe=False)

而 utils.py 是

from pathlib import Path
import mypackage

def get_project_root():
    """Returns project root folder.
    """
    return Path(mypackage.__file__).parent.absolute()

ROOT = get_project_root()
DBFILE = str(ROOT.joinpath('my.db'))

而我的 mypackage/mypackage/tests/test_setup.py 是:

from unittest import TestCase

import mypackage
from mypackage import utils
from pysqlite3 import dbapi2 as sqlite3 

class TestSqlAccess(TestCase):
    def test_sql_access(self):
        conn = sqlite3.connect(utils.DBFILE)
        db = conn.cursor()
        s = db.execute("PRAGMA database_list").fetchall()
        self.assertNotEqual(s, [])
        s2 = db.execute("SELECT 1 from Users LIMIT 1").fetchall()

当我在我的包目录中运行时:

./bin/initialize_sqlite.sh

它很好地创造了

mypackage/my.db

正如我所料。

但是,当我运行安装时:

pip install ./mypackage

当我看到时:

ll /home/user/anaconda3/envs/myenv/lib/python3.6/site-packages/mypackage/my.db

它是空的,大小为 0。

事实上,我的测试失败了:

ERROR: test_sql_access (mypackage.tests.test_setup.TestSqlAccess)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/.../tests/test_setup.py", line 14, in test_sql_access
    s2 = db.execute("SELECT 1 from Users LIMIT 1").fetchall()
pysqlite3.dbapi2.OperationalError: no such table: Users

我的安装箱或路径有什么问题?是不是找不到 maketables.sql 而不是默默地不告诉我(所以它 sqlite3 my.db

它是否在运行 bin 之前/之前/运行测试?

【问题讨论】:

  • Addedum---如何让 bin/initialize_sqlite.sh 在安装时将其 echo 语句打印到标准输出?如果我能做到这一点,我可以调试它认为它所在的目录。

标签: python setup.py python-packaging


【解决方案1】:

您检查文件模式和所有者了吗?

ls -la

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 2023-04-08
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多