将 Python 代码打包并上传到 Conda 可以让其他人轻松地安装和使用你的库。以下是如何创建、上传和管理 Conda 包的详细步骤。

准备包

结构你的项目

一个良好组织的项目结构是成功创建 Python 包的关键。以下是一个推荐的项目结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
myproject/  # 项目根目录

├── mypackage/ # 包根目录,文件夹名就是包名了
│ ├── __init__.py # 包初始化文件
│ ├── coulomb.py # 子模块
│ ├── other.py # 子模块
│ └── readinfo.py # 子模块

├── LICENSE # 许可证文件
├── README.md # 项目说明文件
├── pyproject.toml # 包元数据和安装依赖的配置文件
├── setup.cfg # 包元数据和安装依赖的配置文件
└── meta.yaml # Conda配方文件

文件和目录说明

__init__.py: 标记目录为 Python 包目录,这对于导入模块时的包识别非常必要。
coulomb.py, other.py, readinfo.py: 不同的模块,包含具体的功能。
LICENSE: 包含你的代码的许可证信息。
README.md: 项目的 Markdown 说明文件,通常包含安装指南、使用示例以及开发和贡献指南。
pyproject.toml: 包含包元数据和安装依赖的配置文件,这是替代传统 setup.py 文件的现代方法。
setup.cfg: 包含包元数据和安装依赖的配置文件,这是替代传统 setup.py 文件的现代方法。
meta.yaml: Conda 配方文件,告诉 Conda 如何构建和打包库。
确保所有文件编码为 UTF-8,特别是README.mdsetup.cfg,以避免编码问题。

各文件撰写说明

LICENSE

通常,你可以选择一个开源许可证,如 MIT 许可证。这是一个非常通用的模板,适用于需要提供广泛权限的项目。

模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MIT License

Copyright (c) [year] [full name]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MIT License

Copyright (c) 2024 Mao Ye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

README.md

README.md 文件是项目的主要文档,通常包含安装指南、使用示例以及开发和贡献指南。以下是README.md 模板和相应的示例,专为 Python 库项目设计。

模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Project Name

One or two paragraphs providing an overview of what this project does.

## Installation

Instructions on how to get the development environment running.

```bash
pip install your_package_name
```

## Usage

A quick example to show the basic usage:

```python
import your_package_name

result = your_package_name.some_function()
print(result)
```

## Features

List of features or a brief code walkthrough:

- Feature 1
- Feature 2
- Feature 3

## Contributing

State if you are open to contributions and what your requirements are for accepting them.

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## License

Distributed under the MIT License. See `LICENSE` for more information.

## Contact

Your Name - [@your_twitter](https://twitter.com/your_twitter) - email@example.com

Project Link: [https://github.com/yourusername/your_project_name](https://github.com/yourusername/your_project_name)
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# CoulombCalc

CoulombCalc is a Python library designed for calculating Coulomb stress changes due to fluid injection in geophysical studies.

## Installation

You can install CoulombCalc using pip:

```bash
pip install coulombcalc
```

## Usage

Here's how you can calculate Coulomb stress using CoulombCalc:

```python
import coulombcalc

stress = coulombcalc.calculate_stress(source_parameters, receiver_fault)
print(f"The calculated stress change is: {stress} bars")
```

## Features

CoulombCalc provides:

- Easy-to-use interface for calculating stress changes.
- Support for various fault and source geometries.
- Integration with commonly used geophysical libraries.

## Contributing

Contributions are welcome, especially from those who are also interested in seismology and geophysics.

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/NewCalculationModel`)
3. Commit your Changes (`git commit -m 'Add some NewCalculationModel'`)
4. Push to the Branch (`git push origin feature/NewCalculationModel`)
5. Open a Pull Request

## License

Distributed under the MIT License. See `LICENSE` for more information.

## Contact

Mao Ye - [@maoye](https://twitter.com/maoye) - maoye@example.com

Project Link: [https://github.com/maoye/coulombcalc](https://github.com/maoye/coulombcalc)

这个模板和示例涵盖了基本介绍、安装指南、用法示例、特色功能、贡献指南、许可证信息以及联系方式,是一个全面的项目文档开始点。

pyproject.toml

pyproject.toml 文件是替代传统setup.py文件的现代方法,用于包含包元数据和安装依赖,与 setup.cfg 配合使用。以下是pyproject.toml 示例。

1
2
3
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

setup.cfg

setup.cfg 文件包含包元数据和安装依赖的配置文件。以下是setup.cfg 模板和示例。

模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[metadata]
name = package_name
version = 0.1.0
author = Your Name
author_email = your.email@example.com
description = A short description of your package
long_description = file: README.md
long_description_content_type = text/markdown
url = http://github.com/yourusername/your_package_name
license = License Name

[options]
packages = find:
python_requires = >=3.6
install_requires =
dependency1
dependency2
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[metadata]
name = mypackage
version = 0.1.0
author = Mao Ye
author_email = john.doe@example.com
description = A package for calculating Coulomb stress changes
long_description = file: README.md
long_description_content_type = text/markdown
url = http://github.com/maoye/mypackage
license = MIT

[options]
packages = find:
python_requires = >=3.6
install_requires =
numpy
scipy

meta.yaml

meta.yaml 文件是 Conda 配方文件,告诉 Conda 如何构建和打包库。以下是meta.yaml 模板及示例。

模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package:
name: "{{ package_name }}"
version: "{{ version }}"

source:
path: .

build:
number: 0
script: "{{ PYTHON }} -m pip install . --no-deps --ignore-installed --no-cache-dir"

requirements:
host:
- python
- setuptools
run:
- python

about:
home: "{{ url }}"
license: "{{ license }}"
summary: "{{ summary_description }}"

extra:
recipe-maintainers:
- your_username
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package:
name: "mypackage"
version: "0.1.0"

source:
path: .

build:
number: 0
script: "{{ PYTHON }} -m pip install . --no-deps --ignore-installed --no-cache-dir"

requirements:
host:
- python >=3.6
- setuptools
run:
- python >=3.6

about:
home: "http://github.com/maoye/mypackage"
license: "MIT"
summary: "A package for calculating Coulomb stress changes"

extra:
recipe-maintainers:
- maoye

构建包

mypackage/目录下,运行以下命令使用 Conda 构建系统打包你的项目。

1
conda build .

这将在/Anaconda3/conda-bld/win-64目录生成.tar.bz2文件。

上传包

登录到 Anaconda Cloud

如果没有 anaconda.org 的账号,请先注册一个。随后在命令行中输入以下命令登录到 Anaconda :

1
anaconda login

上传包

1
anaconda upload /path/to/your/conda/package.tar.bz2

确保替换/path/to/your/conda/package.tar.bz2为你的包路径。如果不想每次都这样上传,也可以设置构建完成后自动上传:

1
conda config --set anaconda_upload yes

如果你想临时禁用自动上传,可以在构建命令中添加 --no-anaconda-upload 选项:

1
conda build --no-anaconda-upload

管理包

列出包

1
anaconda package --user your_username

删除包

删除所有版本:

1
anaconda remove your_username/package_name

删除特定版本:

1
anaconda remove your_username/package_name/version

更新包

重新构建你的包并上传新版本。

1
2
conda build .
anaconda upload /path/to/your/conda/package.tar.bz2

希望这个指南可以帮助你成功地将你的 Python 包上传到 Conda,并进行有效的日常管理。记得在上传任何更新之前都要详细测试你的包以确保它的稳定性和可靠性。