文章轉自:http://article.yeeyan.org/view/252423/228631,謝謝原作者,記錄一下,防止丟失。
在Mac OS X Lion 上搭建Python和Django開發環境
第一次安裝Lion系統后,我想知道我之前常用的那些工具在新操作系統下的表現如何。在對啟動過程做了些細微的修改后,我得到了一個相當給力的Python/Django開發環境,現在馬上就讓它運行起來吧!
第一步:
Lion系統默認去掉了“Sites”文件夾,但很容易加回去 - 自定義圖標甚至會自動彈出來。使用Find查找工具,或者在命令行終端輸入如下命令:
另一個不同之處是隱藏的~/Library文件夾。我們可以通過如下的命令使它再次可見(當系統升級后修改會被覆蓋,因此必須再次執行命令):
chflags nohidden ~/Library/
由於Lion是全64位系統,我們得讓編譯器知道所有的編譯工作都需要指定為64位,這會讓我們之后省點頭痛的麻煩。打開 ~/.bash_profile
vim ~/.bash_profile
添加如下代碼:
# Set architecture flags
export ARCHFLAGS="-arch x86_64"
Xcode
Installing development-related software usually requires the compiler tool-chain that comes with Xcode. There is at least one alternative GCC installer, but I ran into problems with it and recommend sticking with Xcode.
安裝開發相關的軟件通常需要涉及到XCode自帶的編譯工具鏈。這里至少有一種可替代的工具——GCC installer, 但我在使用時遇到了些麻煩因此我推薦堅持使用XCode。
First, download Xcode from the Mac App Store. Make sure you're on a high-bandwidth connection, because this is a massive download. Once it's been downloaded, you should have a 3.2GB app entitled "Install Xcode" in your Applications folder. I recommend backing up this installer in case you ever want to re-install it or install Xcode on any of your other Macs. Once the straightforward Xcode installation process has finished, proceed to the next section.
首先,從蘋果App商店下載XCode。保證你的帶寬足夠大,因為下載這個大玩意可不是件輕松的事情。當下載完成后,你的Application文件夾中應該有一個名為“Install Xcode”的3.2G大小的安裝程序。我建議你備份一下這個安裝程序,以便你之后想重新安裝或者在你其他的Mac上安裝XCode。當XCode一鍵式的安裝過程結束后,前進到下一節。
Homebrew
Sometimes you may need cross-platform software — usually without a GUI and accessible only via the command line — that isn't readily available via the Mac App Store. As someone who used MacPorts for years, I can't begin to explain the relative awesomeness that is Homebrew. It's an indispensable tool that should be in every Mac developer's arsenal, so let's install it:
有些時候你需要跨平台的軟件——通常都是不帶圖形界面只能通過命令行方式訪問的——這在蘋果app商店中可不是隨處可見的哦。作為MacPorts的多年用戶,我到現在都無法表達出對Homebrew的贊美之情。這是每一個Mac開發者不可或缺的神器啊,讓我們裝上它:
ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
That's it — Homebrew is now installed. While it should have the latest version of Homebrew and its formulae, let's run the update command just in case:
就是這樣 —— 現在Homebrew已經安裝好了。按照慣例,Homebrew應該是最新版本,以防萬一,讓我們運行一下升級命令
brew update
If the "brew update" command produces an error, make sure /usr/local is owned by you and not by root:
如果“brew update”命令執行出錯,請確保文件夾/usr/local的所有者權限是你本人而不是root:
sudo chown $USER /usr/localbrew update
The Homebrew wiki has a full command list and a bunch of other useful information, but here's a quick command to install some packages that I often find useful across all my Macs:
Homebrew的wiki上有全部的命令列表還有一些其他的有用的信息,這里有個快捷的命令用來安裝一些我覺得十分有用而且能夠在我所有的Mac系統上運行的工具包:
brew install bash-completion byobu growlnotify ssh-copy-id wget
Python模塊安裝包
Let's say you want to install a Python package, such as the fantastic virtualenv environment isolation tool. Nearly every Python-related article for Mac OS X tells the reader to install it thusly:
你打算安裝一個Python包,比如經典的virtualenv環境隔離工具。在Mac系統上幾乎每一篇與Python相關的文章都告訴讀者要這樣安裝:
sudo easy_install virtualenv
Here's why I don't do it that way:
我不這么做的原因是:
installs with root permissions
1. 安裝需要root權限
installs into the global /Library instead of the user's
2. 這會安裝到全局的/Library文件夾中,而不是用戶文件夾
"Distribute" has less bugs than the default legacy easy_install
3. “Distribute”安裝系統比默認的easy_install的bug要少
If I install a Python package and decide I want to delete it, I like having the option to use the Finder to drag it to the Trash without any permission-related complaints. Plus, keeping the Python packages isolated to the user account just makes more sense to me. Last but not least, Distribute is a fork of Setuptools/easy_install that aims to be more reliable with less bugs.
如果我安裝了一個Python包之后又決定刪除它時,我喜歡用Finder將它拖到回收站,沒有任何權限相關的警告跳出來煩我。而且,對我而言保持Python包和用戶賬戶隔離顯得更有意義一些。最后,“Distribute”是一系列的安裝工具,其目標就是更加穩定,更少的bug。
Traditionally, I've always used the "--user" flag to install Python packages into ~/.local, a behavior that was consistent across Macs, Linux, and other UNIX systems. It seems Lion has changed this behavior, however, installing into ~/Library/Python/2.7 when it encounters the --user flag. Since I prefer to have this location remain consistent across the various systems I encounter, I changed the flag from "--user" to the more specific "--prefix=~/.local". Whichever you choose, please keep in mind that the instructions here assume the latter.
一般來說,我總是使用“--user”選項來把Python包安裝到~/.local目錄中。這種行為在Mac,Linux和其他Unix系統上是一致的。看起來Lion系統似乎改變了這種行為,當Lion遇到“--user”選項時,Python包將安裝到~/Library/Python/2.7目錄中。由於我希望將這個路徑在不同的系統中保持
統一,因此我將“--user”選項改為更具體的“--prefix=~/.local”。不管你選擇哪種,請記住后面的內容假定你選擇的是后者。
With that tangent behind us, let's install Distribute:
帶着這種考量,我們安裝Distribute:
安裝到~/.local目錄唯一的缺點就是我們需要手動加入一些路徑到PATH和PYTHONPATH環境變量中去。編輯一下.bash_profile吧...
增加下面幾行代碼:
# Path ------------------------------------------------------------if [ -d ~/.local/bin ]; then export PATH=~/.local/bin:$PATH exists.fi# Python path -----------------------------------------------------if [ -d ~/.local/lib/python2.7/site-packages ]; then export PYTHONPATH=~/.local/lib/python2.7/site-packages:$PYTHONPATHfi# Load in .bashrc -------------------------------------------------
source ~/.bashrc
Let's load those directives now via:
現在,讓我們加載這些設定:
source ~/.bash_profile
Lion's easy_install is normally located in /usr/bin, so let's make sure that our local version is the one that's used by default:
Lion系統自帶的easy_install通常放在/usr/bin目錄下,因此這里要確保我們自己安裝的版本是系統默認的:
which easy_install
確保得到的結果是~/.local/bin/easy_install, 這樣我們就全設置好了!
virtualenv and virtualenvwrapper(虛擬環境和虛擬環境包裝層)
安裝到~/.local下的Python包對於用戶來說的確是具有局部屬性的,但是它們在某種程度上也是全局的,因為對於用戶的所有工程項目它們都是可見的。有時候這會很方便,但這也會帶來一些問題。比如,有時候一個工程需要最新版本的Django,而另一個工程卻需要Django 1.2版來與一個至關重要的第三方擴展包維持兼容。而這正是設計virtualenv所用來解決的問題。virtualenv,virtualenvwrapper和Mercurial(一種源代碼版本管理工具)是唯一一直存在於我的系統中的軟件包。其它的每一個包都局限在它的虛擬環境中。
Let's install virtualenv and its companion virtualenvwrapper:
我們來安裝virtualenv和它的伙伴virtualenvwrapper:
easy_install --prefix=~/.local virtualenv virtualenvwrapper
We'll then open/create the ~/.bashrc file...
然后我們打開根目錄/,創建一個bash配置文件 ~/.bashrc
vim ~/.bashrc
vim ~/.bashrc
... and add some lines to it:
在其中增加如下幾行代碼:
django-admin.py startproject projectmv requirements.txt project/
We can now start Django's development web server...
我們可以啟動Django的開發web服務器了...
python manage.py runserver
... and see the results in our web browser. For those that are new to Django, the Django Tutorial is a good place to learn how to continue building your project.
在我們的瀏覽器里看看結果如何。對於Django的新手,Django教程是個學習如何繼續構建你的項目的好去處。
首次向版本管理工具提交
Now that you have a Django project started, use your preferred version control system to make your first commit. For Mercurial, that would be:
現在你已經有了一個Django項目工程,快使用你最趁手的版本管理工具來做第一次提交吧,對於Mercurial,那就是:
hg init projectcd projecthg addhg commit -m "Initial commit"
配置文件(類Unix系統中的配置文件都是以.開頭的,因此原文的dotfiles這里就是指的配置文件)
While I've posted excerpts of my dotfiles above, you can get the whole enchilada from my Bitbucket repository.
盡管我已經在前文給出了我的配置文件摘錄,你可以在我的存儲庫Bitbucket repository得到全部的配置文件。
咻!搞定
Congrats! It takes a bit of time to set up a new system, but it's well worth the journey. These are the tools of our trade, and we use them every day, so it makes sense to make them as efficient as possible.
恭喜!我們花了一點時間來設定一個新的系統,但這是很值得的。這就是我們的工具,我們將在每天的工作中使用它們,因此很有必要使它們盡可能的高效。
That said, the good can always be made better. What would you do to improve this Lion bootstrap process? Leave a comment below!
那就是說,好的總可以變得更好。對這個Lion系統的啟動設置你還可以做什么改進?留下你的意見吧!