Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python must-have artifacts


May 10, 2021 Python2


Table of contents


1. Pip is used for package management

Document: Https://pip.pypa.io/en/latest/installing.html

# 安装,可指定版本号
(sudo) pip install Django==1.6.8

# 升级
(sudo) pip install bpython --upgrade

# 一次安装多个
(sudo) pip install BeautifulSoup4 fabric virtualenv

# 从文本中安装,文本中为包名,一行一个,可以指定版本号
(sudo) pip install –r requirements.txt

# 删除
(sudo) pip uninstall xlrd

# 导出当前已经安装包
pip freeze > requirements.txt

2. Virtualenv independent Python environmental management

Document: Http://virtualenvwrapper.readthedocs.org/en/latest/

Virtualenv is a package that creates a Python stand-alone environment, and virtualenvwrapper makes virtualenv more useful

# 安装:
(sudo) pip install virtualenv virtualenvwrapper

# 修改.bash_profile 或 .zshrc(如果你用 zsh 的话),添加以下语句
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /usr/local/bin/virtualenvwrapper.sh

mkvirtualenv ENV: Create a running environment ENV

rmvirtualenv ENV: Remove the operating environment ENV

mkproject mic: Create a mic project and run environment mic

mktmpenv: Create a temporary running environment

workon bsp: Works in a bsp running environment

lsvirtualenv: Lists the available operating environments

lssitepackages: Lists the packages installed in the current environment

The environment you create is independent, non-disturbing, and you can use pip for package management without sudo permissions.

Here's a demo:

Python must-have artifacts

3. Fabric server management and app publishing

Website: http://www.fabfile.org/

Document: Http://docs.fabfile.org/

fabric: application deployment or systems administration tasks

#coding:utf-8

from fabric.api import *

# 服务器列表
env.hosts = ['user@server1','user2@server2']

def ls_home():
    with cd('/home/bae/'):
        run('ls')


'''
常用命令

lcd(dir): 进入本机某目录
local(cmd): 本机上执行命令
cd(dir): 进入服务器某目录
run(cmd):服务器上执行命令
'''

Save the file above as fabfile.py enter the directory of the file on the terminal and execute

fab 函数名
比如:
fab ls_home

For more information, see the official documentation.