雖然 Python 2.7 已停止維護(2020/01/01 EOL),但維護舊專案時仍需此環境。本文記錄從零安裝到 Hello World 的完整步驟,新專案請直接使用 Python 3.8+。
python-2.7.3.msi (官網或鏡像站)。C:\Python27。C:\Python27;C:\Python27\Scripts 加入系統環境變數 PATH。features、plugins 資料夾覆蓋至 Eclipse 同名目錄。C:\Python27\python.exe,自動偵測系統庫路徑。# 下載原始碼包
# https://pypi.python.org/pypi/Django/1.4
# 解壓後進入目錄
python setup.py install
或使用 easy_install Django==1.4(需先安裝 setuptools)。
Windows 下直接 easy_install MySQL-python 常因缺 Visual C++ 編譯器失敗。建議:
pip install MySQL_python-1.2.5-cp27-none-win32.whl (需先 pip install wheel)。djangoTest。helloworld.py:from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World!')
urls.py:from django.conf.urls.defaults import patterns
urlpatterns = patterns('',
(r'^$', 'djangoTest.helloworld.index'),
)
https://127.0.0.1:8000/ 應顯示 Hello World!。ImportError: No module named MySQLdb → MySQL-Python 未正確安裝,檢查 site-packages 或重裝 .whl。python 非內部命令 → 環境變數未生效,重啟 CMD/Eclipse 或重新登入。settings.py 的 DATABASES 設定、防火牆 3306、MySQL 允許遠端/本機連線。新專案請直接使用:
venv / conda / poetrymysqlclient (PyMySQL) 或 ORM (SQLAlchemy/Tortoise-ORM)