【问题标题】:Management command not found without unzipping .egg未解压缩 .egg 未找到管理命令
【发布时间】:2013-06-20 00:22:38
【问题描述】:

我有一个 django 应用程序,我根据此处的文档打包:https://docs.djangoproject.com/en/1.5/intro/reusable-apps/

我使用 setup.py 将应用安装到虚拟环境中。

./setup.py install

应用程序的 Web UI 在虚拟环境中运行良好。但是我无法通过这个 vanilla install 访问自定义管理命令。

(django_grm)[grm@controller django_grm]$ python ./manage.py sync_to_graphite
Unknown command: 'sync_to_graphite'

命令不执行时的虚拟环境如下所示:

(django_grm)[grm@controller django_grm]$ ll /home/grm/venv/django_grm/lib/python2.7/site-packages
total 1148
...
-rw-rw-r--  1 grm grm 243962 Jun 19 17:11 django_grm-0.0.4-py2.7.egg
...

但是,一旦我解压了 .egg 文件,管理命令就会按预期工作。

(django_grm)[grm@controller django_grm]$ cd /home/grm/venv/django_grm/lib/python2.7/site-packages
(django_grm)[grm@controller site-packages]$ unzip django_grm-0.0.4-py2.7.egg 

(django_grm)[grm@controller site-packages]$ ll /home/grm/venv/django_grm/lib/python2.7/site-packages
total 1152
...
-rw-rw-r--  1 grm grm 243962 Jun 19 17:11 django_grm-0.0.4-py2.7.egg
drwxrwxr-x  6 grm grm   4096 Jun 19 17:16 dj_grm
...

(django_grm)[grm@controller site-packages]$ cd /home/grm/django_projects/django_grm/
(django_grm)[grm@controller django_grm]$ python ./manage.py sync_to_graphite

<success>

这是正常行为吗?感觉很奇怪。

【问题讨论】:

    标签: django


    【解决方案1】:

    我强烈建议使用pip 而不是setup.py。它在安装和管理软件包方面往往做得更好。

    一旦您的虚拟环境就位,它将是:

    $ . env/bin/activate
    $ pip install [APP_NAME]
    

    这会在虚拟环境中安装应用的非压缩版本。

    如果应用是来自某个地方的 zip,您仍然可以使用 pip

    $ pip install http://[URL_TO_ZIP]
    

    【讨论】:

    • 使用 pip 让一切变得不同。谢谢!
    【解决方案2】:

    我们来看看the part of the source that loads management commands

    def find_commands(management_dir):
        """
        Given a path to a management directory, returns a list of all the command
        names that are available.
    
        Returns an empty list if no commands are defined.
        """
        command_dir = os.path.join(management_dir, 'commands')
        try:
            return [f[:-3] for f in os.listdir(command_dir)
                    if not f.startswith('_') and f.endswith('.py')]
        except OSError:
            return []
    

    is called by:

    # Find and load the management module for each installed app.
    for app_name in apps:
        try:
            path = find_management_module(app_name)
            _commands.update(dict([(name, app_name)
                                   for name in find_commands(path)]))
        except ImportError:
            pass # No management module - ignore this app
    

    所以,是的,Django 不支持安装在压缩文件中的应用程序,至少在这里是这样;它需要在management_dir 内有一个明确的commands 目录。


    正如@tghw 所说,通过pip 安装会将包保存在一个目录中,而不是压缩它。你也可以(并且可能应该也)在你的setup() 命令中设置zip_safe=False;这将阻止 setuptools/distribute/etc 尝试压缩您的包,无论您如何安装它。

    【讨论】:

    • @TravisBear 哦,我刚刚想起zip_safe=Falsesetup() 的参数,这是解决这个问题的正确方法™。然后没有安装方法会尝试压缩你的包。
    猜你喜欢
    • 2014-01-22
    • 2016-12-14
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2020-07-16
    • 2017-05-05
    相关资源
    最近更新 更多