You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/1-installable-code.md
+16-24Lines changed: 16 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -94,16 +94,15 @@ Notice a few things about the above layout:
94
94
95
95
1. Your package code lives within a `src/packagename` directory. We suggest that you use `src` (short for **source code**) directory as it [ensures that you are running tests on the installed version of your code](https://www.pyopensci.org/python-package-guide/package-structure-code/python-package-structure.html#the-src-layout-and-testing).
96
96
1. Within the `src` directory you have a package directory called `pyospackage`. Use the name of your package for that directory name. This will be the name for importing your package in Python code once installed.
97
-
1. In your package directory, you have an `__init__.py` file and all of your Python modules. You will learn more about the __init__.py file below.
97
+
1. In your package directory, you have an `__init__.py` file and all of your Python modules. You will learn more about the `__init__.py` file below.
98
98
1. The `pyproject.toml` file lives at the root directory of your package.
99
99
1. The name of the root directory for the package is **pyospackage** which is the name of the package. This is not a requirement but you will often see that the GitHub / GitLab repo and the root directory name are the same as the package name.
100
100
101
101
### What is an `__init__.py` file?
102
102
103
103
The `__init__.py` file tells Python that a directory
104
104
should be treated as a Python package. As such, a directory with an `__init__.py` file can be imported
105
-
directly into Python. The __init__.py file does not need
106
-
to contain any code in order for Python to recognize it; it can be empty.
105
+
directly into Python. The `__init__.py` file does not need to contain any code in order for Python to recognize it; it can be empty.
107
106
108
107
For example, following the file structure example above which has an `__init__.py` file within it, you can run:
109
108
@@ -146,18 +145,16 @@ Neither 'setup.py' nor 'pyproject.toml' found.
146
145
147
146
## Time to create your Python package!
148
147
149
-
Now that you understand the basics of the Python package directory
150
-
structure, and associated key files (`__init__.py` and `pyproject.toml`),
151
-
it's time to create your Python package! Below you will create a directory
152
-
structure similar to the structure described above.
148
+
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!
149
+
Below you will create a directory structure similar to the structure described above.
153
150
154
151
If you don’t wish to create each of the files and directories below, you
155
152
can always [fork and clone and customize the pyOpenSci example package.](https://github.com/pyOpenSci/pyosPackage)
156
153
157
154
## Step 1: Set Up the Package Directory Structure
158
155
159
-
Below you create the basic directory structure required
160
-
for your Python package. Note that there are instructions for creating the files and directories using shell. However you can also create files and directories in your preferred file directory tool (e.g. Finder on MAC or File Explorer on Windows or even a tool such as VS Code or Spyder) if you wish.
156
+
Below you create the basic directory structure required for your Python package.
157
+
Note that there are instructions for creating the files and directories using shell. However you can also create files and directories in your preferred file directory tool (e.g. Finder on macOS or File Explorer on Windows or even a tool such as VS Code or Spyder) if you wish.
161
158
162
159
### Create your package's project directory structure
163
160
* Create a new project directory for your package. Choose a name for your package, preferably in lowercase and without spaces. For this tutorial we'll use `pyospackage`.
@@ -203,21 +200,17 @@ pyospackage/ # This is your project directory
203
200
204
201
## Step 2: Add code to your package
205
202
206
-
Within the `pyospackage` subdirectory, add 1 or more Python modules
207
-
(.py files) containing the code that you want your package to access and run.
208
-
If you don't have code already and are just learning how to
209
-
create a Python
210
-
package, then create an empty `add_numbers.py` file.
203
+
Within the `pyospackage` subdirectory, add one (1) or more Python modules (.py files) containing the code that you want your package to access and run.
204
+
If you don't have code already and are just learning how to create a Python package, then create an empty `add_numbers.py` file.
211
205
212
206
:::{admonition} Python modules and the `__init__.py` file
213
207
:class: tip
214
208
215
209
When you see the word module, we are referring to a `.py` file containing Python
216
210
code.
217
211
218
-
The `__init__.py` allows Python to recognize that a directory contains at least one
219
-
module that may be imported and used in your code. A package can have multiple
220
-
modules.
212
+
The `__init__.py` allows Python to recognize that a directory contains at least one (1) module that may be imported and used in your code.
213
+
A package can have multiple modules.
221
214
222
215
[Learn more about Python packages and modules in the Python documentation.](https://docs.python.org/3/tutorial/modules.html#packages)
223
216
@@ -238,12 +231,12 @@ pyospackage/
238
231
239
232
If you are following along and making a Python package from scratch then you can add the code below to your `add_numbers.py` module. The function below adds two integers together and returns the result. Notice that the code below has a few features that we will review in future tutorials:
240
233
241
-
1. It has a [numpy-style docstring](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html#three-python-docstring-formats-and-why-we-like-numpy-style)
234
+
1. It has a [numpy-style docstring](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html#three-python-docstring-formats-and-why-we-like-numpy-style)
242
235
2. It uses [typing](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html#adding-type-hints-to-your-docstrings)
243
236
244
-
If you aren’t familiar with docstrings or typing yet, that is ok. We will get
245
-
to it later in our tutorial series. Or, you can review the pyOpenSci [packaging guide](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html)
246
-
for an overview.
237
+
Python can support many different docstrings formats depending on the documentation build system you wish to use. The most popular supported formats for documenting Python objects are [NumPy Style Docstring](https://numpydoc.readthedocs.io/en/latest/format.html), [Google Style Docstring](https://google.github.io/styleguide/pyguide.html), [ReStructuredText Style Docstring](https://peps.python.org/pep-0287/), and the [Epytext Style Docstrings](https://epydoc.sourceforge.net/epytext.html). **pyOpensci recommends using the NumPy Docstring convention.**
238
+
239
+
If you aren’t familiar with docstrings or typing yet, that is ok. We will get to it later in our tutorial series. Or, you can review the pyOpenSci [packaging guide](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html)for an overview.
247
240
248
241
```python
249
242
defadd_num(a: int, b: int) -> int:
@@ -284,9 +277,8 @@ are welcome to copy the file we have in our [example pyospackage GitHub reposito
284
277
285
278
`[this-is-a-table]`.
286
279
287
-
Tables can contain variables within them defined by an variable name and
288
-
an `=` sign. For
289
-
instance, a `build-system` table most often holds 2 variables:
280
+
Tables can contain variables within them defined by an variable name and an `=` sign.
281
+
For instance, a `build-system` table most often holds two (2) variables:
290
282
291
283
1.`requires = `, which tells a build tool what tools it needs to install prior to building your package. In this case
0 commit comments