Skip to content

Commit 040b232

Browse files
committed
Your turn updated for packaging chapter.
1 parent 3cd8881 commit 040b232

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

your-turn/11-python-packages/readme.md

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
1. Create a project
66
2. Make it a package
77
3. Add a feature
8-
4. Include a setup
8+
4. Include a package definition file
99
5. Use the package
1010

1111
## Create a project
@@ -74,40 +74,67 @@ In this special environment, it should work.
7474

7575
## Include a pyproject.toml
7676

77-
It's great you can run your code locally. But for real packages, you'll need to be able to install it for consumers. To do this, we need a `setup.py`.
78-
79-
Luckily, PyCharm has our back. First select the top-level folder in the project (the one which contains the `calcy` folder with `__init__.py`. Choose `Tools > Create setup.py` and fill in the dialog. Most values are somewhat irrelevant. But name the package `calcy`.
80-
81-
PyCharm may go overboard and include a bunch of stuff in the virtual environment. Just cut that out but leave:
82-
83-
```python
84-
packages=['calcy']
77+
It's great you can run your code locally. But for real packages, you'll need to be able to install it for consumers. To do this, we need a `pyproject.toml` file.
78+
79+
Luckily, we can easily add one. While, at the time of this writing, PyCharm doesn't have support to generate one, they are pretty straightforward. Here is a template you can use.
80+
81+
```toml
82+
[build-system]
83+
requires = ["hatchling"]
84+
build-backend = "hatchling.build"
85+
86+
[project]
87+
name = "calcy"
88+
version = "0.0.1"
89+
description = "A LIBRARY DESCRIPTION"
90+
# readme = "readme.md"
91+
license = "MIT"
92+
authors = [
93+
{ name = "YOUR NAME", email = "[email protected]" }
94+
]
95+
classifiers = [
96+
"Programming Language :: Python :: 3",
97+
"License :: OSI Approved :: MIT License",
98+
"Operating System :: OS Independent"
99+
]
100+
keywords = ["calcy", "math", "cacluator"]
101+
dependencies = []
102+
103+
[project.urls]
104+
Homepage = "https://github.com/YOURNAME/calcy"
105+
106+
[tool.hatch.version]
107+
path = "calcy/__init__.py"
108+
109+
[tool.hatch.build.targets.sdist]
110+
include = ["calcy/", "readme.md"]
111+
112+
[tool.hatch.build.targets.wheel]
113+
include = ["calcy/"]
85114
```
86115

116+
1. Create a new file in the top-level folder and name it `pyproject.toml`. Then copy the contents above into it and adjust as needed (e.g. your name).
117+
87118
## Use the package
88119

89120
Now let's install the package. Open a terminal / shell. Activate the virtual environment in the project folder:
90121

91-
```
122+
```bash
92123
# mac / linux
93-
. .env/bin/activate
124+
. .venv/bin/activate
94125
```
95126

127+
```bash
128+
# Windows
129+
.venv/scripts/activate
96130
```
97-
Windows
98-
.env/scripts/activate
99-
```
100-
101-
Now run `setup.py` **from the same directory** as it is in as follows. The easiest way to do this is with Python's builtin terminal (make sure the env is activated):
102131

103-
```
104-
python3 setup.py develop
105-
```
132+
Now run `pip install -e .` **from the same directory** as it is in as follows. The easiest way to do this is with Python's builtin terminal (make sure the env is activated). If you have used uv, then you'll need `uv pip install -e .` (yes, the dot is part of the command, it means the current working directory)
106133

107-
Now, in the terminal, change out of that folder to somewhere else. Run Python 3 and try to import and use it:
134+
Now, in the terminal, change out of that folder to somewhere else. Run Python and try to import and use it:
108135

109-
```
110-
$ python3
136+
```python
137+
$ (venv) python
111138
import calcy
112139
calcy.math.add(7, 5)
113140
12

0 commit comments

Comments
 (0)