官术网_书友最值得收藏!

  • Flask Blueprints
  • Jo?l Perras
  • 492字
  • 2021-07-30 10:17:01

Chapter 1. Starting on the Right Foot – Using Virtualenv

One of the great difficulties in modern software development is that of dependency management. Generally, a dependency of a software project consists of a library or component that is required for the project to function correctly. In the case of a Flask application (and more generally, that of a Python application), most dependencies are comprised of specially organized and annotated source files. Once created, these packages of source files may then be included in other projects and so forth. For some, this chain of dependencies can become an unmanageable mess, where the slightest alteration to any of the libraries in the chain can cause a cascade of incompatibilities that would bring further development to a screeching halt. In the Python world, as you may know already, the fundamental unit of a reusable set of source files is that of a Python module (a file that contains definitions and statements). Once you've created a module on your local filesystem and ensured that it is in your system's PYTHONPATH, including it in a newly created project is as simple as specifying the import, which is as follows:

import the_custom_module

Where the_custom_module.py is a file that exists somewhere in $PYTHONPATH of the system executing the program.

Note

The $PYTHONPATH can include paths to the compressed archives (.zip folders) in addition to the normal file paths.

This is not where the story ends, of course. While modules littering your local filesystem might be convenient at first, what happens when you want to share some of the code that you've written for others? Usually, this would entail emailing/Dropboxing the files in question, however, this is obviously a very cumbersome and error-prone solution. Thankfully, this is a problem that has been considered and some progress has been made in alleviating the common issues. The most significant of these advances is the subject of this chapter, and how the following techniques for creating reusable, isolated packages of code can be leveraged to ease the development of a Flask application:

  • Python packaging with pip and setuptools
  • Encapsulation of virtual environments with virtualenv

The solution presented by the various Python packaging paradigms/libraries is far from perfect; one sure way to start an argument with a passionate Python developer is to proclaim that the packaging problem has been solved! We still have a long way to go for that but headway is being made in incremental steps with improvements to setuptools and various other libraries used in building, maintaining, and distributing a reusable Python code.

In this chapter, when we refer to a package, what we will actually be talking about would be succinctly described as a distribution—a bundle of software to be installed from a remote source—and not a collection of modules in a folder structure that utilizes the__init__.py convention in order to delineate the folders containing the modules that we want to be importable.

主站蜘蛛池模板: 高要市| 罗城| 丹东市| 石柱| 乐亭县| 繁昌县| 泉州市| 宁阳县| 洪湖市| 汤阴县| 镇远县| 松潘县| 周口市| 石狮市| 文登市| 磐安县| 康平县| 辽源市| 阿拉善右旗| 高唐县| 青田县| 沁水县| 贵阳市| 项城市| 望都县| 云阳县| 乐清市| 阿拉善盟| 同德县| 卓资县| 新源县| 永丰县| 武川县| 安化县| 民丰县| 闸北区| 汕头市| 鄯善县| 正镶白旗| 邵阳市| 师宗县|