Relative imports require the 'package' argument

Question:

I want to use Sphinx so it can automatically generate a pydoc for my python code but I’m getting an error. What an I doing wrong?

conf.py sphinx config file

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 file

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 error thrown when I’m trying to make the html file.

    C:UsersogwardSTUDPROJdocscode.rst:3: WARNING: autodoc: failed to import module u'views'; the following exception wa
s raised:
Traceback (most recent call last):
  File "C:Python27libsite-packagessphinx-1.2.2-py2.7.eggsphinxextautodoc.py", line 335, in import_object
    __import__(self.modname)
  File "C:UsersogwardSTUDPROJcloud_servercloud_apiviews.py", line 1, in <module>
    from django.contrib.auth.models import User, Group
  File "C:Python27libsite-packagesdjangocontribauth__init__.py", line 6, in <module>
    from django.middleware.csrf import rotate_token
  File "C:Python27libsite-packagesdjangomiddlewarecsrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "C:Python27libsite-packagesdjangoutilscache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "C:Python27libsite-packagesdjangocorecache__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "C:Python27libsite-packagesdjangoconf__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:Python27libsite-packagesdjangoconf__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "C:Python27libsite-packagesdjangoconf__init__.py", line 128, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:Python27libsite-packagesdjangoutilsimportlib.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.
Asked By: ogward

||

Answers:

DJANGO_SETTINGS_MODULE is expected to be a Python module identifier, not a filesystem path. Looking at the django/conf/__init__py file, it seems that a relative path to your settings module won’t work there. You will need to move it below a directory listed in your sys.path, or you should add a parent directory to your sys.path and reference your settings module from there.

Answered By: lanzz
  1. may be the settings you set in uwsgi.py is not correct
  2. the settings path in uwsgi.py(XXXX is in the same dir as uwsgi.py):

    os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “XXXX.settings”)

Answered By: jammyWolf

I came to this question via Google, so I’ll answer what helped me (not directly related to the question).

I use importlib to dynamically import sub-packages given by a string.

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

This simply has to be adjusted to

import importlib
module_name = 'subpackage.i.import'
special_module = importlib.import_module(module_name, package='my_current_pkg')
Answered By: Martin Thoma

You can also get this error simply if you have a typo in where you’re specifying your settings file name.

Answered By: Jan Kyu Peblik

you need to add your project path to sys path just like

sys.path.append("C:\Users\ogward\STUDPROJ") 
os.environ['DJANGO_SETTINGS_MODULE'] = '../cloud_server.settings'
 
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.