How to Create Python Package and Upload to PyPi

theurbanpenguin
theurbanpenguin
Create your own Python Package and upload to PyPi to share with other. Or at least in this case the test.pypi index. Adding the ...
Create your own Python Package and upload to PyPi to share with other. Or at least in this case the test.pypi index. Adding the licence the readme and the setup.py file plus you package we take you through it all and download in another python project.

Create new project
pip install --upgrade pip
pip install twine wheel setuptools
create README.md

my_test_project
This is a simple test project

create LICENSE.txt

see https://choosealicense.com/

MIT License

Copyright (c) 2024 The Urban Penguin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

create setup.py

from setuptools import setup, find_packages
from os import path
working_directory = path.abspath(path.dirname(__file__))

with open(path.join(working_directory, 'README.md'), encoding='utf-8') as f:
   long_description = f.read()

setup(
   name='my_test_project_am', # name of packe which will be package dir below project
   version='0.0.1',
   url='https://github.com/yourname/yourproject',
   author='The Urban Penguin',
   author_email='[email protected]',
   description='Simple test package',
   long_description=long_description,
   long_description_content_type='text/markdown',
   packages=find_packages(), auto_discover packages
   install_requires=[],
)

Now create the package to match name in setup.py
Now in package create module with function

project-
      - setup.py
      - README.md
      - LICENSE.txt
      - my_test_project_am/
                          - test.py
                                   - add_two_numbers()

Now create sdist (source dist) and bdist (Binary dist) files

To distribute your Python packages via PyPI (Python Package Index), it's common practice to generate both source distribution (sdist) and built distribution (often a wheel, which is a kind of binary distribution).
Here's why you might want to create both types:

Source Distribution (sdist):
A source distribution contains your source code and a setup.py file. This allows end-users to view and build your project from its source code. This is useful for distributing to other developers, who may want to modify your code, learn from it, or who may be running environments that require compilation (some C extensions, for example). A source distribution can be created using the python setup.py sdist command.

Built Distribution (wheel):
A wheel is a Python built distribution that is fast to install, doesn't require the execution of setup.py on the client's side, and doesn't require compilers since it's precompiled. Wheel packages can be installed with pip without building any source code. This is useful for end-users who only want to install and use your package without going through the build process. A wheel distribution can be created using the python setup.py bdist_wheel command.
Ideally, when publishing a package to PyPi, you'd upload both source (sdist) and built (wheel) distributions. This is because not all platforms or versions of Python out there may be compatible with a pre-built wheel file, but theoretically, they could all install from the source distribution. On the other hand, wheel is faster and easier to install, which is why it's preferred when compatible

python setup.py sdist bdist_wheel

check for errors or warning with twine

twine check dist/*

upload to testpypi ( you'll need API or username and password depending on your system )

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

create new project

pip install --index-url https://test.pypi.org/simple/ my-test-project-am


Additionally you can find my video courses on Pluralsight: http://pluralsight.com/training/Autho... and take time to see my own site http://www.theurbanpenguin.com

همه توضیحات ...