Python日常笔记

2016-05-25

关闭*.pyc文件生成

import 别的 py 文件时,那个 py 文件会被存一份 pyc 加速下次装载。

但是测试时为了不影响目录结构可以关闭:

import sys
sys.dont_write_bytecode = True

安装pip

1.安装easy_install

wget https://bootstrap.pypa.io/ez_setup.py --no-check-certificate
python ez_setup.py --insecure

2.安装pip 下载地址:https://pypi.python.org/pypi/pip

解压,安装.
wget https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz#md5=3823d2343d9f3aaab21cf9c917710196
tar -zxvf pip-7.1.2.tar.gz 
cd pip-7.1.2
python setup.py install

安装pip (推荐用该方法,自动集成依赖)

apt-get install python-pip  # Ubuntu

yum install python-pip   # RedHat

pip使用国内源

linux的文件在~/.pip/pip.conf,windows在%HOMEPATH%\pip\pip.ini

[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

使用多个源(备用):

[global]
timeout=40
index-url = http://mirrors.aliyun.com/pypi/simple/
extra-index-url = http://pypi.douban.com/simple/
[install]
trusted-host=
    mirrors.aliyun.com
    pypi.douban.com

pypi国内镜像目前有:

http://pypi.douban.com/simple/  豆瓣
http://mirrors.aliyun.com/pypi/simple/  阿里云

pip参数解释

# pip --help

Usage:
  pip <command> [options]

Commands:
  install                     安装软件.
  uninstall                   卸载软件.
  freeze                      按着一定格式输出已安装软件列表
  list                        列出已安装软件.
  show                        显示软件详细信息.
  search                      搜索软件,类似yum里的search.
  wheel                       Build wheels from your requirements.
  zip                         不推荐. Zip individual packages.
  unzip                       不推荐. Unzip individual packages.
  bundle                      不推荐. Create pybundles.
  help                        当前帮助.

General Options:
  -h, --help                  显示帮助.
  -v, --verbose               更多的输出,最多可以使用3次
  -V, --version               现实版本信息然后退出.
  -q, --quiet                 最少的输出.
  --log-file <path>           覆盖的方式记录verbose错误日志,默认文件:/root/.pip/pip.log
  --log <path>                不覆盖记录verbose输出的日志.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --timeout <sec>             连接超时时间 (默认15秒).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
  --cert <path>               证书.

Anaconda conda 的使用

miniconda安装 (官方下载地址:https://conda.io/miniconda.html):

wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh
bash Miniconda2-latest-Linux-x86_64.sh

scipy安装(指定清华源):

conda install -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r scipy

修改conda为国内源:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

直接安装:

conda install numpy
conda install scikit-learn

神经网络依赖包:

conda install numpy
conda install pandas
conda install scipy

查看安装列表:

conda list
# packages in environment at /root/miniconda2:
#
cffi                      1.9.1                    py27_0  
conda                     4.3.13                   py27_0  
conda-env                 2.6.0                         0  
cryptography              1.7.1                    py27_0  
enum34                    1.1.6                    py27_0  
idna                      2.2                      py27_0  
ipaddress                 1.0.18                   py27_0  
libffi                    3.2.1                         1  
libgfortran               3.0.0                         1  
mkl                       2017.0.1                      0  
numpy                     1.12.0                   py27_0  
openssl                   1.0.2k                        0  
pip                       9.0.1                    py27_1  
pyasn1                    0.1.9                    py27_0  
pycosat                   0.6.1                    py27_1  
pycparser                 2.17                     py27_0  
pyopenssl                 16.2.0                   py27_0  
python                    2.7.13                        0  
readline                  6.2                           2  
requests                  2.12.4                   py27_0  
ruamel_yaml               0.11.14                  py27_1  
scikit-learn              0.18.1                    <pip>
scipy                     0.18.1              np112py27_1  
setuptools                27.2.0                   py27_0  
six                       1.10.0                   py27_0  
sklearn                   0.0                       <pip>
sqlite                    3.13.0                        0  
tk                        8.5.18                        0  
wheel                     0.29.0                   py27_0  
yaml                      0.1.6                         0  
zlib                      1.2.8                         3  

Python二维码识别库

apt-get install python-qrtools

使用方法:

#!/usr/bin/env python
import qrtools
qr = qrtools.QR()
qr.decode("qr.png")
print qr.data

python有序字典

import collections

d = collections.OrderedDict()

d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'

用Python建立最简单的web服务器

python -m SimpleHTTPServer 8080

错误

pip list 显示错误异常:

[ root@0d4c1b027902:~ ]$ pip list
argparse (1.2.1)
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/list.py", line 80, in run
    self.run_listing(options)
  File "/usr/lib/python2.7/dist-packages/pip/commands/list.py", line 142, in run_listing
    self.output_package_listing(installed_packages)
  File "/usr/lib/python2.7/dist-packages/pip/commands/list.py", line 151, in output_package_listing
    if dist_is_editable(dist):
  File "/usr/lib/python2.7/dist-packages/pip/util.py", line 366, in dist_is_editable
    req = FrozenRequirement.from_dist(dist, [])
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 286, in from_dist
    assert len(specs) == 1 and specs[0][0] == '=='
AssertionError

Storing debug log for failure in /root/.pip/pip.log

解决办法:

  • to remove ubuntu package: sudo apt-get remove –auto-remove python-pip
  • download official pip installer: wget https://bootstrap.pypa.io/get-pip.py
  • install pip by running downloaded installer: sudo python get-pip.py
  • add sym-link to make it run without full path: sudo ln -s /usr/local/bin/pip /usr/bin/pip

Done!