How to create a python pip package?

If you are like me and come from .net or node js background, you might wonder how to create pip packages!

These are the steps you need to follow

1. Make sure you have the latest versions of `setuptools`, `wheel` and `twine` installed:
`pip install --user --upgrade setuptools wheel`
`pip install --user --upgrade twine`

2. Create a folder structure
`/yourpackagename
    __init__.py
setup.py
LICENSE
README.md`


`yourpackagename` can contain letters, numbers, _, and -. It also must not already have taken on pypi.org
` __init__.py` is your module, you can see a simple hello world sample here.
`setup.py` contains information about your package, you can see the sample\template from here.
`LICENSE` and `README.md` are optional

3. Test locally from source code

`pip install .`

it should install the package you created and you can import and test your package

4. Create a wheel local package

 `python setup.py sdist bdist_wheel`

5. Test wheel local package

 under `dist` there will be one or many `*.whl` file you can install it locally

`pip install C:/some-dir/some-file.whl` or
`pip install /dist/some-file.whl`

it should install the package you created and you can import and test your package


6. Now you are ready to upload your package to the Python Package Index under your account:

get a user account from  https://test.pypi.org and https://pypi.org

7. Upload the wheel package to the test\prerelease environment

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

8. Test newly uploaded package

`pip install -i https://test.pypi.org/simple/ yourpackagename`

10. Upload it to the production environment

`twine upload dist/*`

11. Test your package

`pip install yourpackagename`


Happy coding! GitHub repo for your reference https://github.com/Azadehkhojandi/helloworld

Comments