Python project standard structure, I recommend this!

What is this article about?
Doing data analysis is accustomed to using Jupyter notebook, writing a few lines of code, it is very convenient to get the results.

But in real projects, PyCharm or VSCode is generally used for development. Then some friends asked, is there a standard structure for the project file directory?

There should be no single unified standard, but the Python community leader Kenneth Reitz proposed a Python project directory structure in 2013, which is recommended for your reference in future projects.

Python project recommended structure
Kenneth recommends the directory structure as follows:

samplemod-master
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.rst
├── docs
│   ├── Makefile
│   ├── conf.py
│   ├── index.rst
│   └── make.bat
├── requirements.txt
├── sample
│   ├── __init__.py
│   ├── core.py
│   └── helpers.py
├── setup.py
└── tests
    ├── __init__.py
    ├── context.py
    ├── test_advanced.py
    └── test_basic.py
复制代码

A total of 3 folders, 17 files.

Let's take a look at the first folder docs, which is the project-related documentation, including Makefile and make.bat installed by dependent packages, project configuration conf.py, project introduction document index.rst, which is more detailed than README.rst A document, README.rst is more like a project overview.

The second folder, sample, is generally called a package in Python. It is the core code of the project. There are usually multiple such packages. __init__.py is the file included in the Python package. core.py and helpers.py are two core modules in the sample package.

The third package, tests, is the unit test package corresponding to the sample package. The module name in it generally starts with test, and tests each module in the sample.

What does setup.py do?
With the above three main packages, the most important file is setup.py. What does this file do?

Simply put, setup.py is a configuration module related to packaging, generally combined with setuptools.

With setup.py, we can install the package to the local global environment, or upload it to PyPi, so that developers around the world have the opportunity to search for your package and use pip to install your package.

The code inside is relatively standard and fixed. For example, the code in this project is as follows:

from setuptools import setup, find_packages


with open('README.rst') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='sample',
    version='0.1.0',
    description='Sample package for Python-Guide.org',
    long_description=readme,
    author='Kenneth Reitz',
    author_email='[email protected]',
    url='https://github.com/kennethreitz/samplemod',
    license=license,
    packages=find_packages(exclude=('tests', 'docs'))
)
复制代码

See that the README.rst and LICENSE files are used here, and specify which packages to distribute.

以上就是本次分享的所有内容,想要了解更多欢迎前往公众号:Python 编程学习圈,每日干货分享

Guess you like

Origin juejin.im/post/7101138954002366501