Best Practice Of Python S Project Structure
There's no standard answer about how to structure the python's project.I'll introduce one relatively reasonable solution.Here's the example.
1.Chnagelog
Changelog is very important for a project.It records the release history of the project and helps the developer find bugs quickly.Here is the recommended example of a Changelog.
Normally,we often use three numbers to represent the version number.The first number tracks major changes,the second tracks minor changes,the last tracks patches or bug fix.
2.README
README
or README.md
is used to introduce the basic information of the project,including :
the purpose of the project
how to run the project
how to develop the project
introduce the structure of the code
3.build script
Python use wheel or egg to pack the code,we can use build script to reduce the redundant typing work.Here's the example.
We can also use a clean_build.sh
to clean the build products.
You maybe want to build and install with easy install.
Cleaning the build products is recommended because it will influence the navigate operation of some IDE.You may modify the built python code ,not the project files.Clean save your time!
4.setup.py
We always use setuptools
as the package tools.The documentation of setuptools
is here.I'll only give some instructions about the basic usage of this tool base on one example.
setuptools
use setup
method to make the package.We just need to specify some parameters.Here's the meaning of some important arguments.
name:the name of the project
version:the version of the project ,you can specify it each time you make changes to your project.I recommend use a simple function
find_version
to read the version number from your Changelog file.You can just cpoy the function to yoursetup.py
,feel free to use it😏!description:the description of your project .
long_description:similar to description ,not important.
packages:this parameter is used to specify which Python packages you want to pack.Usually ,we use
setuptools.find_packages
to find the Python packages.where
means the package location ,include
means package name .You can use the shell blob to represent your Python package name.Here thesalvage*
means all the Python packages which hassalvage
as the prefix .For example ,salvagecore
and so on.You can also useexclude
to exclude some Python packages which you do not want to include in the package.package_dir :the
:
in the example means root directory ,src
means the Python package dir.install_requires:the dependency of the project.
easy_install
andpip
will automatically install these packges you specifed .package_data:the resource file you want to pack .Sometimes you need to add some non-python file in your package.The
''
in the example means root directory.**Attention,these means the package root.setuptools
will look up in all of your target packages' directories.Here is the files insrc
:the
www/*
means pack all files inwww
directory ,soadb.bundle.js
andindex.html
will be include in the package.include_package_data:if this parameter is
True
,thepackage_data
make sense .entry_points:this argument specify the entry point of your package.
console_scripts
means you can run the code from console after you installed the package.salvage = salvage.main:main
means the console script name issalvage
and the script entry point is themain
function in salvage package'smain.py
file.Python will generate a script file on thePATH
,which is usually/usr/local/bin
.python_requires:this stipulated the ranges the Python version of your packages.
>=3.7,<3.9
means you can use this package with Python 3.7/3.8/3.9.platform:this means the platform of your package ,
any
means all platform is compatible .
5.code structure
I recommend use src
to place all of your code and test
to place your test code.You can also use the project name to replace src
.
Last updated
Was this helpful?