Matrix
Everything that Has a Beginning Has an End
Browse: Home / 2012 / 1月 / Lion中基于Homebrew搭建python开发环境

Lion中基于Homebrew搭建python开发环境

By matrix on 2012/01/17

告别ubuntu,转移到mac上了。这里是在lion中搭建python开发环境的简单记录。这份记录不是一份step by step.而是事后写的记录,可能有记忆遗漏。如果有错误,请指正。

1.安装homebrew的准备:

$ sudo chown -R `whoami` /usr/local

homebrew希望在没有sudo的环境下工作(Homebrew is designed to work without using sudo.)所以首先对/usr/local/这个文件夹赋权限。(homebrew的软件是安装在这里的)

2.通过shell安装:

$ ruby -e "$(curl -fsSL https://gist.github.com/raw/323731/install_homebrew.rb)"

3.配置环境变量:

$ nano ~/.bash_profile

加入以下内容:

# Setting PATH for Homebrew
PATH="/usr/local/bin:${PATH}"
export PATH

Ok,这样homebrew就装好了。

4.brew的简单命令:

$ brew search <pkg_name>  #查找软件包
$ brew install <pkg_name>  #安装软件包
$ brew list #列出软件包
$ brew uninstall <pkg_name> #卸载软件包
$ brew update #更新
$ brew info <pkg_name> #查看软件包的基本资料

5.安装python

$ brew install python --framework --universal

The –framework option tells it to build as a Framework, which has some downstream niceties, and –universal builds a universal (32/64 bit) version.

6.将python加入path。

$ nano ~/.bash_profile
export PATH=/usr/local/share/python:$PATH

7.检查安装是否正确

此时,最好重启term。如果要确认修改是否正确,可以用which python和which easy_install查看python的路径是否正确。如果返回为

/usr/local/bin/python

和

/usr/local/share/python/easy_install

那么,安装正确

8.通过easy_install安装pip

$ easy_install pip

9.安装virtualenv 和 virtuanenvwarpper

$ /usr/local/share/python/pip install virtualenv
$ /usr/local/share/python/pip install virtualenvwrapper ##其实这个装不装已经无所谓了。

10.建立和项目相关的env

比如,项目文件在~/projects中,那么,

$ cd ~/projects
$ virtualenv --no-site-packages project_name
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in project_name/bin/python
Installing setuptools............done.
Installing pip...............done.

11.启动虚拟环境:

首先进入刚刚建立的虚拟环境目录,比如:

$ cd project_name
$ source bin/activate

注意,此时shell提示符前多了虚拟环境的名称提示:
(project_name)….$
Now,do anything you want.

12.退出虚拟环境

$ deactivate

13.pip的设定

如果要保证在虚拟环境中调用系统的pip,可以在~/.bash_profile里加上:

export PIP_REQUIRE_VIRTUALENV=true

如果要让系统的pip自动调用虚拟环境中的pip

export PIP_RESPECT_VIRTUALENV=true

14.安装你需要的软件包(注意提示符)

(project_name)....$ pip install pkg_name

【END】

Posted in osx | Tagged homebrew, Lion, osx, pip, python, virtuanenv

« Previous Next »

Copyright © 2023 Matrix.

Powered by WordPress and Hybrid.