依赖性

为运行你的应用所必须用的那些库和模块就是你的项目依赖。

为什么依赖性重要?

Python web 应用常常构建与其他众多程序员的代码之上,不仅仅是开发框架,还包括爬虫、分词、分析、可视化等众多工作。

寻找库函数Finding libraries

Python函数库有一个中央存储仓库叫Python Package Index (PyPi)。PyPi可按照相关权重和相关性提供搜索功能。

除了PyPi还有其他“必备”资源:

隔离依赖性

为了预防系统版本冲突,有时候要隔离依赖性,最常用方法是 virtualenv。每一个virtualenv 有自己的一份Python解释器和依赖库,存放在自己的目录下。要使用virtualenv必须先创建并激活。

How the virtualenv separates dependencies on the server.

安装Python依赖包

先创建并激活virtualenv,然后推荐用 pip 安装依赖包。

requirements.txt

当你开发一个Python项目时应当包含一份 requirements.txt 在项目根目录下,例如:

django==1.6
bpython==0.12
django-braces==0.2.1
django-model-utils==1.1.0
logutils==0.3.3
South==0.7.6
requests==1.2.0
stripe==1.9.1
dj-database-url==0.2.1
django-oauth2-provider==0.2.4
djangorestframework==2.3.1

setup.py

There is another type of dependency specification for Python libraries known as setup.py. Setup.py is a standard for distributing and installing Python libraries. If you’re building a Python library, such as requests or underwear you must include setup.py so a dependency manager can correctly install both the library as well as additional dependencies for the library. There’s still quite a bit of confusion in the Python community over the difference between requirements.txt and setup.py, so read this well written post for further clarification.

依赖性资源

Open source app dependency projects

  • Autoenv is a tool for activating environment variables stored in a .env file in your projects’ home directories. Environment variables aren’t managed by virtualenv and although virtualenvwrapper has some hooks for handling them, it’s often easiest to use a shell script or .env file to set them in a development environment.
  • Pipreqs searches through a project for dependencies based on imports. It then generates a requirements.txt file based on the libraries necessary to run those dependencies. Note though that while this could come in handy with a legacy project, the version numbers for those libraries will not be generated with the output.

Application dependencies learning checklist

  1. Ensure the libraries your web application depends on are all captured in a requirement.txt file with pegged versions.
  2. An easy way to capture currently installed dependencies is with the pip freeze command.
  3. Create a fresh virtualenv and install the dependencies from your requirements.txt file by using the pip install -r requirements.txt command.
  4. Check that your application runs properly with the fresh virtualenv and only the installed dependencies from the requirements.txt file.

Git

Git is a distributed open source source control (also referred to as “version control”) system commonly used to track and manage file changes. Git is frequently used as the version control system for Python projects.

Git is an implementation of the source control concept. Learn how these pieces fit together in the development environments chapter or view the table of contents for all topics.

Git tutorials

Git can take awhile to wrap your head around, even for experienced software developers. The following tutorials can quickly get you up to speed.

  • The official Pro Git book is available online for free. It is awesome both as a step-by-step walkthrough and as a bookmarked reference on specific topics.
  • Git from the inside out provides a spectacular walkthrough for developers who have used Git before but want to go deeper in understanding what each command does under the covers instead of simply using the tool as a black box.
  • Git and GitHub in plain English is a high-level overview of both Git and GitHub. This guide is intended for both non-programmers and junior developers who want to learn everything from terminology to workflow.
  • A Designer’s Guide to Git gives a beginner’s Git overview for non-programmers. The tutorial also covers using Git clients such as the GitHub desktop application.
  • Git in Six Hundred Words is a concise essay explaining what happens when you add and commit files in a Git repository.
  • A practical git introduction is rich with pragmatic examples for Git usage.

Specific Git resources

Large tutorials are great for getting started with Git. However, sometimes you need tactical support or want to learn new tricks to add to your workflow. These resources will come in handy for specific Git subjects.

  • How to Write a Git Commit Message provides strong advice that will help you write consistent, concise and contextual messages on your commits. Commit messages are especially important when working with others on a long-lasting project where you dive through the commit history via git log and related commands.
  • Oh shit, Git! is a profanity-filled description of tips to get you out of binds you may find yourself in when you get too tricky with Git commands.
  • Phil Nash shows how to use the git reflog command in Git back to the future.
  • Another Git catastrophe cleaned up goes through a difficult merge scenario that required deep Git understanding to properly fix.
  • Erlang’s source code provides a concise explanation on writing good commit messages that any programming ecosystem can learn from.
  • GitTips is a list of pro tips to clean up common issues and how to dive through Git history to find specific text.
  • 19 Tips For Everyday Git Use is a laundry list of helpful Git tips on commands such as git bisect, git stash and git difftool.
  • Git allows command aliasing, which allowed one developer to create his own list of lesser known Git commands that alias more complicated Git lines.
  • Git from the inside out demonstrates how Git’s graph-based data structure produces certain behavior through example Git commands. This is a highly recommended read after you’ve grasped the basics and are looking to go deeper with Git.
  • How To Host Your Own Private Git Repositories provides the steps for handling private Git repositories on your own server. This setup is great for either mirroring GitHub repositories or just getting away from hosted services entirely.
  • How I configure my git in a new computer shows how to handle a .gitconfig file, with an example Gist that the author uses for his own environment.

Git Workflows

Teams of developers can use Git in varying workflows because of Git’s distributed model and lightweight branching. There is no “right way” to use Git, especially because development teams can range in size from a single developer up to entire companies with thousands of developers in a repository. The only correct answer is to let the developers decide on a workflow that maximizes their ability to frequently commit code and minimize merge conflicts.

  • git-flow shows one possible way for small teams to use Git branches. GitHub Flow explains why at GitHub they do not use the git-flow model and provides an alternative that solves some of the issues they found with git-flow.
  • Git Workflows That Work is a helpful post with diagrams to show how teams can create a Git workflow that will help their development process.
  • Our Git Workflow” by Braintree goes over how this payments company uses Git for development and merging source code.

源代码控制

源代码控制,也叫版本控制,记录了源代码变动的详细历史记录。

为什么版本控制是必需的?

版本控制允许程序员修改代码,但无需担心会搞砸事情。不想要的改动可以回滚到之前的稳定状态。

版本控制也使得团队合作开发变得容易。一个程序员可以把他改动过的代码更其他程序员的代码,通过diff进行逐行对比,再决定把合适的代码加入到主分支里去。

版本控制应当在所有软件项目一开始就启用,无论是用什么开发环境,语言,项目规模。

开发部署中的版本控制

Pulling code during a deployment is a potential way source control systems fit into the deployment process.

App deployment uses a server to pull from the source control system.

Note that some developers recommend deployment pipelines package the source code to deploy it and never have a production environment touch a source control system directly. However, for small scale deployments it’s often easiest to pull from source code when you’re getting started instead of figuring out how to wrap the Python code in a system installation package.

版本控制产品

  • Git 免费、开源的分布式版本控制系统。
  • Mercurial 类似Git,另一个免费、开源的分布式版本控制系统。

云端版本控制

Git 和 Mercurial都能下载并安装到你自己的服务器上。更简单方便的方法是使用云端版本控制系统(自己的服务器上就不用安装啦):

  • GitLab 有免费版,也有针对企业客户的收费版
  • GitHub 有开源、免费服务,也有付费的私人化版本控制服务
  • BitBucket 类似

常用版本控制资源

Git资源

版本控制学习对照表

  1. 选择一个版本控制系统。Git首选,因为使用方便,免费学习资源多。
  2. 学习基本操作,比如提交更改,回滚到前一版本,搜索某一行代码。
  3. 确保源代码在一个中央仓库里有备份。
  4. 在项目开发过程中使用版本控制。有3种方式,1,把源代码从仓库里拖到本地开发环境;2,把修改后的代码推到仓库;3,如果更改出错,能够回滚到前一版本。

Emacs

重点推荐实用PyCharm。介绍Emacs的内容,可看可不看。

Emacs is an extensible text editor that can be customized by writing Lisp code.

Why is Emacs a good choice for Python coding?

Emacs is designed to be customized via the built-in Lisp interpreter and package manager. The package manager, named package.el, has menus for handling installation. The largest Lisp Package Archive is Melpa, which provides automatic updates from upstream sources.

Macros are useful for performing repetitive actions in Emacs. A macro is just a recording of a previous set of keystrokes that can be replayed to perform future actions.

Hooks, which are Lisp variables that hold lists of functions to call, provide an extension mechanism for Emacs. For example, kill-emacs-hook runs before exiting Emacs so functions can be loaded into that hook to perform necessary actions before the exiting completes.

While you’re reading about coding Python in Emacs be sure to also learn about which web frameworks to use as well as how to deploy an application.

General Emacs resources

Notable Elisp Packages

  • Magit allows the user to inspect and modify Git repositories from within Emacs.
  • company-mode creates a modular in-buffer completion framework.
  • Flycheck provides syntax checking.
  • anaconda-mode is specific to Python development and allows code navigation, documentation lookup and code completion. The jedi library is used under the hood.
  • Tern is a stand-alone code-analysis engine for JavaScript. It can be integrated within a Django project via the tern-django package.

Popular user configurations

  • Prelude is an enhanced Emacs version 24 distribution.
  • A reasonable Emacs config shows a batteries-includes Emacs configuration bundle.
  • Emacs settings is a repository of configurations used in the Emacs Rocks screencasts.
  • Spacemacs mashes together Emacs’ extensibility and Vim’s ergonomic text editing features.

Vim

重点推荐实用PyCharm。介绍Vim的内容,可看可不看。

Vim, short for Vi IMproved, is a configurable text editor often used as a Python development environment. Vim proponents commonly cite the numerous plugins, Vimscript and logical command language as major Vim strengths.

Why is Vim a good Python development environment?

Vim’s philosophy is that developers are more productive when they avoid taking their hands off the keyboard. Code should flow naturally from the developer’s thoughts through the keyboard and onto the screen. Using a mouse or other peripheral is a detriment to the rate at which a developer’s thoughts become code.

Vim has a logical, structured command language. When a beginner is learning the editor she may feel like it is impossible to understand all the key commands. However, the commands stack together in a logical way so that over time the editor becomes predictable.

If you’re interested in Vim you’ll also want to read about development environments for coding and learn what makes Python a great programming language.

Configuring Vim with a Vimrc file

The Vimrc file is used to configure the Vim editor. A Vimrc file can range from nothing in it to very complicated with hundreds or thousands of lines of configuration commands.

Here’s a short, commented example .vimrc file I use for Python development to get a feel for some of the configuration statements:

" enable syntax highlighting
syntax enable

" show line numbers
set number

" set tabs to have 4 spaces
set ts=4

" indent when moving to the next line while writing code
set autoindent

" expand tabs into spaces
set expandtab

" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4

" show a visual line under the cursor's current line
set cursorline

" show the matching part of the pair for [] {} and ()
set showmatch

" enable all Python syntax highlighting features
let python_highlight_all = 1

Here is how these configuration options look with a dark background on Mac OS X while editing the markdown for this webpage (how meta!).

Vim with basic configuration options on a dark background.

Take a look at another example using these configuration options, this time with a light background and editing Python code from my Choose Your Own Adventures Presentations project.

Vim with basic configuration options on a white background.

The Vimrc file lives under the home directory of the user account running Vim. For example, when my user account is ‘matt’, on Mac OS X my Vimrc file is found at /Users/matt/.vimrc. On Ubuntu Linux my .vimrc file can be found within the /home/matt/ directory.

If a Vimrc file does not already exist, just create it within the user’s home directory and it will be picked up by Vim the next time you open the editor.

Vim tutorials

Vim has a reputation for a difficult learning curve, but it’s much easier to get started with these tutorials.

  • Learn Vim Progressively is a wonderful tutorial that follows the path I took when learning Vim: learn just enough to survive with it as your day-to-day editor then begin adding more advanced commands on top.
  • A vim Tutorial and Primer is an incredibly deep study in how to go from beginner to knowledgeable in Vim.
  • Why Atom Can’t Replace Vim discusses one of Vim’s core principles: command composability. Vim has a language where simple commands are combined to execute more advanced operations. For example, in command mode,$ moves to the end of a line. When $ is preceded by d then everything to the end of the line is deleted. Over time the simple commands become intuitive and the combinations become more powerful than having distinct commands such as a drop-down menu with a specific option to delete all text until the end of the line.
  • Vim as a Language explains the language syntax and how you can build up over time to master the editor.
  • How to install and use Vim on a cloud server along with How to use Vim for advanced editing of code on a VPS are two detailed Digital Ocean guides for getting up and running with Vim, regardless of whether you’re using it locally or on a cloud server.
  • Vim Adventures is a cute, fun browser-based game that helps you learn Vim commands by playing through the adventure.
  • In Vim: revisited the author explains his on-again off-again relationship with using Vim. He then shows how he configures and uses the editor so it sticks as his primary code editing tool.
  • Why Atom Can’t Replace Vim explains how the composability of Vim and the extensibility of Emacs allow both editors to remain relevant and very useful in the 21st century.
  • Things About Vim I Wish I Knew Earlier explores the lessons one developer learned while exclusively using Vim for several years. The author includes using relative instead of absolute line numbering, setting numerous configuration options and fuzzy finding to quickly open files in other directories rather than expanding the whole path.

Vimrc resources

These are a few resources for learning how to structure a .vimrc file. I recommend adding configuration options one at a time to test them individually instead of going whole hog with a Vimrc you are unfamiliar with.

Vim installation guides

These installation guides will help you get Vim up and running on Mac OS X, Linux and Windows.

  • Upgrading Vim on OS X explains why to upgrade from Vim 7.2 to 7.3+ and how to do it using Homebrew.
  • The easiest way to install Vim on Windows 7+ is to download and run the gvim74.exe file.
  • On Linux make sure to install the vim package with sudo apt-get install vim.
  • If you’re using PyCharm as your IDE you won’t need to install Vim as a separate text editor – instead use the IdeaVim PyCharm plugin to get Vim keybindings, visual/insert mode, configuration with ~/.ideavimrc and other Vim emulation features.

Using Vim as a Python IDE

Once you get comfortable with Vim as an editor, there are several configuration options and plugins you can use to enhance your Python productivity. These are the resources and tutorials to read when you’re ready to take that step.

  • VIM and Python – a Match Made in Heaven details how to set up a powerful VIM environment geared towards wrangling Python day in and day out.
  • The python-mode project is a Vim plugin with syntax highlighting, breakpoints, PEP8 linting, code completion and many other features you’d expect from an integrated development environment.
  • Vim as Your IDE discusses how to set up Vim for greater productivity once you learn the initial Vim language for using the editor.
  • Vim as a Python IDE goes through the steps necessary to make Vim into a more comfortable environment for Python development.
  • Setting up Vim for Python has a well written answer on Stack Overflow for getting started with Vim.
  • If you’re writing your documentation in Markdown using Vim, be sure to read this insightful post on a Vim setup for Markdown.

Vim Plugin Managers

  • Vundle comes highly recommended as a plugin manager for Vim.
  • Pathogen is a widely used plugin manager.
  • Vim-plug bills itself as a minimalistic Vim plugin manager.

Vim Plugin resources

开发环境

开发环境包括文本编辑器和Python解释器。前者用来写代码,后者用来执行代码。

一个开发环境示例

一个开发环境,MacBook Pro,装OS X,在里面用虚拟机环境Parallesl跑Ubuntu 16.04虚拟机。用Pycharm写代码,用Python 3.5解释器执行代码。用virtualenv创建各自独立的Python运行环境,以使用不同版本的Python解释器。

开源文本编辑器

  • vim是各种Unix和Linux系统的自带文本编辑器
  • emacs是另一个Unix和Linux系统常用文本编辑器
  • AtomGitHub出品的开源文本编辑器

Python IDE

  • PyCharm是专注于Python的IDE,从名字上就看得出来。专业版收费,社区版免费。
  • PyDevEclipse的Python IDE 插件。

私有闭源编辑器

  • Sublime Text 。
  • Komodo 是一款跨平台的文本编辑器,能编写Python, Ruby, JavaScript, Go等。

云端开发环境

基于云的开发环境已经出现好几年了,一般都是试用免费,正式部署收费。

  • CodeAnywhere一款能在浏览器、iOS,安卓都能使用的云端IDE。
  • Cloud9现在被亚马逊收购是其云服务的一部分。
  • Terminal 另一个云环境。

开发环境资源