Advertisement
efxtv

How to Create and Publish a Simple Hello World Python Package to PyPI

May 19th, 2025 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | Cybersecurity | 0 0
  1. ##################################################################
  2. How to Create and Publish a Simple Hello World Python Package to PyPI (helloworldfx)
  3. To install the package directly (pypi):
  4. pip install hellowordlfx
  5. ##################################################################
  6. Join our telegram channel for more : https://t.me/LinuxClassesEFXTv
  7. ##################################################################
  8.  
  9. ## 1. Bash Script to Create the Project Structure
  10. helloworldfx/
  11. ├── helloworldfx/
  12. │ ├── __init__.py
  13. │ ├── __main__.py
  14. │ └── app.py
  15. ├── setup.py
  16. └── README.md
  17.  
  18. ################################################################## BashFileStarts
  19. #!/bin/bash
  20.  
  21. PROJECT_NAME="helloworldfx"
  22. mkdir -p $PROJECT_NAME/$PROJECT_NAME/templates
  23. cd $PROJECT_NAME || exit
  24.  
  25. # Create __init__.py with version
  26. echo '__version__ = "0.1.0"' > $PROJECT_NAME/__init__.py
  27.  
  28. # Create __main__.py to allow python -m helloworldfx
  29. cat << EOF > $PROJECT_NAME/__main__.py
  30. from .app import main
  31.  
  32. if __name__ == "__main__":
  33. main()
  34. EOF
  35.  
  36. # Create app.py with Hello World Flask app
  37. cat << EOF > $PROJECT_NAME/app.py
  38. #!/usr/bin/env python3
  39. from flask import Flask
  40.  
  41. app = Flask(__name__)
  42.  
  43. @app.route("/")
  44. def hello():
  45. return "Hello, World!"
  46.  
  47. def main():
  48. app.run(debug=True)
  49.  
  50. if __name__ == "__main__":
  51. main()
  52. EOF
  53.  
  54. # Create setup.py
  55. cat << EOF > setup.py
  56. from setuptools import setup, find_packages
  57.  
  58. setup(
  59. name="$PROJECT_NAME",
  60. version="0.1.0",
  61. packages=find_packages(),
  62. install_requires=["Flask"],
  63. entry_points={
  64. "console_scripts": [
  65. "$PROJECT_NAME = $PROJECT_NAME.app:main",
  66. ],
  67. },
  68. include_package_data=True,
  69. )
  70. EOF
  71.  
  72. # Create README.md
  73. echo "# $PROJECT_NAME" > README.md
  74.  
  75. # Create LICENSE (MIT license example)
  76. cat << EOF > LICENSE
  77. MIT License
  78.  
  79. Copyright (c) $(date +%Y) Your Name
  80.  
  81. Permission is hereby granted, free of charge, to any person obtaining a copy
  82. ...
  83. EOF
  84.  
  85. ################################################################## BashFileEnds
  86. $ bash create_helloworldfx.sh
  87.  
  88. ## 2. Create your PyPI API Token and .pypirc File
  89. Generate PyPI API Token:
  90. 1. Log into https://pypi.org/manage/account/token/
  91. 2. Create a new token scoped to your project (helloworldfx) or global access.
  92. 3. Copy the generated token string.
  93.  
  94. Create .pypirc file in your home directory for secure authentication:
  95. nano ~/.pypirc
  96. [distutils]
  97. index-servers =
  98. pypi
  99.  
  100. [pypi]
  101. username = __token__
  102. password = <YOUR_PYPI_TOKEN>
  103.  
  104. chmod 600 ~/.pypirc
  105.  
  106. 3. Build and Upload Your Package #
  107. Note: If you wish to push v2 you need to change the project version in setup.py. Example: present version is (version="0.1.0",) new update will have (version="0.1.1",).
  108.  
  109. Make sure you are in your project root (where setup.py is):
  110. python3 -m pip install --upgrade build twine
  111. python3 -m build
  112. python3 -m twine upload dist/*
  113.  
  114. # Update update version 0.1.1
  115. nano setup.py #change version
  116. rm -rf dist build linkgenfx.egg-info
  117. python3 -m pip install --upgrade build twine
  118. python3 -m build
  119. python3 -m twine upload dist/*
  120.  
  121. 4. Test Your Package
  122. pip3 install helloworldfx
  123. helloworldfx
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement