包setup.py和Flask目录结构

时间:2012-08-29 12:53:30

标签: python flask setuptools flask-sqlalchemy

更新:添加了setup.py内容。

我有以下目录结构:

setup.py
packagename/
    __init__.py
    useless_file.py

__init__.py中,我有以下内容:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()

CONFIG_FILENAME = "packagename.settings"


def create_app():
    app = Flask(__name__)
    app.config.from_object(CONFIG_FILENAME)

    db.init_app(app)

    app.register_blueprint(sms_testing)

    return app

我关注了Flask的recommended package structure

以下是setup.py

import os
from setuptools import setup

requires = (
        "flask",
        "flask-sqlalchemy",
        "requests>=0.13.6",
        "python-dateutil>=1.5",
        "twilio",
        "selenium",
        )

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name = "Package Name",
    version = "0.0.1",
    author = "Charles-Axel Dein",
    author_email = "ca@d3in.org",
    description = ("useless"),
    license = "BSD",
    keywords = "example documentation tutorial",
    url = "http://packages.python.org/an_example_pypi_project",
    packages=['package_name',],
    # namespace_packages = ['package_name'],
    install_requires=requires,
    long_description=read('README.md'),
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
)

当我python setup.py develop时,我得到:

Traceback (most recent call last):
  File "setup.py", line 2, in <module>
    from setuptools import setup
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/__init__.py", line 2, in <module>
    # The Python Imaging Library.
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/extension.py", line 2, in <module>
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/dist.py", line 6, in <module>
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/sdist.py", line 5, in <module>
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2623, in <module>
    if not len(req.specs):
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 679, in subscribe
    `requirements` must be a string or a (possibly-nested) sequence
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2623, in <lambda>
    if not len(req.specs):
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2172, in activate
    return pv
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 1790, in declare_namespace
    module.__path__ = []; _set_parent_ns(packageName)
  File "/.virtualenvs/api/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 1761, in _handle_ns

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 238, in load_module
    mod = imp.load_module(fullname, self.file, self.filename, self.etc)
  File "/package_name/__init__.py", line 2, in <module>
    import flask
  File "/.virtualenvs/api/lib/python2.7/site-packages/flask/exthook.py", line 86, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named flask

(我编辑了文件夹)

我认为这是合乎逻辑的,因为setuptools正在尝试导入我的包,导入packagename/__init__.py,其中有import flask才有机会安装它。

问题是,我需要import flask中的__init__.py来创建数据库。

你会推荐什么?

谢谢!

CHX

1 个答案:

答案 0 :(得分:1)

我个人不喜欢python setup.py develop,你试过这个吗?

% pip install -e .

当前目录是其中包含setup.py的目录。它与develop类似,但会安装所有必需的包。