python项目结构一般如下:
my_project/
├── my_project/
│ ├── __init__.py
│ ├── module1.py
│ ├── module2.py
│ ├── subpackage/
│ │ ├── __init__.py
│ │ └── submodule.py
│ └── main.py
├── requirements.txt
├── setup.py
├── README.md
└── .gitignore
其中main.py为主程序,若无模块引用与包,就不用创建 my_project/文件夹,直接放main.py与即requirements.txt同级可。
requirements.txt
在Python项目中,通常会使用requirements.txt文件来记录项目所需的依赖项。可以在requirements.txt文件中添加注释,说明每个依赖项的用途或特殊注意事项。
以下是一个简单的requirements.txt文件示例:
# 生产依赖项
Flask==2.0.2
requests==2.26.0
numpy==1.21.4
# 开发依赖项
pytest==6.2.5
flake8==4.0.1
在项目中使用requirements.txt文件时,可以通过运行以下命令来安装所有依赖项:
pip install -r requirements.txt
这将自动安装requirements.txt文件中列出的所有依赖项。
在requirements.txt文件中,应该列出项目所需的所有第三方库和框架,而不需要列出Python标准库中的内置库。Python标准库中的内置库会随着Python解释器一起安装,因此不需要在requirements.txt文件中列出。
setup.py
编写 setup.py
文件是为了配置项目的元数据和安装信息,使得项目可以打包和发布。
下面是一个简单的示例,展示了如何编写一个基本的 setup.py
文件:
from setuptools import setup, find_packages
setup(
name='my_project', # 包的名称
version='0.1.0', # 版本号
packages=find_packages(), # 包含的包列表,使用 find_packages() 可以自动发现所有包
install_requires=[ # 项目依赖的第三方包列表
'requests>=2.20.0',
'numpy>=1.15.0',
],
entry_points={ # 可执行命令的入口点
'console_scripts': [
'my_command=my_project.module1:main_function',
],
},
author='Your Name', # 作者信息
author_email='your.email@example.com',
description='Description of my project', # 项目描述
long_description=open('README.md').read(), # 长描述,通常从 README 文件中读取
long_description_content_type='text/markdown', # 长描述的内容类型
url='https://github.com/yourusername/my_project', # 项目主页
license='MIT', # 许可证信息
classifiers=[ # 分类器列表,用于 PyPI 的分类
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
)
在这个示例中,setup()
函数接受一系列参数,包括包名称、版本号、依赖项、入口点、作者信息、描述、许可证信息等。
README.md
README.md
文件通常用于向其他开发者或用户介绍项目,并提供项目的相关信息、用法、安装说明等。通常,README.md
文件采用 Markdown 格式编写,以便于呈现简单的格式化文本和链接。
以下是一个示例的 README.md
模板文件内容:
# Project Name
Brief introduction of the project.
## Table of Contents
- [Background](#background)
- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)
## Background
Provide background information about the project. This can include the project's origin, objectives, and main features.
## Installation
Provide instructions for installing the project. This may include using pip, installing from source, or other methods.
## Usage
Provide some usage examples and code snippets to demonstrate the basic usage of the project.
## Contributing
If you want to contribute to this project, you can follow these steps:
Fork the project
Create your feature branch (git checkout -b feature/my-feature)
Commit your changes (git commit -am 'Add some feature')
Push to the branch (git push origin feature/my-feature)
Create a new Pull Request
## License
This project is licensed under the [License Type] License - see the LICENSE file for details.
```bash
pip install <package-name>
.gitignore
以下是一个通用的.gitignore文件示例,可以用于排除常见的临时文件和构建文件:
# 排除常见的临时文件和构建文件
*.pyc
*.pyo
__pycache__/
*.so
*.out
# 排除编辑器临时文件
*~
*.swp
*.swo
# 排除系统生成的文件
.DS_Store
ehthumbs.db
Icon?
Thumbs.db
# 排除IDE生成的文件和目录
.vscode/
.idea/
*.iml
# 排除依赖和虚拟环境
venv/
env/
# 排除日志文件
*.log
以下是一个适用于Python项目(使用PyCharm编写)的通用.gitignore文件示例:
# 排除常见的临时文件和构建文件
__pycache__/
*.pyc
*.pyo
*.pyd
*.pyz
# 排除IDE生成的文件和目录
.idea/
*.iml
*.ipr
*.iws
# 排除PyCharm项目文件
*.charm
# 排除虚拟环境和依赖
venv/
env/
venv.bak/
*.egg-info/
*.egg/
dist/
build/
# 排除本地配置文件
*.sqlite3
thonny.sqlite
# 排除日志文件
*.log
这个.gitignore文件会排除常见的临时文件、构建文件、IDE生成的文件和目录、PyCharm项目文件、虚拟环境和依赖以及本地配置文件等内容。
基本上这些都写了,python的项目结构就写好了,方便进一步打包上传python项目。