西维蜀黍

【Nginx】macOS 下安装 Nginx

安装Nginx

更新homebrew

$ brew update

查看nginx信息

$ brew search nginx

安装nginx

$ brew install nginx

对应的配置文件地址在/etc/nginx/nginx.conf

  ...


【Node.js】使用 nvm 管理本地 Node.js 版本

Background

在我们的日常开发中经常会遇到这种情况:手上有好几个项目,每个项目的需求不同,进而不同项目必须依赖不同版的 NodeJS 运行环境。如果没有一个合适的工具,这个问题将非常棘手。

nvm 应运而生,nvm 是 Mac 下的 node 管理工具,有点类似管理 Ruby 的 rvm,如果需要管理 Windows 下的 node,官方推荐使用 nvmwnvm-windows。不过,nvm-windows 并不是 nvm 的简单移植,他们也没有任何关系。但下面介绍的所有命令,都可以在 nvm-windows 中运行。

  ...


【Django】处理 PUT 和 DELETE 方法

Context

使用django的小伙伴们应该都知道我们是无法开心的处理PUT和DELETE:

def func(request):
    if request.method == 'GET':
        s = request.GET.get('s', None)
        return XXX
    elif request.method == 'POST':
        s = request.POST.get('s', None)
        return XXX
    elif request.method == 'PUT':
        s = request.PUT.get('s', None)                    # 我们希望愉快的获取继续处理
        return XXX
    elif request.method == 'DELETE':
        s = request.DELETE.get('s', None)                # 我们希望愉快的获取继续处理
        return XXX
    else:
        pass
  ...


【Python】杂 - virtualenv 管理 Python 项目依赖

Background

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

总结来说,virtualenv 是用来隔离不同 Python 项目的开发与运行环境。一个典型的场景是,有一个基于 Python 2.7 的 Django 项目,基于的 Django 版本是 1.1,而同时我们本机上正在开发一个同样基于 Python 2.7 的 Django 项目,不同的是这个项目基于Django 1.2 版本。

我们通过 pip install django来安装 Django,而这个 Django 依赖包默认会保存在/usr/lib/python2.7/site-packages,且这两个项目均会通过访问 /usr/lib/python2.7/site-packages来完成我们的 Django 项目的编译。

这意味着,我们要么安装 1.1 的 Django 版本,要么安装 1.2的版本。

通过使用 virtualenv 后,它会在我们的工程文件夹下创建一个 venv 文件夹,里面包含了这个项目使用的 Python 可执行程序(或者称为 compiler 或者 interpreter)、所有依赖的第三方包。

最终,就解决了不同项目可能需要依赖不同版本的第三方包的问题。

  ...


【Python】Django - 连接 MySQL

MySQLdb(MySQL-python)

MySQL-python 又叫 MySQLdb,是 Python 连接 MySQL 最流行的一个驱动,很多框架都也是基于此库进行开发,遗憾的是它只支持 Python2.x,而且安装的时候有很多前置条件,因为它是基于C开发的库,在 Windows 平台安装非常不友好,经常出现失败的情况,现在基本不推荐使用,取代的是它的衍生版本。

# 前置条件
sudo apt-get install python-dev libmysqlclient-dev # Ubuntu
sudo yum install python-devel mysql-devel # Red Hat / CentOS

# 安装
pip install MySQL-python

安装完依赖,直接使用pip安装,MySQLdb模块的名字在pip上叫MySQL-python。

$ pip install MySQL-python

mysqlclient

由于 MySQL-python 年久失修,后来出现了它的 Fork 版本 mysqlclient,完全兼容 MySQLdb,同时支持 Python3.x,是 Django ORM的依赖工具,如果你想使用原生 SQL 来操作数据库,那么推荐此驱动。安装方式和 MySQLdb 是一样的。

# Windows安装
pip install some-package.whl

# linux 前置条件
sudo apt-get install python3-dev # debian / Ubuntu
sudo yum install python3-devel # Red Hat / CentOS
brew install mysql-connector-c # macOS (Homebrew)

pip install mysqlclient

PyMySQL

PyMySQL是一个纯Python写的MySQL客户端,它的目标是替代MySQLdb,可以在CPython、PyPy、IronPython和Jython环境下运行。PyMySQL在MIT许可下发布。

步骤

1 安装 pymysql

$ pip install pymysql

2 修改 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',        #数据库名字
        'USER': 'root',          #账号
        'PASSWORD': '123456',      #密码
        'HOST': '127.0.0.1',    #IP
        'PORT': '3306',                   #端口
    }
}

3 init.py里面导入pymysql模块

# [your app in your project]/init.py

import pymysql
pymysql.install_as_MySQLdb()
  ...