Python software directory structure specification and module import

Source: Internet
Author: User

Why standardize the software directory structure?

    1. High readability: People who are unfamiliar with the code for this project can read the directory structure at a glance, know which program startup script is, where the test directory is, where the configuration file is, and so on. So that we can understand and follow up the project very quickly.
    2. High maintainability: Once you have defined your organization's rules, the maintainer will be able to clearly know which file and code should be placed under what directory. The benefit is that as time goes on, the size of the code/configuration increases and the project structure is not cluttered and can still be well organized.

Therefore, in the actual project development process, the good directory structure is very necessary. Even if the pursuit of personal style, but also to follow the basis of the basic norms, otherwise the code will only be written by themselves, which is the same as in the code to write the necessary comments are the same.

How directory is organized

Assuming the project name is foo, the necessary directory structure is as follows:

foo/|--bin/|   | -- foo| | --foo/|   | --tests/|   |   __init__ . py|   |   | -- test_main.py|   | |   __init__ . py|   | -- main.py| | --docs/|   | -- conf.py|   | -- Abc.rst| | -- setup.py|-- requirements.txt|--README

Briefly explain:

    1. bin/: Store some executable files of the project, of course you can name and script/ so on.
    2. foo/: Stores all source code for the project. (1) All modules and packages in the source code should be placed in this directory. Do not place the top level directory. (2) Its subdirectory tests/ holds Unit test code , and (3) The entrance of the program is preferably named main.py .
    3. docs/: Store Some documents, configuration files, etc.
    4. setup.py: Install, deploy, and package the scripts.
    5. requirements.txt: A list of external Python packages that store software dependencies, which is just a txt file.
    6. README: Project description file.

About configuration files:

Here the configuration file conf.py is not directly placed in the source directory, but placed in the docs/directory, the configuration of the module should be flexible, not affected by the external configuration file, so you should not directly import conf in the code to use the configuration file , you can main.pythe startup parameter specifies the way to configure the path for the program to read the configuration content.

About setup.py

In general, use it setup.py to manage the packaging, installation, and deployment of your code. The industry standard notation is to use Python's popular packaging tools Setuptools to manage these things. This approach is commonly used in open source projects. But the core idea here is not to use standardized tools to solve these problems, but to say that a project must have an installation deployment tool that can quickly and easily install the environment on a new machine, deploy the code, and run the program.

When consolidating packaging, check that the list of Python packages documented in requirements matches the Python package actually used, including the version of the package used

About Requirements.txt

The purpose of this file exists is to:

      1. Easy for developers to maintain software package dependencies. Add the new packages in the development process to this list to avoid setup.py missing packages when installing dependencies.
      2. Make it easy for readers to identify which Python packages the project uses, and quickly follow up on the program through the package learning.

The list of external Python packages that the software relies on in Requirements.txt is best and the Python package that the software actually uses in the development process remains in real-time, so that there is no omission when the setup.py file is finally packaged.

About Readme

This, like setup.py, is a document that each project should have, with the aim of briefly describing the project's information so that the reader can quickly understand the project.

It needs to illustrate several things:

      1. Software positioning, the basic functions of the software.
      2. How to run your code: Installation environment, startup commands, and so on.
      3. Brief instructions for use.
      4. Code directory structure Description, more detailed can explain the basic principles of the software.
      5. FAQ's description.

The role of the Readme is as if the user manual is a smart way to write the document, which is often written at the end of the project.

Because of the existence of the software directory structure, it is unavoidable that cross-catalog Import module exists .

Module Introduction

Learn a few concepts first:

    1. Module (python file): Python files that are essentially the end of. Py.
    2. Packages (Python package): essentially a directory (that is, the directory structure specification described above), writing modules in packages, organizing modules, and automatically generating a __init__.py file for each Python package created.

Import Method:

    1. To import a module method:
1 #Basic Import Module2 ImportSYS3 #importing multiple modules at the same time4 Importsys, copy, numpy, Pandas5 #importing multiple modules at the same time and partially renaming them6 ImportSYS, numpy as NP, pandas as PD, TensorFlow as TF7 8 #import a module from a package9  fromTensorflow.contribImportRnn

Imports can only import modules from the current directory, standard libraries, and third-party libraries. In the import, you can also directly import a variable or method in the module, that is, import xxx.xxx (XXX can be a variable or method)

2. Multi-level Catalog Import method:

There are two modules to know:

    • SYS module
    • OS Module
1 #Multi-level Catalog Import method:2 Importsys, OS3Sys.path ()#returns all paths that perform the search performed by the current file4BaseDir = Os.path.abspath (__file__)#gets the path to the current file5Adddir = Os.path.dirname (Os.path.dirname (BaseDir))#Get parent Directory address6Sys.path.append (Adddir)#Add a change address to the default search path7  fromLogsImportLogger#importing modules from a package

Sys.path (): The return value is a list that contains all the paths of the search.

Sys.path.append (): is to append the target path to the end of the list, that is, in the search module, the current directory will be searched first, and then to the standard library, third-party library to search, if not changed module, will be in the appended path to search. If a module with the same name exists in a standard library or a third-party library, it will not be accessible to the module under the Add path.

Sys.path.insert (): Adds the path to the front of the Python system path list

Python software directory structure specification and module import

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.