简体   繁体   English

“from .import views”:未解决的导入

[英]"from . import views": Unresolved import

I'm following the Django 1.8 tutorial .我正在关注Django 1.8 tutorial In my project mysite , there is a source folder polls .在我的项目mysite ,有一个源文件夹polls In the folder there is views.py module where a index function is defined.在文件夹中有定义了index函数的views.py模块。 And there is a urls.py file:还有一个urls.py文件:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

This is what the tutorial suggests, and Django works well with the codes.这就是教程所建议的,Django 可以很好地处理这些代码。 However Eclipse (PyDev) complains of unresolved imports for views .然而Eclipse (PyDev)抱怨unresolved importsviews unresolved imports If I remove from . import views如果我from . import views from . import views and use auto-correction function, PyDev would recommend import views , now Django complains that " the name views is not defined ". from . import views并使用自动更正功能, PyDev会推荐import views ,现在 Django 抱怨“名称视图未定义”。 I tried with and without __init__.py in the folder, both give the same results.我尝试在文件夹中使用和不使用__init__.py ,两者都给出相同的结果。

I am using the latest version for both Django (1.8) and PyDev (4.2) .我正在使用Django (1.8)PyDev (4.2)的最新版本。

Thanks!谢谢!

The project was created with the PyDev wizard as a Django project .该项目是使用PyDev向导作为Django 项目创建的 When it was created, the folder polls is not a source folder.创建时,文件夹polls不是源文件夹。 As a result, no code analysis was performed.结果,没有进行代码分析。 So I changed the folder polls (which is inside the project folder mysite ) to a source folder.因此,我将文件夹polls (位于项目文件夹mysite )更改为源文件夹。 Now the code was analyzed and unresolved import error was raised.现在分析了代码并引发了unresolved import错误。

The fix is to change polls back to a normal folder (removed from PYTHONPATH ), and instead set the top-level project folder mysite as a source folder.修复方法是将polls改回普通文件夹(从PYTHONPATH 中删除),而是将顶级项目文件夹mysite为源文件夹。 Now both PyDev and Django work well.现在PyDevDjango都运行良好。

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

what you need is to change the.你需要的是改变。 with your parent folder.与您的父文件夹。 that's what I did to solve my problem.这就是我为解决我的问题所做的。 for example the folder of my project is called blog so I made Do this : from blog import views instead of from .例如,我的项目文件夹名为 blog,所以我做了Do this : from blog import views而不是from 。 import views导入视图

from django.conf.urls import url
from blog import views
urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
]

from django.urls import path from . 从django.urls导入路径。 import views urlpatterns=[path('',views.home,name='home'),] 导入视图urlpatterns = [path('',views.home,name ='home'),]

Traceback (most recent call last): File "c:/Users/a.eed/Desktop/blogger/src/my_blog/blog/urls.py", line 2, in from . 追溯(最近一次通话最近):文件“ c:/Users/a.eed/Desktop/blogger/src/my_blog/blog/urls.py”,位于第2行。 import views ImportError: attempted relative import with no known parent package 导入视图ImportError:尝试相对导入,没有已知的父包

Please help to solve the problem 请帮忙解决问题

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

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