Skip to content

Commit 0fa07d2

Browse files
kierisilwasser
authored andcommitted
pre-formatting
1 parent aac1ed0 commit 0fa07d2

File tree

1 file changed

+146
-7
lines changed

1 file changed

+146
-7
lines changed

_posts/2024-06-25-create-your-first-python-package-scipy-2024.md

Lines changed: 146 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,154 @@ classes: wide
1515
toc: true
1616
comments: true
1717
---
18-
## <i class="fa-regular fa-handshake"></i> See you at SciPy!
19-
Event title
20-
Event location (Conference link, room number, remote options)
21-
Event date
22-
Event time and time zone(s)
18+
## Create Your First Python Package: Make Your Python Code Easier to Share and Use
2319

20+
* **What:** A hands-on workshop, titled: [Create Your First Python Package: Make Your Python Code Easier to Share and Use](https://cfp.scipy.org/2024/talk/QT9GBY/)
21+
* **Where:** [SciPy 2024](https://www.scipy2024.scipy.org/), Room 316
22+
* **When:** Tuesday, July 9th, 2024, from 13:30--17:30 Pacific
2423

24+
## Event overview
25+
26+
Creating code that can be shared and reused is the gold-standard of open science. But the tools and skills to share your code can be tricky to learn. In this hands-on tutorial, you’ll learn how to turn your code into an installable Python module (a file containing Python code that defines functions, classes, and variables), that can be shared with others. We will provide pre-built code and module examples for each step of the tutorial to help make it more beginner-friendly, but you will need some basic Python skills to get the most out of this session.
27+
28+
You will leave this tutorial understanding how to:
29+
* Create code that can be installed into different environments
30+
* Use [Hatch](https://hatch.pypa.io/latest/) as a workflow tool, making setup and installation of your code easier
31+
* Use Hatch to publish your package to the test version of [PyPI](https://pypi.org/)
32+
33+
## What you need to know
34+
* Basic Python programming
35+
* How to write functions in Python
36+
* How to use a Python environment manager of your choosing
37+
38+
## What you need installed
39+
* Python
40+
* An environment manager
41+
* Hatch
42+
43+
### Install Python using `conda` and the `conda-forge` channel
44+
*These instructions were adapted from the [Install Python Using Conda & Conda-forge - Mambaforge post](https://datascienceskills.org/install-python-science-conda/) from Leah Wasser and Jenny Palomino, originally posted on [datascienceskills.org](http://datascienceskills.org).*
45+
46+
#### For Mac users
47+
*For homebrew users*
48+
* Find directions for installing homebrew here.
49+
* If you have homebrew installed, then the easiest way to install mambaforge is to use: `brew install mambaforge`
50+
51+
*If you don’t have homebrew*
52+
* Download a mamba installer and use bash to install mambaforge:
53+
* Download the installer: [Mambaforge installer for Mac](https://github.com/conda-forge/miniforge#mambaforge).
54+
* Note that if you have a newer mac with a m1 or m2 chip, then you will want to install the Apple Silicon version: OS X arm64 (Apple Silicon) Mambaforge-MacOSX-arm64. If you have an older mac use: OS X x86_64 Mambaforge-MacOSX-x86_64
55+
* In your Terminal window, cd to the location of the download file. Run: `bash Mambaforge3-latest-MacOSX-modify-filename-here.sh`
56+
* Follow the prompts on the installer screens.
57+
* If you are unsure about any setting, accept the defaults. You can change them later.
58+
* To make sure that the changes take effect, close and then re-open your Terminal window.
59+
60+
#### For Linux users
61+
* Download the installer: [Mambaforge installer for Linux](https://github.com/conda-forge/miniforge#mambaforge).
62+
In your Terminal window, run making sure to modify the file name to match the file that you downloaded: `bash Mambaforge3-latest-Linux-modify-file-name-here.sh`
63+
* Follow the prompts on the installer screens.
64+
* If you are unsure about any setting, accept the defaults. You can change them later.
65+
* To make sure that the changes take effect, close and then re-open your Terminal window.
66+
67+
#### For Windows users
68+
Download the [Mambaforge installer for Windows](https://github.com/conda-forge/miniforge#mambaforge) and run the installer by double-clicking on the downloaded file. Then follow the steps below:
69+
* Click “Run”.
70+
* Click on “Next”.
71+
* Click on “I agree”.
72+
* Leave the selection on “Just me” and click on “Next”.
73+
* Click on “Next”.
74+
* **Select the first option for “Add Anaconda to my PATH environment variable”** and also **leave the selection on “Register Anaconda as my default Python 3.x”**. Click on “Install”.
75+
* Note that even though the installation is for Mambaforge, the installer uses the word Anaconda in these options.
76+
* You will also see a message in red text that selecting “Add Anaconda to my PATH environment variable” is not recommended; continue with this selection to make using conda easier in Git Bash. If you have questions or concerns, please contact your instructor.
77+
* When the install is complete, Click on “Next”.
78+
* Click on “Finish”.
79+
80+
#### Test that the installation was successful (all systems)
81+
* Search for and open the Terminal program (found in /Applications/Utilities). In this `Terminal` window, type `bash` and hit enter. If you do not get a message back, then `Bash` is available for use.
82+
* Next, type `git` and hit enter. If you see a list of commands that you can execute, then `Git` has been installed correctly.
83+
* Next, type `conda` and hit enter. Again, if you see a list of commands that you can execute, then Mambaforge `Python` has been installed correctly.
84+
* Close the `Terminal` by typing `exit`.
85+
86+
### Install Hatch
87+
*These instructions were adapted from the [Introduction to Hatch](https://www.pyopensci.org/python-package-guide/tutorials/get-to-know-hatch.html) section of the [pyOpenSci Python Packaging Guide](https://www.pyopensci.org/python-package-guide/).*
88+
89+
#### For Mac users
90+
*These instructions are for installing Hatch using the GUI installer. If you’d prefer to use the Command line installer, please see the [Hatch documentation](https://hatch.pypa.io/latest/install/.#command-line-installer).*
91+
1. In your browser, download the `.pkg` file: [hatch-universal.pkg](https://github.com/pypa/hatch/releases/latest/download/hatch-universal.pkg)
92+
2. Run your downloaded file and follow the on-screen instructions.
93+
3. Restart your terminal.
94+
4. To verify that the shell can find and run the hatch command in your `PATH`, run `hatch --version` in your Terminal.
95+
96+
#### For Linux users
97+
*Note: if you prefer to use a tool other than pipx, please refer to the [Hatch documentation](https://hatch.pypa.io/latest/) for more information.*
98+
* Install hatch from the command line using pipx: `pip install hatch`
99+
100+
#### For Windows users
101+
*These instructions are for installing Hatch using the GUI installer. If you’d prefer to use the Command line installer, please see the [Hatch documentation](https://hatch.pypa.io/latest/install/#command-line-installer_1).*
102+
1. In your browser, download the `.msi` file: [hatch-x64.msi](https://github.com/pypa/hatch/releases/latest/download/hatch-x64.msi).
103+
2. Run your downloaded file and follow the on-screen instructions.
104+
3. Restart your terminal.
105+
4. To verify that the shell can find and run the hatch command in your `PATH`, run `hatch --version` in your Terminal.
106+
107+
### Configure Hatch (all systems)
108+
Once you have installed Hatch, you will want to customize the configuration. Hatch stores your configuration information in a `config.toml` file.
109+
110+
While you can update the `config.toml` file through the command line, it might be easier to look at it and update it in a text editor if you are using it for the first time.
111+
112+
1. Open and edit your `config.toml` file by either:
113+
* Running `hatch config explore` in your shell, which will open up a directory window that will allow you to double click on the file and open it in your favorite text editor.
114+
* Alternatively, you can retrieve the location of the Hatch config file by running `hatch config find` in your shell.
115+
2. Update your email and name
116+
* Once the file is open, update the \[template\] table of the `config.toml` file with your name and email. This information will be used in any `pyproject.toml` metadata files that you create using Hatch.
117+
3. Set tests to `false`
118+
*While tests are important, setting the tests configuration in Hatch to true will create a more complex pyproject.toml file. You won’t need to use this feature in this beginner friendly tutorial series but we will introduce it in later tutorials.*
119+
* Set tests to `false` in the `[template.plugins.default]` table.
120+
4. Close the config file and `run hatch config show`
121+
`hatch config show` will print out the contents of your config.toml file in your shell. Look at the values and ensure that your name and email are set, and also make sure that `tests=false`.
122+
123+
## Helpful links
124+
* Our [example Python package repo, `pyosPackage`](https://github.com/pyOpenSci/pyosPackage), that goes along with pyOpenSci tutorials.
125+
* [Workshop information on the SciPy 2024 website](https://cfp.scipy.org/2024/talk/QT9GBY/).
126+
127+
## Workshop agenda
128+
129+
### Hour One: the structure of an installable module
130+
131+
**Key Takeaways: After this hour you will have an understanding of:**
132+
* the purpose of the __init__.py file, and
133+
* how workflow tools such as Hatch can be useful when making code installable.
134+
135+
**0-15 minutes:** Here we will get to know each other. I’ll also briefly introduce pyOpenSci and the work that we are doing in open science education and training space.
136+
137+
**15-30 minutes:** Interactive discussion: Here, we’ll discuss why shareable code is important. And we’ll explore some best practices for making code easier to work with. I’ll also introduce Hatch as a workflow tool that streamlines tasks.
138+
139+
**30-60 minutes, hands-on:** You will take an existing script and turn it into an installable module. You are welcome to use the provided scripts for this. If you are more comfortable with Python, then you can also bring your own script with you and work on it during the workshop.
140+
141+
142+
### Hour Two: everything you need to know about the `pyproject.toml` file & project metadata.
143+
144+
**0-15 minutes:** Interactive Discussion: Here you’ll learn about the pyproject.toml and how it’s used to document dependencies, and metadata for your project.
145+
146+
**15-30 minutes:** Short Break. Stretch your legs, get a drink.
147+
148+
**30-60 minutes, hands-on:** In the hands-on part of this hour, you will modify your pyproject.toml file with required dependencies needed to run your code. You will also learn how to install your code in interactive or development mode using both pip and Hatch. Interactive mode will allow you to dynamically update your code and test it locally without reinstalling it. Finally, you will take your shiny new Python module for a test drive in your favorite Python environment.
149+
150+
### Hour Three: the power of metadata and instructions for you, your future self & your colleagues
151+
152+
**0-15 minutes:** Interactive discussion: In this part of the tutorial we’ll discuss the power of documentation when sharing code and also for you when you have to update things in the future. **
153+
154+
**15-50 minutes, hands-on:** Here you will [create a README file](https://www.pyopensci.org/python-package-guide/tutorials/add-readme.html) that helps users of your module understand how to install it and how to get started using it. You will also add [docstrings](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html) to your code. See how docstrings are useful as “hints” when coding real time. Optional: if you are speedy, you can also delve into [typing](https://www.pyopensci.org/python-package-guide/documentation/write-user-documentation/document-your-code-api-docstrings.html#adding-type-hints-to-your-docstrings) your code on your own. However, we won’t directly cover typing in this tutorial.
155+
156+
**50-60 minutes:** Break
157+
158+
### Hour Four: publishing and sharing your code
159+
160+
**0-15 minutes:** Interactive discussion: Here we will discuss what it means to “[publish](https://www.pyopensci.org/python-package-guide/tutorials/publish-pypi.html)” code. We will also discuss other important elements such as license files and codes of conduct if you intend to turn your code into a published [package](https://www.pyopensci.org/python-package-guide/tutorials/intro.html).
161+
162+
**15-45 minutes, hands-on:** Publishing to PyPI vs. installing from Github. Those who’d like to follow along interactively can do so here. However, if your brain is tired, sit back and learn how to build your module into a package distribution using Hatch. And then we will give you all of the tools needed to [publish to the test version of PyPI](https://www.pyopensci.org/python-package-guide/tutorials/publish-pypi.html).
163+
164+
**45-60 minutes:** wrap up, questions. Feedback.
25165

26166
## <i class="fa-solid fa-link"></i> Connect with pyOpenSci
27167

28-
Stay up-to-date with all things pyOpenSci by following us on [LinkedIn](https://linkedin.com/company/pyopensci
29-
) and [Fosstodon](https://fosstodon.org/@pyOpenSci), and connect with the broader pyOpenSci community on our [Discourse forum](https://pyopensci.discourse.group/). If you’re interested in our weekly newsletter where we share news, blog posts, and monthly updates, [subscribe on LinkedIn](https://www.bit.ly/pyOSNewsletter).
168+
Stay up-to-date with all things pyOpenSci by following us on [LinkedIn](https://linkedin.com/company/pyopensci) and [Fosstodon](https://fosstodon.org/@pyOpenSci), and connect with the broader pyOpenSci community on our [Discourse forum](https://pyopensci.discourse.group/). If you’re interested in our weekly newsletter where we share news, blog posts, and monthly updates, [subscribe on LinkedIn](https://www.bit.ly/pyOSNewsletter).

0 commit comments

Comments
 (0)