[英]Backporting from 3 to 2.7: ImportError: cannot import name
After reading this , and crying a lot, I'm trying to make my Django app work with Python 2.7. 看完这篇文章 ,哭了很多之后,我试图使我的Django应用程序与Python 2.7一起使用。
Here's my Django web dir: 这是我的Django网站目录:
├── locale
│ ├── en
│ ├── fr
│ └── sv
├── produits
│ ├── migrations
│ └── templatetags
├── pyweb
├── templates
│ └── produits
├── third_party
│ ├── authomatic_0_1_0
│ ├── defusedxml-0.4.1
│ ├── google_appengine_1_9_25
│ ├── python-openid_2_2_5
│ └── python3-openid
└── uploads
The most important to notice is that I've tried to add all my "external" module into a folder third_party
. 需要注意的最重要的一点是,我尝试将所有“外部”模块添加到文件夹
third_party
。
In my views.py
, the following code was working: 在我的
views.py
,以下代码正在工作:
from third_party.authomatic_0_1_0 import authomatic
from third_party.authomatic_0_1_0.authomatic import adapters
from third_party.authomatic_0_1_0.authomatic.providers import oauth1, oauth2
It was working because I added those lines in settings.py
at the very beginning: 之所以起作用,是因为我一开始就在
settings.py
中添加了这些行 :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR+'/third_party/defusedxml-0.4.1')
sys.path.append(BASE_DIR+'/third_party/python3-openid')
sys.path.append(BASE_DIR+'/third_party/google_appengine_1_9_25')
sys.path.append(BASE_DIR+'/third_party/authomatic_0_1_0')
But now, with python 2.7 it doesnt work anymore. 但是现在,在python 2.7中它不再起作用了。 What should I do to make it work?
我应该怎么做才能使其正常工作? And what is a good practice in Python (because Pycharm doesn't recognize all the subfolders of
third_party
)? 在Python中有什么好的做法(因为Pycharm不能识别
third_party
所有子文件夹)?
Two problems: (1) encoding, so I added in all files at the very beginning: 两个问题:(1)编码,因此我在一开始就添加了所有文件:
# coding=UTF-8
(2) Decorator python_2_unicode_compatible
: needed this for all my models with __str__
implemented, for example: (2)Decorator
python_2_unicode_compatible
:我实现了__str__
所有模型都需要装饰器,例如:
@python_2_unicode_compatible
class Texte(models.Model):
texte = models.CharField(max_length=200)
def __str__(self):
return self.texte
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.