我有一個我正在使用iPython進行交互的Django產品。使用iPython和Django中的配置文件
我想有自動加載的模塊時,我開始一個shell:
蟒蛇manage.py殼
我已經複製.ipython/ipythonrc到項目的根目錄下,並添加到文件:
import_some MODULE_NAME MODEL1 MODEL2
然而,當我開始外殼,這些名字都沒有被加載。
我在做什麼錯?
我有一個我正在使用iPython進行交互的Django產品。使用iPython和Django中的配置文件
我想有自動加載的模塊時,我開始一個shell:
蟒蛇manage.py殼
我已經複製.ipython/ipythonrc到項目的根目錄下,並添加到文件:
import_some MODULE_NAME MODEL1 MODEL2
然而,當我開始外殼,這些名字都沒有被加載。
我在做什麼錯?
我不知道ipythonrc,但如果你只需要在模型中,你可以使用django-extensions
。你安裝它之後,你已經有了新的同治命令,包括shell_plus
過多,這將打開一個IPython的會話,並自動加載所有模型:
python manage.py shell_plus
BryanWheelock您的解決方案是行不通的,因爲你的shell是產卵的結果不是與它直接交流。你想要做的是 - 或者至少這是我所做的。
在您的工作區內(鍵入python manage.py shell
的地方)創建一個ipythonrc文件。在它把以下內容:
include ~/.ipython/ipythonrc
execute from django.contrib.auth.models import User
# .
# .
# .
execute import_some module_name model1 model2
比如我還添加以下代碼行中礦..
# Setup Logging
execute import sys
execute import logging
execute loglevel = logging.DEBUG
execute logging.basicConfig(format="%(levelname)-8s %(asctime)s %(name)s %(message)s", datefmt='%m/%d/%y %H:%M:%S', stream=sys.stdout)
execute log = logging.getLogger("")
execute log.setLevel(loglevel)
execute log.debug("Logging has been initialized from ipythonrc")
execute log.debug("Root Logger has been established - use \"log.LEVEL(MSG)\"")
execute log.setLevel(loglevel)
execute log.debug("log.setlevel(logging.DEBUG)")
execute print ""
這可以讓你在你的模塊使用日誌記錄,並保持乾燥。希望這可以幫助。 Django的擴展
shell_plus
命令可以自動導入模型,但似乎不能加載IPython中的形象。我做了一些冒險的工作來完成這件事。
使用start_ipython
推出IPython的外殼,而不是embed
並傳遞一些參數給它。
我也寫過一篇博文,你可以找到詳細信息here