【问题标题】:Relative imports require the 'package' argument相对导入需要 'package' 参数
【发布时间】:2014-03-04 13:11:31
【问题描述】:

我想使用 Sphinx,以便它可以为我的 python 代码自动生成一个 pydoc,但我遇到了一个错误。我做错了什么?

conf.py狮身人面像配置文件

import sys
import os
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = '../cloud_server.settings'

sys.path.insert(0, os.path.abspath('../cloud_server/cloud_api'))

views.py django 文件

from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from cloud_api.serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

Typeerror 在我尝试制作 html 文件时抛出错误。

    C:\Users\ogward\STUDPROJ\docs\code.rst:3: WARNING: autodoc: failed to import module u'views'; the following exception wa
s raised:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\sphinx-1.2.2-py2.7.egg\sphinx\ext\autodoc.py", line 335, in import_object
    __import__(self.modname)
  File "C:\Users\ogward\STUDPROJ\cloud_server\cloud_api\views.py", line 1, in <module>
    from django.contrib.auth.models import User, Group
  File "C:\Python27\lib\site-packages\django\contrib\auth\__init__.py", line 6, in <module>
    from django.middleware.csrf import rotate_token
  File "C:\Python27\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "C:\Python27\lib\site-packages\django\utils\cache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "C:\Python27\lib\site-packages\django\core\cache\__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 128, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 33, in import_module
    raise TypeError("relative imports require the 'package' argument")
TypeError: relative imports require the 'package' argument
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [ 50%] code
writing output... [100%] index

writing additional files... genindex search
copying static files... done
copying extra files... done
dumping search index... done
dumping object inventory... done
build succeeded, 1 warning.

【问题讨论】:

    标签: python django python-sphinx pydoc


    【解决方案1】:

    我是通过 Google 提出这个问题的,所以我会回答对我有帮助的内容(与问题没有直接关系)。

    我使用importlib 来动态导入字符串给定的子包。

    import importlib
    module_name = 'subpackage.i.import'
    special_module = importlib.import_module(module_name, package=None)
    

    这只是必须调整为

    import importlib
    module_name = 'subpackage.i.import'
    special_module = importlib.import_module(module_name, package='my_current_pkg')
    

    【讨论】:

      【解决方案2】:

      DJANGO_SETTINGS_MODULE 应该是 Python module identifier,而不是文件系统路径。查看django/conf/__init__py 文件,您的设置模块的相对路径似乎在那里不起作用。您需要将其移动到您的sys.path 中列出的目录下方,或者您应该将父目录添加到您的sys.path 并从那里引用您的设置模块。

      【讨论】:

        【解决方案3】:

        如果您在指定设置文件名时出现拼写错误,也可能会出现此错误。

        【讨论】:

          【解决方案4】:

          您需要将项目路径添加到系统路径,就像

          sys.path.append("C:\\Users\\ogward\\STUDPROJ") 
          os.environ['DJANGO_SETTINGS_MODULE'] = '../cloud_server.settings'
           
          

          【讨论】:

          • 这可能仅适用于本地计算机,但当您尝试在远程服务器上部署时会出现问题。
          【解决方案5】:
          1. 可能是你在uwsgi.py中设置的设置不正确
          2. uwsgi.py中的设置路径(XXXX和uwsgi.py在同一个目录):

            os.environ.setdefault("DJANGO_SETTINGS_MODULE", "XXXX.settings")

          【讨论】:

            猜你喜欢
            • 2017-04-16
            • 1970-01-01
            • 1970-01-01
            • 2021-11-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-23
            • 1970-01-01
            相关资源
            最近更新 更多