簡體   English   中英

從3移植到2.7:ImportError:無法導入名稱

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM