Python的安装与使用

安装pyenv

pyenv用来管理多个版本的python在用户目录的安装和使用。

1
2
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
vim ~/.zshrc

添加

1
2
3
export PYENV_ROOT="$HOME/.pyenv
export PATH="$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"

1
2
3
➜  ~ source ~/.zshrc
➜ ~ pyenv -v
pyenv 1.2.2-14-gb95d0d9

使用pyenv安装python

1
2
3
4
5
6
7
8
9
10
11
12
➜  ~ pyenv versions # 显示本地系统可用python版本(*为当前shell使用的python版本)
* system (set by /Users/apple/.pyenv/version)
➜ ~ pyenv version # 显示当前shell使用的python版本
system (set by /Users/apple/.pyenv/version)
➜ ~ pyenv install -l # 显示远程python的可安装版本
➜ ~ pyenv install 3.5.0 # 安装3.5.0版本python
➜ ~ pyenv versions
* system (set by /Users/apple/.pyenv/version)
3.5.0
➜ ~ pyenv global 3.5.0
➜ ~ python -V
Python 3.5.0

使用实例

1
➜  python vim python1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
# -*- coding:utf-8 -*-

ten_things = "Apples Oranges Crows Telephone Light Sugar"

print "Wait there's not 10 things in that list, let's fix that."

stuff = ten_things.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]

while len(stuff) != 10:
next_one = more_stuff.pop()
print "Adding: ", next_one
stuff.append(next_one)
print "There's %d items now." % len(stuff)

print "There we go: ", stuff

print "Let's do some things with stuff."

print stuff[1]
print stuff[-1] # whoa! fancy
print stuff.pop()
print ' '.join(stuff) # what? cool!
print '#'.join(stuff[3:5]) # super stellar!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
➜  python python python1.py
Wait there's not 10 things in that list, let's fix that.
Adding: Boy
There's 7 items now.
Adding: Girl
There's 8 items now.
Adding: Banana
There's 9 items now.
Adding: Corn
There's 10 items now.
There we go: ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']
Let's do some things with stuff.
Oranges
Corn
Corn
Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
Telephone#Light

pip

pip是python模块(pypi)的包管理器。提供了对 Python 包的查找、下载、安装、卸载的功能。
配置pip源

1
vim ~/.pip/pip.conf

1
2
3
4
5
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

使用pip

1
2
3
4
5
6
➜  ~ pip list # 列出本地已经安装的pypi包
pip (9.0.3)
setuptools (18.2)
➜ ~ pip search requests # 搜索requests包
➜ ~ pip install requests # 安装requests包
➜ ~ pip download requests # 下载requests包

常用网站

官网:https://www.python.org/
pypi仓库:https://pypi.python.org/
django:https://www.djangoproject.comflask
flask: http://docs.jinkan.org/docs/flask
中文社区:http://python-cn.com
文档:http://docs.python.org
文档:http://pythondoc.com
文档:http://docs.pythontab.com
文档:https://yiyibooks.cn/
文档:http://effbot.org/