Create Your Own pip Library

Posted by: Juspreet51

Last Updated on: 30 Oct, 2021


Create Your Own pip Library



Packaging Python Projects

  1. Make Sure you have updated pip installed
    py -m pip install --upgrade pip

  2. Follow the steps from official doc to create a ready to push directory

  3. Make sure you're following the below dir structure:
    .
    └── your_pkg_name/
    ├── LICENSE
    ├── pyproject.toml
    ├── README.md
    ├── setup.py
    ├── src/
    │ └── example_package/
    │ ├── __init__.py
    │ └── example.py
    └── tests/

  4. Open example.py file and enter the following content: def add_one(number):
    return number + 1

  5. pyproject.toml tells build tools (like pip and build) what is required to build your project. This tutorial uses setuptools, so open pyproject.toml and enter the following content: [build-system]
    requires = [
    "setuptools>=42",
    "wheel"
    ]
    build-backend = "setuptools.build_meta"

  6. Open setup.py and enter import setuptools with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

    setuptools.setup(
    name="pkg_name",
    version="0.0.x",
    author="author name,
    author_email="contact@email.com",
    description="An EDA & modellling assist library",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://something.com/come/here",
    project_urls={
    "Bug Tracker": "https://something.com/go/there",
    },
    classifiers=[
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
    ],
    package_dir={"": "src"},
    install_requires = ["pandas", "numpy","matplotlib","pandas", "seaborn", "plotly", "sklearn"],
    packages=setuptools.find_packages(where="src"),
    python_requires=">=3.6",
    )

  7. Open README.md and enter the following content: # Example Package This is a simple example package. You can use [Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content.

  8. For help picking a license, see https://choosealicense.com/
    Once you have chosen a license, open LICENSE file and enter the license text, e.g. for MIT License Copyright (c) 2018 The Python Packaging Authority 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.

Generating distribution archives

  1. Open the setup.cfg or setup.py file and update the version to be x.y.1b1 (beta release 1)
    x.y.z = major.minor.patch
    .
    └── your_pkg_name/
    ├── LICENSE
    ├── pyproject.toml
    ├── README.md
    ├── setup.py <==== update version here
    ├── src/
    │ └── example_package/
    │ ├── __init__.py
    │ └── example.py
    └── Tests

  2. Move to the your_pkg_name directory and open cmd .
    └── your_pkg_name/ <==== Open cmd here
    ├── LICENSE
    ├── pyproject.toml
    ├── README.md
    ├── setup.cfg
    ├── src/
    │ └── example_package/
    │ ├── __init__.py
    │ └── example.py
    └── Tests

    ** CMD Trick: Go to the desired folder, press alt+D+Home, type cmd + Spacebar, hit enter

  3. Make sure you have the latest version of PyPA’s build installed:
    py -m pip install --upgrade build

  4. Build your package, so that it's ready to be pushed to PyPi
    py -m build

  5. That command just built your package, stored under your_pkg_name/dist, ready to be deployed to PyPi website

  6. Now we're ready to push the package. Using Twine to push the package
    First, we want to push it to Test-PyPi, to make sure everything works fine and is alright. Register your account

    Then move to Accounts page and search for Add API token To securely upload your project, you’ll need a PyPI API token. Create one token, fill "token name" as "token" and set "Scope" to "Entire account"
    Don’t close the page until you have copied and saved the token — you won’t see that token again. It's displayed only once . Save it somewhere safe

    Move back to CMD prompt and enter
    py -m pip install --upgrade twine
    py -m twine upload --repository testpypi dist/*

  7. For the username, use __token__. For the password, use the token value, including the pypi- prefix. And press enter

  8. Once pushed, goto PyPi Projects, click on View button and get pip install command for your library from the top blue banner of the page, under your_pkg_name 0.0.1b1

  9. It'll look something like this:
    pip install -i https://test.pypi.org/simple/ your_pkg_name

  10. Open anaconda prompt & create a new virtual env

  11. Activate newly created environment
    conda activate new_env_name

    Then install your library by pasting the command you copied from the above step
    pip install -i https://test.pypi.org/simple/ your_pkg_name

  12. Open jupyter nb to see if the package is properly installed and working:
    Import the package:
    from your_pkg_name import example
    example.add_one(2)

  13. If evrything is working rightly, reupdate the setup.cfg or setup.py file as x.y.1 (1st release)
    .
    └── your_name_pkg/
    ├── LICENSE
    ├── pyproject.toml
    ├── README.md
    ├── setup.py <==== update version here
    ├── src/
    │ └── example_package/
    │ ├── __init__.py
    │ └── example.py
    └── Tests

  14. Build your package, so that it's ready to be pushed to PyPi
    py -m build

  15. Using Twine to push the package:
    py -m twine upload dist/*

  16. For the username, use __token__. For the password, use the token value, including the pypi- prefix

  17. Once pushed, goto PyPi Projects and get pip install command for your library

  18. Open anaconda prompt & uninstall your library pip uninstall your_name_pkg And pip install the latest package from PyPi Projects page

  19. Run python to test proper working of the library:
    py

  20. Import the package:
    from example_package import example
    example.add_one(2)

  21. GitHub repository for Juspreet51 library

Something