一直得到ImportError:无法导入设置'myapp.settings'错误

时间:2021-12-06 00:20:32

There appears to be a number of potential solutions to this problem but nothing seems to work for me.

似乎有许多潜在的解决方案可以解决这个问题,但似乎没有什么对我有用。

Running python manage.py runserver is fine but I get the error when trying to run django-admin.py - with any option. I am actually trying to do a django-admin.py dumpdata myapp.

运行python manage.py runserver很好,但是在尝试运行django-admin.py时遇到错误 - 有任何选项。我实际上是在尝试做一个django-admin.py dumpdata myapp。

Traceback (most recent call last):
File "/Users/lemon/.virtualenvs/ram/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'ram.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named ram.settings

Structure of my project directory is:

我的项目目录的结构是:

README.md
├── TODO
├── fabfile.py
├── manage.py
├── ram
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── ram.sqlite
│   ├── ram.sqlite.orig
│   ├── settings.py
│   ├── settings.py.orig
│   ├── settings.pyc
│   ├── templates
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── ramapp 
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   └── ram
│   │       ├── contributors.html
│   │       ├── index.html
│   │       ├── ram_sheet.html
│   │       └── scenario_add.html
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.py.orig
│   └── views.pyc
├── requirements.txt
└── templates
    └── admin
        └── base_site.html

I'm using virtualenvrapper which I have reinstalled and have no issue with.

我正在使用virtualenvrapper,我重新安装并且没有问题。

manage.py looks like this:

manage.py看起来像这样:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ram.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Not quite sure where else to go with this - any help massively appreciated. Thanks.

不太确定在哪里可以使用它 - 任何帮助都非常感激。谢谢。

Having tried to amend the PYTHONPATH as suggested, now get similar error:

尝试按照建议修改PYTHONPATH,现在得到类似的错误:

Traceback (most recent call last):
File "/Users/lemon/.virtualenvs/ram/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/__init__.py", line 75, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/management/commands/dumpdata.py", line 3, in <module>
from django.core import serializers
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 22, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/serializers/base.py", line 5, in <module>
from django.db import models
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/db/__init__.py", line 83, in <module>
signals.request_started.connect(reset_queries)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 88, in connect
if settings.DEBUG:
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

1 个解决方案

#1


9  

Try:

尝试:

export PYTHONPATH=/path/to/folder/with/manage.py:$PYTHONPATH

It seems like it is a path problem. the path from where you call manage.py is usually added to PYTHONPATH, if you call django-admin.py it is not. Your application therefore does not find the ram module. You need to add the folder where your manage.py resides to PYTHONPATH

这似乎是一个路径问题。您调用manage.py的路径通常会添加到PYTHONPATH,如果您调用django-admin.py则不会。因此,您的应用程序找不到ram模块。您需要将manage.py所在的文件夹添加到PYTHONPATH

EDIT

编辑

django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

django.core.exceptions.ImproperlyConfigured:请求设置DEBUG,但未配置设置。您必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。

Means you have to tell django, which settings. So enter the following line

意味着你必须告诉django,哪些设置。因此,请输入以下行

export DJANGO_SETTINGS_MODULE='ram.settings'

or start django-admin the following way

或者按照以下方式启动django-admin

django-admin.py runserver --settings='ram.settings'

(With the changed pythonpath, of course)

(当然,改变了pythonpath)

#1


9  

Try:

尝试:

export PYTHONPATH=/path/to/folder/with/manage.py:$PYTHONPATH

It seems like it is a path problem. the path from where you call manage.py is usually added to PYTHONPATH, if you call django-admin.py it is not. Your application therefore does not find the ram module. You need to add the folder where your manage.py resides to PYTHONPATH

这似乎是一个路径问题。您调用manage.py的路径通常会添加到PYTHONPATH,如果您调用django-admin.py则不会。因此,您的应用程序找不到ram模块。您需要将manage.py所在的文件夹添加到PYTHONPATH

EDIT

编辑

django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

django.core.exceptions.ImproperlyConfigured:请求设置DEBUG,但未配置设置。您必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。

Means you have to tell django, which settings. So enter the following line

意味着你必须告诉django,哪些设置。因此,请输入以下行

export DJANGO_SETTINGS_MODULE='ram.settings'

or start django-admin the following way

或者按照以下方式启动django-admin

django-admin.py runserver --settings='ram.settings'

(With the changed pythonpath, of course)

(当然,改变了pythonpath)