mac 安装python和Django开发环境
博客专区 > xxjbs001 的博客 > 博客详情
mac 安装python和Django开发环境
xxjbs001 发表于3年前
mac 安装python和Django开发环境
  • 发表于 3年前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

文章转自: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系统的启动设置你还可以做什么改进?留下你的意见吧!

共有 人打赏支持
粉丝 53
博文 297
码字总数 215619
×
xxjbs001
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: