ModuleNotFoundError:没有名为'PyDemo.accounts'的模块

时间:2017-04-18 12:48:53

标签: python django

我正在开发一个Django应用程序,它将托管一个简单的index.html文件:

url.py:

from django.conf.urls import url,include
from django.contrib import admin
from PyDemo.accounts.views import my_view 

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^demo/',my_view)
]

views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext

# Create your views here.

def my_view(request):
    return render_to_response('index.html',locals(),context_instance=RequestContext(request))

但是,当我运行应用程序时,我收到以下错误:

    return import_module(self.urlconf_name)
  File "C:\Users\tcssuoy\Desktop\DJANGO\mysite\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\tcssuoy\workspace\PyDemo\PyDemo\PyDemo\urls.py", line 18, in <module>
    from PyDemo.accounts.views import my_view 
ModuleNotFoundError: No module named 'PyDemo.accounts'

2 个答案:

答案 0 :(得分:0)

对于任何有此问题的人,他们都希望从Python 2迁移到3,这似乎是因为Python 3对在哪里可以找到模块更加了解。 @Ranjith Singhu的评论使我朝着正确的方向前进。就我而言,我必须更改:

Python 2:

from views import MyView

Python 3(在2中也可以使用):

from project_name.views import MyView

答案 1 :(得分:0)

您的项目名称似乎是 PyDemo,模块名称是 accounts。所以尝试使用

from accounts.views import my_view

您可能会发现由 IDE 引起的错误(红色下划线),因此请忽略并迁移它。它会起作用