|
5 | 5 | 1. Create a project |
6 | 6 | 2. Make it a package |
7 | 7 | 3. Add a feature |
8 | | -4. Include a setup |
| 8 | +4. Include a package definition file |
9 | 9 | 5. Use the package |
10 | 10 |
|
11 | 11 | ## Create a project |
@@ -74,40 +74,67 @@ In this special environment, it should work. |
74 | 74 |
|
75 | 75 | ## Include a pyproject.toml |
76 | 76 |
|
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/"] |
85 | 114 | ``` |
86 | 115 |
|
| 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 | + |
87 | 118 | ## Use the package |
88 | 119 |
|
89 | 120 | Now let's install the package. Open a terminal / shell. Activate the virtual environment in the project folder: |
90 | 121 |
|
91 | | -``` |
| 122 | +```bash |
92 | 123 | # mac / linux |
93 | | -. .env/bin/activate |
| 124 | +. .venv/bin/activate |
94 | 125 | ``` |
95 | 126 |
|
| 127 | +```bash |
| 128 | +# Windows |
| 129 | +.venv/scripts/activate |
96 | 130 | ``` |
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): |
102 | 131 |
|
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) |
106 | 133 |
|
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: |
108 | 135 |
|
109 | | -``` |
110 | | -$ python3 |
| 136 | +```python |
| 137 | +$ (venv) python |
111 | 138 | import calcy |
112 | 139 | calcy.math.add(7, 5) |
113 | 140 | 12 |
|
0 commit comments