Python[Defining Python Source Code Encodings]文件编码详解

Python文件如果没指定文件编码格式时,默认使用ASCII,所以源文件中使用了中文字符时,需要在第一行或者第二行这样指定编码格式:

# coding=<encoding name>

或者采用大多数编辑器认可的格式:

#!/usr/bin/python
# -*- coding: <encoding name> -*-

又或者:

#!/usr/bin/python
# vim: set fileencoding=<encoding name> :

准确来说,第一行或者第二行需要符合以下正则表达式:

^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)

一般情况下,我们使用如下代码放在第一行即可:

# coding: utf-8

参考文档:https://www.python.org/dev/peps/pep-0263/

Python[Mac OS X]环境搭建之Virtualenv

上篇已简单介绍如何安装python以及依赖的各种库,实际上应用中会遇到很多问题,例如:
pip install mysqlclient
安装mysql时经常会遇到mysql_config找不到,或者安装完成后,在import mysql时抛出异常,提示so中 Symbol not found: _mysql_affected_rows

再例如不同的工程需要依赖的特定库版本不同,又或者python2切换到python3…

于是我们今天学习使用Virtualenv来创建纯粹的单个python依赖环境,解决上面的这些问题。

1、安装

$ pip install virtualenv

2、Usage:

$ virtualenv ENV

在当前目录下创建了ENV文件夹,存放我们准备配置的python环境
3、Activate

$ source bin/activate

在ENV/bin目录下执行activate, 切换到该工作环境
4、Deactivate

$ deactivate

退出该工作环境

附录:PyCharm在Project Interpreter中也可以快速新建一个工程适用的virtualenv,打开Python Console可以看到python是用得哪个环境下的

==============分割线===================
后记:为了找到mysqlclient安装失败的原因,尝试了N次,以及修改其setup.py才明白原因。
1、mysql_config是mysql安装时产生的可执行文件(这就需要从mysql官网上下载适合系统的版本,我的机器上安装了多个版本的mysql,任选一个都可以)
2、setup.py中使用了mysql_config命令执行,所以需要将mysql_config文件所在的bin目录,放到PATH环境变量中,如:PATH=”/usr/local/mysql-5.7.10-osx10.9-x86_64/bin:${PATH}”
3、site.cfg中“mysql_config = /usr/local/bin/mysql_config”需要替换为你的该文件路径,如果你需要从zip解压文件安装的话,或者你没有设置PATH
4、卸载错误的版本 pip3 uninstall mysqlclient
5、重新安装 pip3 install mysqlclient

Python[Mac OS X]搭建服务器后台运行环境

1、Python2.7
默认Mac应该会安装了python2.7版本,如果需要更换版本,可以在官方网站上下载:
https://www.python.org/downloads/
2、Setuptools 7.0
按照官网的提示:Install pip, setuptools, and wheel
从官网下载的Python 2 >=2.7.9 or Python 3 >=3.4 已经包含了pip以及setuptools
可以更新到最新版本:
pip install -U pip setuptools
实际更新的时候会遇到坑……

Collecting setuptools
  Downloading setuptools-36.2.6-py2.py3-none-any.whl (477kB)
    100% |████████████████████████████████| 481kB 187kB/s 
Installing collected packages: setuptools
  Found existing installation: setuptools 18.5
    Uninstalling setuptools-18.5:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 778, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 299, in move
    copytree(src, real_dst, symlinks=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 208, in copytree
    raise Error, errors
Error: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', "[Errno 1] Operation not permitted: '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', "[Errno 1] Operation not permitted: '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', "[Errno 1] Operation not permitted: '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', "[Errno 1] Operation not permitted: '/tmp/pip-PlzVRR-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib'")]

果断用zip文件安装成功,下载页面:
https://pypi.python.org/pypi/setuptools/7.0#downloads
解压 setuptools-7.0.zip
执行 sudo python setup.py install
3、Django
官方网站:https://www.djangoproject.com/download/
安装简单,直接运行:
pip install Django==1.11.3
提示我“OSError: [Errno 13] Permission denied: ‘/Library/Python/2.7/site-packages/django’”
于是加上sudo:
sudo -H pip install Django==1.11.3

关于Python3.x的问题,因为已经存在Python2.7版本了,想同时使用3.x的时候,需要使用命令python3来区别,另外pip也对应有pip3来执行,对应上面的两条命令如下:
pip3 install -U pip setuptools
pip3 install Django==1.11.3
验证安装是否正确:
执行 python3
命令行输入:
import django
print (django.get_version()) 或者django.VERSION
显示结果:

Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print (django.get_version())
1.11.3
>>> django.VERSION
(1, 11, 3, 'final', 0)