在Mac OS X 上搭建Python和Django开发环境

文章转自: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得到全部的配置文件。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339

推荐阅读更多精彩内容