Skip to content

Commit 0b18289

Browse files
authored
Merge branch 'main' into patch-4
2 parents 85512eb + 37a8d5e commit 0b18289

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

tutorials/get-to-know-hatch.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ and maintaining your Python package easier.
134134

135135
A few features that Hatch offers
136136

137-
1. it will convert metadata stored in a `setup.py` or `setup.cfg` file to a `pyproject.toml` file for you. While we have not extensively tested this feature yet, please let us know if you try it!
137+
138+
1. it will convert metadata stored in a `setup.py` or `setup.cfg` file to a pyproject.toml file for you (see [Migrating setup.py to pyproject.toml using Hatch](setup-py-to-pyproject-toml.md
139+
))
138140
2. It will help you by storing configuration information for publishing to PyPI after you've entered it once.
139141

140142
Use `hatch -h` to see all of the available commands.

tutorials/installable-code.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ Neither 'setup.py' nor 'pyproject.toml' found.
147147

148148
:::
149149

150+
:::{admonition} Note about `setup.py`
151+
:class: tip
152+
153+
If your project already defines a `setup.py` file, Hatch can be used to automatically create the `pyproject.toml`.
154+
* See [Using Hatch to Migrate setup.py to a pyproject.toml
155+
](setup-py-to-pyproject-toml.md)
156+
157+
:::
158+
159+
160+
150161
## Time to create your Python package!
151162

152163
Now that you understand the basics of the Python package directory structure, and associated key files (`__init__.py` and `pyproject.toml`), it's time to create your Python package!

tutorials/intro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ understanding the steps involved in creating a Python package.
2929
:caption: Python Packaging Tutorial Setup
3030

3131
Get to know Hatch <get-to-know-hatch>
32+
Migrate setup.py to a pyproject.toml using Hatch <setup-py-to-pyproject-toml>
3233
:::
3334

3435
:::{toctree}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Using Hatch to Migrate setup.py to a pyproject.toml
2+
3+
Hatch can be particularly useful to generate your project's `pyproject.toml` if your project already has a `setup.py`.
4+
5+
:::{admonition} Note
6+
:class: tip
7+
8+
This step is not necessary and is only useful if your project already has a `setup.py` file defined.
9+
* If your project does not already define a `setup.py` see [Make your Python code installable](installable-code.md)
10+
:::
11+
12+
:::{admonition} Learning Objectives
13+
:class: tip
14+
15+
In this lesson you will learn:
16+
17+
1. The process of using Hatch to transition to using `pyproject.toml` for projects that already have a `setup.py` defined.
18+
:::
19+
20+
## What is Hatch?
21+
22+
Hatch is a Python package manager designed to streamline the process of creating, managing, and distributing Python packages. It provides a convenient CLI (Command-Line Interface) for tasks such as creating new projects, managing dependencies, building distributions, and publishing packages to repositories like PyPI.
23+
24+
:::{admonition} Get to know Hatch
25+
:class: tip
26+
27+
See [Get to know Hatch](get-to-know-hatch.md) for more information.
28+
29+
:::
30+
31+
## Prerequisites
32+
33+
Before we begin, ensure that you have Hatch installed on your system. You can install it via pip:
34+
35+
```bash
36+
pipx install hatch
37+
```
38+
39+
## Sample Directory Tree
40+
41+
Let's take a look at a sample directory tree structure before and after using `hatch init`:
42+
43+
### Before `hatch init`
44+
45+
```
46+
project/
47+
48+
├── src/
49+
│ └── my_package/
50+
│ ├── __init__.py
51+
│ └── module.py
52+
53+
├── tests/
54+
│ └── test_module.py
55+
56+
└── setup.py
57+
```
58+
59+
### After `hatch init`
60+
61+
```
62+
project/
63+
64+
├── pyproject.toml
65+
66+
├── src/
67+
│ └── my_package/
68+
│ ├── __init__.py
69+
│ └── module.py
70+
71+
├── tests/
72+
│ └── test_module.py
73+
74+
└── setup.py
75+
```
76+
77+
As you can see, the main change after running `hatch init` is the addition of the `pyproject.toml` file in the project directory.
78+
79+
## Step-by-Step Guide
80+
81+
Now, let's walk through the steps to use Hatch to create a `pyproject.toml` file for your project.
82+
83+
1. **Navigate to Your Project Directory**: Open your terminal or command prompt and navigate to the directory where your Python project is located.
84+
85+
2. **Initialize Hatch**: Run the following command to initialize Hatch in your project directory:
86+
87+
```bash
88+
hatch new --init
89+
```
90+
91+
3. **Review and Customize**: After running the previous command, Hatch will automatically generate a `pyproject.toml` file based on your existing project configuration. Take some time to review the contents of the generated `pyproject.toml` file. You may want to customize certain settings or dependencies based on your project's requirements (see [pyproject.toml tutorial](pyproject-toml.md) for more information about the `pyproject.toml`).
92+
93+
4. **Verify**: Verify that the `pyproject.toml` file accurately reflects your project configuration and dependencies. You can manually edit the file if needed, but be cautious and ensure that the syntax is correct.
94+
95+
5. **Delete setup.py**: Since we're migrating to using `pyproject.toml` exclusively, the `setup.py` file becomes unnecessary. You can safely delete it from your project directory.
96+
97+
6. **Test Build**: Before proceeding further, it's essential to ensure that your project builds successfully using only the `pyproject.toml` file. Run the following command to build your project:
98+
99+
```bash
100+
hatch build
101+
```
102+
103+
This command will build your project based on the specifications in the `pyproject.toml` file. Make sure to check for any errors or warnings during the build process.
104+
105+
7. **Test Existing Functionality**: After successfully building your project with `pyproject.toml`, it's crucial to ensure that your project's existing functionality remains intact. Run any pre-existing tests to verify that everything still works as expected.

0 commit comments

Comments
 (0)