A simple "Hello, World!" package
Project description
Status | Proposed |
---|---|
Category | Library |
Team | Developer |
Authors(s) | eazycloudlife |
Created By | Jul 2024 |
Modified By | Jul 2024 |
What is this repository for?
- This is the
ezcl hello python library
repository.
Creating a "Hello, World!"
package in Python is a great way to learn the basics of packaging and distributing Python libraries. Here’s a step-by-step guide to create a simple "Hello, World!"
package:
index
Step-by-Step Guide
1. Set Up Your Project Directory
Create a new directory for your project. Let's name it ezcl-py-hello
.
mkdir ezcl-py-hello
cd ezcl-py-hello
2. Create the Package Structure
Inside your ezcl-py-hello
directory, create the following structure:
ezcl-py-hello/
└── hello/
├── __init__.py
└── world.py
└── test/
├── __init__.py
└── test_hello.py.py
├── .gitignore
├── LICENSE
├── README.md
├── setup.py
├── requirements.txt
├── run.py
├── setup.py
hello/__init__.py
: This file will initialize your package.hello/world.py
: This file will contain your "Hello, World!" function.
3. Implement Your "Hello, World!" Function
Open hello/world.py
and write the following Python code:
def hello(name):
"""
In return, Hello, name!.
"""
return f"Hello, {name}!"
def hello_world():
"""
In return, Hello, World!.
"""
return "Hello, World!"
def hi(name):
"""
In return, Hello, name!.
"""
return f"Hi, {name}"
def hi_world():
"""
In return, Hi, World!.
"""
return "Hi, World!"
4. Create init.py
In hello/__init__.py
, include the following:
from .world import hello_world
This imports the hello_world function from world.py into your package.
5. Write setup.py
Create setup.py in your project root directory (hello_world_package) with the following content:
from setuptools import setup, find_packages
setup(
name='ezcl-py-hello',
version='1.0.0',
packages=find_packages(),
install_requires=[], # No dependencies for this simple package
entry_points={
'console_scripts': [
'hello-world = hello.world:hello_world'
]
},
author='Eazy Cloud Life',
author_email='wazycloudlife@email.com',
description='A simple "Hello, World!" package',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='https://github.com/eazycloudlife/ezcl-py-hello',
license="MIT",
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
extras_require={},
python_requires=">=3.12.4",
)
Replace Your Name
, your@email.com
, and https://github.com/yourusername/hello_world_package
with your information.
6. Write README.md
Create README.md
and write a brief description of your package.
7. Install Your Package Locally
To test
your package locally, run the following command in the hello_world_package directory:
pip install .
This will install your package in the current Python environment.
8. Test Your Package
Create a Python script to test your package. For example, create test_hello.py
:
from hello import (
hello,
hello_world,
hi,
hi_world
)
def test_hello(name):
assert hello(name) == f"Hello, {name}!"
print("Test passed!")
def test_hello_world():
assert hello_world() == "Hello, World!"
print("Test passed!")
def test_hi(name):
assert hi(name) == f"Hi, {name}"
print("Test passed!")
def test_hi_world():
assert hi_world() == "Hi, World!"
print("Test passed!")
if __name__ == "__main__":
name = "Eazy Cloud Life"
test_hello(name)
test_hello_world()
test_hi(name)
test_hi_world()
Run python test_hello.py
to verify that your package works correctly.
9. Document Your Library
Use docstrings ("""...""") to document each function, class, and module. Consider generating documentation using tools like Sphinx.
10. Publish Your Package (Optional)
If you want to share your package with others, consider publishing it on PyPI
. You will need to create an account on PyPI
and follow their guidelines for uploading packages.
-
Package your library using setuptools:
python setup.py sdist bdist_wheel
-
Create the PyPI token to Upload your package to PyPI.
- Log in to PyPI:
- Go to PyPI and log in with your account.
- Create a Token:
- Click on your
username
in the top right corner and select"Account settings."
- Scroll down to the
"API tokens"
section and click on"Add API token."
- Give your token a name (e.g., "my-package-upload-token").
- Choose the
scope
of the token. You can either create a token with"Entire account"
scope or "Project:your-project-name
" scope. For most cases, it's better to use the project scope for security reasons. - Click
"Add token."
- Click Copy button to Copy token.
- Click on your
- Log in to PyPI:
-
Upload your package to PyPI or another package repository:
twine upload dist/*
-
Once you've passed the token, it will now appear when you press Enter.
-
-
Go to PyPI and check you
projects / package
.
Summary
Creating a "Hello, World!"
package involves defining a simple function, organizing your code into a package structure, writing a setup.py
file for packaging, and optionally testing and publishing your package. This exercise provides a solid foundation for understanding Python packaging and distribution.
Who do I talk to?
Authors
A repository is maintained by eazycloudlife with help from these awesome contributors. ⬆ Back to index
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ezcl_py_hello-4.0.0.tar.gz
.
File metadata
- Download URL: ezcl_py_hello-4.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
8c1990a497f81f22cbcecfbcccf1ba2ebfa997b1014c9af0071facc0095b62fb
|
|
MD5 |
3a3896251d808d334d4cb4a099088a2a
|
|
BLAKE2b-256 |
7eb7fffd5ffd8b3989d9465e432cf7cee15705d28b0afc933dc000fc1699f1b8
|
File details
Details for the file ezcl_py_hello-4.0.0-py3-none-any.whl
.
File metadata
- Download URL: ezcl_py_hello-4.0.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
90e5919689f8a4c648aeb089544863ce23a9632805061101a5b105ffb2be366f
|
|
MD5 |
e4021fda2c7972ad2153ced16d72791c
|
|
BLAKE2b-256 |
34df4e6de0915a1968440889c42b96de19b6a58eede6c2ee00082869dbf6acef
|