![](/img/trans.png)
[英]import airflow -> ImportError: cannot import name version (with Python 2.7)
[英]Backporting from 3 to 2.7: ImportError: cannot import name
看完这篇文章 ,哭了很多之后,我试图使我的Django应用程序与Python 2.7一起使用。
这是我的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
需要注意的最重要的一点是,我尝试将所有“外部”模块添加到文件夹third_party
。
在我的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
之所以起作用,是因为我一开始就在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')
但是现在,在python 2.7中它不再起作用了。 我应该怎么做才能使其正常工作? 在Python中有什么好的做法(因为Pycharm不能识别third_party
所有子文件夹)?
两个问题:(1)编码,因此我在一开始就添加了所有文件:
# coding=UTF-8
(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.