efxtv

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

May 19th, 2025 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 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. PROJECT TYPE 2
  129. #############################################################################
  130.  
  131.  
  132. # Python Package Deployment Guide
  133.  
  134. # 1. Update run.sh with your package name
  135. # Replace 'text2url' with the name of your app/package you want to push to PyPI
  136. # Example: sed -i 's#text2url#myapp#g' run.sh
  137. # Download run.sh https://github.com/efxtv/EFX-Tv-Bookmarks/tree/main/bin/pypi-python-pip-projects
  138. sed -i 's#text2url#appname#g' run.sh
  139. bash run.sh
  140. cd appname
  141. # Change in setup.cfg in project home
  142. # nano setup.cfg
  143. # name = text2url #change
  144. # version = 0.1.0 setup.cfg #change
  145.  
  146. # nano ~/.pypirc
  147. :<< 'COMMENT'
  148. [pypi]
  149. username = __token__
  150. password = TOCKEN FROM https://pypi.org/manage/account/token/
  151. COMMENT
  152.  
  153.  
  154. # 2. Activate Python virtual environment easily
  155. # Add this function to your /etc/bash.bashrc or ~/.profile for persistent usage
  156.  
  157. : <<'COMMENT'
  158. # Open the file using nano
  159. # nano /etc/bash.bashrc or nano ~/.profile
  160.  
  161. # Add this function
  162. activatepy(){
  163. python3 -m venv myenv # Create a Python virtual environment named 'myenv'
  164. source myenv/bin/activate # Activate the virtual environment
  165. }
  166. COMMENT
  167.  
  168. # After adding, reload the bash config or open a new terminal
  169. # source /etc/bash.bashrc
  170.  
  171. # 3. Install necessary Python tools for packaging
  172. # 'build' is for building distributions, 'twine' is for uploading to PyPI
  173. pip install build twine
  174.  
  175. # 4. Build your package
  176. # Creates source distribution and wheel files in the 'dist/' folder
  177. python3 -m build
  178.  
  179. # 5. Install your package locally to test
  180. # Replace with your actual wheel file
  181. pip install dist/text2url-0.1.0-py3-none-any.whl
  182.  
  183. # 6. Upload your package to PyPI
  184. # This pushes all files inside 'dist/' to https://pypi.org/
  185. twine upload dist/* --verbose
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
Advertisement
Add Comment
Please, Sign In to add comment