Python开发的环境设置

概要

之所以谈到 python 开发的环境, 是因为python的2.7版本和3.*版本目前都有一定的应用. 开发环境中兼容这两个版本就显得尤为必要. 另一个原因是python 在 Ubuntu, Mac 上的包管理多多少少还是有些坑要填. 因为 python 支持 pip 和 easy_install, 同时这两个包管理系统又难免有些小的问题发生.

工具

推荐使用pyenv这个工具来管理多个 python 环境. 使用这个工具, 你将得到如下的好处:

  • 自由切换多个python 版本
  • 避免了Linux 和 Mac 下安装包需要权限的问题
  • 包管理可以只用 pip, 回避了 easy_install 可能有的各种问题

安装

这里以 Mac 为例子:

brew install pyenv

安装完毕后, 记得初始化一下

echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

注意如果是Ubuntu, 则是 ~/.bashrc

为了shell中只有切换 python 环境, 需要在~/.bash_profile文件中,添加如下的一行:

if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

验证安装

在终端下输入 pyenv :

pyenv 20160628
Usage: pyenv  []

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help ' for information on a specific command.
For full documentation, see: https://github.com/yyuu/pyenv#readme

使用

  • 查看系统中存在的 python 版本:
pyenv versions
  • 查看可以安装的版本
pyenv install -l
  • 安装指定版本
pyenv install <版本名称>
  • 使用指定版本的 python (全局)
pyenv global <版本名称>

同时不要忘记了指定 shell 使用的 python 版本

pyenv shell <版本名称>

设置指定的版本后, 可以使用 which python 来检查一下时候设置好了
例如:

$ which python
/Users/username/.pyenv/shims/python
  • 总结
    安装和使用都很简单, 推荐给喜爱 python 的你.

你可能感兴趣的:(Python开发的环境设置)