简体   繁体   English

Django 1.9 ImportError:外壳上没有名为“ appname”的模块

[英]Django 1.9 ImportError: No module named 'appname' on shell

I have a project say MyProject with the given directory structure, where the apps are installed in the applications directory. 我有一个具有给定目录结构的项目MyProject ,其中应用程序安装在applications目录中。

.
├── myapp
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── applications
│   └── polls
│       ├── admin.py
│       ├── apps.py
│       ├── __init__.py
│       ├── models.py
│       ├── tests.py
│       └── views.py
├── db.sqlite3
├── manage.py
└── wsgi
    ├── static
    │   └── keep.me
    └── wsgi.py

My polls/apps.py content is 我的polls/apps.py内容是

from __future__ import unicode_literals

from django.apps import AppConfig


class PollConfig(AppConfig):
    name = 'applications.polls'

and the polls/__init__.py content, 以及polls/__init__.py内容,

default_app_config='applications.polls.apps.PollConfig'

and the INSTALLED_APPS in settings, 以及设置中的INSTALLED_APPS

INSTALLED_APPS = [
    ......
    ......
    'applications.polls'
]

the shell throws ImportError: No module named polls 外壳程序引发ImportError: No module named polls

How do I fix it? 我如何解决它?

thank you so much. 非常感谢。

update (adds traceback) 更新(添加回溯)

Traceback (most recent call last):
  File "<input>", line 5, in <module>
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/apps/config.py", line 123, in create
    import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/opt/pycharm-5.0.3/helpers/pydev/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: No module named polls

update: python manage.py shell traceback 更新:python manage.py shell追溯

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/marty/.virtualenvs/dj19/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 101, in handle
    code.interact(local=imported_objects)
AttributeError: 'module' object has no attribute 'interact'

You need to create a __init__.py file under applications directory to use it as a package/ module. 您需要在Applications目录下创建__init__.py文件,以将其用作软件包/模块。

create a file named applications/__init__.py 创建一个名为applications/__init__.py的文件

另外,如果您的意思是django shell或者如果您只是想从python shell中使用您的应用程序,则必须首先import django然后调用django.setup()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM