


Four Python project management and construction tools, recommended collection!
Python has not had a de facto standard project management and construction tool for so long, resulting in a variety of Python project structures and construction methods. This may reflect Python's free will.
Unlike Java, it has gone through the initial manual construction, to semi-automated Ant, and then to Maven, which is basically the de facto standard. During this period, Maven also accepted challenges from other Gradle (mainly promoted by Android projects), SBT (mainly Scala projects), Ant Ivy, Buildr, etc., but it was difficult to shake Maven's status in the world, and the others almost followed Maven's directory layout. .
Back in Python, there have been package management tools such as pip, pipenv, and conda, but there is no agreement on the directory layout of the project.
Many aspects of building still follow the traditional Makefile method, and then add setup.py and build.py to use program code to install and build. Regarding the project directory layout, some make project templates, and then make tools to apply the project templates.
The following is a brief overview of the use of the four tools
- CookieCutter
- PyScaffold
- PyBuilder
- Poetry
CookieCutter A classic Python project directory structure
$ pip install cookiecutter $ cookiecutter gh:audreyr/cookiecutter-pypackage # 以 github 上的 audreyr/cookiecutter-pypackage 为模板,再回答一堆的问题生成一个 Python 项目 ...... project_name [Python Boilerplate]: sample ......
The final project template generated by cookiecutter is as follows It looks like:
$ tree sample sample ├── AUTHORS.rst ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.rst ├── docs │ ├── Makefile │ ├── authors.rst │ ├── conf.py │ ├── contributing.rst │ ├── history.rst │ ├── index.rst │ ├── installation.rst │ ├── make.bat │ ├── readme.rst │ └── usage.rst ├── requirements_dev.txt ├── sample │ ├── __init__.py │ ├── cli.py │ └── sample.py ├── setup.cfg ├── setup.py ├── tests │ ├── __init__.py │ └── test_sample.py └── tox.ini 3 directories, 26 files
This is probably the main framework of the currently popular directory structure. The main elements are:
$ tree sample sample ├── Makefile ├── README.rst ├── docs │ └── index.rst ├── requirements.txt ├── sample │ ├── __init__.py │ └── sample.py ├── setup.cfg ├── setup.py └── tests ├── __init__.py └── test_sample.py
Repeat in the project sample directory. Place the Python source file in the sample directory, and place the Python source file in the tests directory. It is a test file, plus a docs directory for documentation, README.rst, and other setup, setup.cfg and Makefile files for building.
This is actually a very classic Python project structure. The next build uses the make command. Enter make and you will see the instructions defined in the Makefile file.
$ make cleanremove all build, test, coverage and Python artifacts clean-buildremove build artifacts clean-pycremove Python file artifacts clean-test remove test and coverage artifacts lint check style test run tests quickly with the default Python test-all run tests on every Python version with tox coverage check code coverage quickly with the default Python docs generate Sphinx HTML documentation, including API docs servedocscompile the docs watching for changes releasepackage and upload a release dist builds source and wheel package installinstall the package to the active Python's site-packages
In order to use the above build process, you need to install the corresponding packages, such as tox, wheel, coverage, sphinx, flake8, they can all be installed through pip. Then you can make test, make coverage, make docs, make dist, etc. Among them, make docs can generate a beautiful web document.
PyScaffold Create a project
PyScaffold As the name suggests, it is a tool used to create Python project scaffolding. Install and use:
$ pip install pyscaffold $ putup sample
Create like this I created a Python project, and the directory structure is similar to the template selected by cookiecutter, except that it places the source files in the src directory instead of the sample directory.
$ tree sample sample ├── AUTHORS.rst ├── CHANGELOG.rst ├── CONTRIBUTING.rst ├── LICENSE.txt ├── README.rst ├── docs │ ├── Makefile │ ├── _static │ ├── authors.rst │ ├── changelog.rst │ ├── conf.py │ ├── contributing.rst │ ├── index.rst │ ├── license.rst │ ├── readme.rst │ └── requirements.txt ├── pyproject.toml ├── setup.cfg ├── setup.py ├── src │ └── sample │ ├── __init__.py │ └── skeleton.py ├── tests │ ├── conftest.py │ └── test_skeleton.py └── tox.ini
The entire project requires the use of tox. tox is an automated testing and build tool that creates a Python virtual environment during the build process, allowing a clean environment for testing and building.
tox -av can display all the tasks defined in tox.ini:
$ tox -av default environments: default -> Invoke pytest to run automated tests additional environments: build -> Build the package in isolation according to PEP517, see https://github.com/pypa/build clean -> Remove old distribution files and temporary build artifacts (./build and ./dist) docs-> Invoke sphinx-build to build the docs doctests-> Invoke sphinx-build to run doctests linkcheck -> Check for broken links in the documentation publish -> Publish the package you have been developing to a package index server. By default, it uses testpypi. If you really want to publish your package to be publicly accessible in PyPI, use the `-- --repository pypi` option.
Use tox -e build, tox -e docs, etc. to which command you want to execute
During my experience with the tox command, every step seemed to be slow. It should take some time to create a virtual machine.
PyBuilder
It’s best to look at another build tool PyBuilder. The directory structure it creates is very close to Maven. Let’s take a look
$ pip install pybuilder $ mkdir sample && cd sample# 项目目录需手工创建 $ pyb --start-project# 回答一些问题后创建所需的目录和文件
After finishing, take a look at its directory structure:
$ tree sample . ├── build.py ├── docs ├── pyproject.toml ├── setup.py └── src ├── main │ ├── python │ └── scripts └── unittest └── python
The building process still uses the pyb command. You can use pyb -h to view the help, and pyb -t to list all tasks. The tasks of PyBuilder are in the form of plug-ins. Added, the plug-in configuration is in the build.py file.
$ pyb -t sample Tasks found for project "sample": analyze -Execute analysis plugins. depends on tasks: prepare run_unit_tests clean - Cleans the generated output. compile_sources - Compiles source files that need compilation. depends on tasks: prepare coverage - <no description available> depends on tasks: verify install - Installs the published project. depends on tasks: package publish(optional) package - Packages the application. Package a python application. depends on tasks: compile_sources run_unit_tests(optional) prepare - Prepares the project for building. Creates target VEnvs print_module_path - Print the module path. print_scripts_path - Print the script path. publish - Publishes the project. depends on tasks: package verify(optional) coverage(optional) run_integration_tests - Runs integration tests on the packaged application. depends on tasks: package run_unit_tests - Runs all unit tests. Runs unit tests based on Python's unittest module depends on tasks: compile_sources upload - Upload a project to PyPi. verify - Verifies the project and possibly integration tests. depends on tasks: run_integration_tests(optional) $ pyb run_unit_tests sample
PyBuilder also creates a virtual environment before building or testing. Starting from version 0.12.9, you can skip the step of creating a virtual environment through the parameter --no-venvs. If --no-venvs is used, the Python code will be executed in the current Python environment running pyb, and the required dependencies will have to be installed manually.
The dependencies of the project must also be defined in the build.py file.
@init def set_properties(project): project.depends_on('boto3', '>=1.18.52') project.build_depends_on('mock')
The above dependencies will be installed when pyb is executed to create a virtual environment, and tests and builds will be run in it.
Poetry
The last Poetry, I feel that this is a more mature and more active Python build. It has more powerful trust management functions. Use poetry add boto3 to add dependencies, poetry show --tree displays the dependency tree. Take a look at how to install and create a project
$ pip install poetry $ poetry new sample
The project it creates is simpler than the above
$ tree sample sample ├── README.rst ├── pyproject.toml ├── sample │ └── __init__.py └── tests ├── __init__.py └── test_sample.py
If you give poetry new the --src parameter, the source file directory sample will be placed in src directory, that is, sample/src/sample.
poetry init will generate the pyproject.toml file in the current directory, and the generation of directories must be completed manually.
It does not focus on document generation, code specification checking, or code coverage. Its project configuration is more centralized, all in the pyproject.toml file. What is toml? It is a configuration file format Tom's Obvious, Minimal Language (https://github.com/toml-lang/toml).
pyproject.toml is somewhat similar to the NodeJS package.json file. For example, poetry add, poetry install command lines
# 往 pyproject.toml 中添加对boto3 的依赖并安装(add 还能从本地或 git 来安装依赖 ), poetry add boto3 # 将依照 pyproject.toml 文件中定义安装相应的依赖到当前的 Python 虚拟环境中 # 比如在 <test-venv>/lib/python3.9/site-packages 目录中,安装好模块后也可让测试用例使用 poetry install
其他主要的
1.poetry build# 构建可安装的 *.whl 和 tar.gz 文件 2.poetry shell# 会根据定义在 pyproject.toml 文件中的依赖创建并使用虚拟环境 3.poetry run pytest# 运行使用 pytest 的测试用例,如 tests/test_sample.py 4.poetry run python -m unittest tests/sample_tests.py# 运行 unittest 测试用例 5.poetry export --without-hashes --output requirements.txt# 导出 requirements.txt 文件, --dev导出含 dev 的依赖,或者用 poetry export --without-hashes > requirements.txt
poetry run 能执行任何系统命令,只是它会在它要的虚拟环境中执行。所以可以想见,poetry 的项目要生成文档或覆盖率都必须用 poetry run ... 命令来支持 sphinx, coverage 或 flake8。
在 sample 目录(与 pyproject.toml 文件平级)中创建文件 my_module.py, 内容为
def main(): print('hello poetry')
然后在 pyproject.toml 中写上。
[tool.poetry.scripts] my-script="sample.my_module:main"
再执行
$ poetry run my-script
就会输出 "hello poetry"。
通过对以上四个工具的认识,项目结构的复杂度由 cookiecutter-pyproject -> PyScaffold -> PyBuilder -> Poetry 依次降低,使用的难度大略也是相同的顺序
The above is the detailed content of Four Python project management and construction tools, recommended collection!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

MySQL download file is corrupt, what should I do? Alas, if you download MySQL, you can encounter file corruption. It’s really not easy these days! This article will talk about how to solve this problem so that everyone can avoid detours. After reading it, you can not only repair the damaged MySQL installation package, but also have a deeper understanding of the download and installation process to avoid getting stuck in the future. Let’s first talk about why downloading files is damaged. There are many reasons for this. Network problems are the culprit. Interruption in the download process and instability in the network may lead to file corruption. There is also the problem with the download source itself. The server file itself is broken, and of course it is also broken when you download it. In addition, excessive "passionate" scanning of some antivirus software may also cause file corruption. Diagnostic problem: Determine if the file is really corrupt

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration
