This article demonstrate how to build and publish donno to PyPI.
A minimum hello-world demo
Necessary files & scripts
cd pyDsnote
python -m venv env
. env/bin/activate
cat << EOF > .envrc
. env/bin/activate
EOF
cat << EOF > 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.
EOF
cat << EOF > README.md
This is a simple note-take CLI application.
EOF
cat << EOF > setup.py
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
version="0.0.1",
author="Example Author",
author_email="author@example.com",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
keywords="note pim",
scripts=['hw'],
)
EOF
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip setuptools wheel
cat << EOF > hw
#!/usr/bin/env python
print('hello world')
EOF
pip install -e .
hw
Note use pip install -e
to install the package in the development environment.
This demo use scripts
as the application command, which is not very flexible.
Publish to TestPyPI
Install and configure publishing tool twine:
pip install twine
echo "export TWINE_USERNAME=leechau" >> .envrc
keyring set https://upload.pypi.org/legacy/ leechau
keyring set https://test.pypi.org/legacy/ leechau
Register on TestPyPI, verify the email address,
and set auto login with keyring
in root folder of pyDsnote,
and upload the package to TestPyPI:
pip install twine
echo "export TWINE_USERNAME=leechau" >> .envrc
keyring set https://upload.pypi.org/legacy/ leechau
keyring set https://test.pypi.org/legacy/ leechau
python setup.py sdist bdist_wheel
twine upload -r testpypi dist/*
In another virtualenv:
pip install -i https://test.pypi.org/simple/ donno==0.2.0
# if the app have dependencies:
pip install --extra-index-url https://test.pypi.org/simple/ donno
hw
Standard development process
Create the folder for the package:
mkdir tests donno
touch donno/__init__.py
See donno for more details of Python scripts.
Publish to PyPI
python setup.py sdist bdist_wheel
twine upload dist/*
Notes in 2014:
Publish to PyPI
Follow How To Package Your Python Code.
Register
I ran "python setup.py register" twice. Firstly I use option 2 to register a new account, then ran it again and use option 1 to upload the "funniest" package.
Then I saw this package in my account on PyPI. In the management page, there is a "remove" button for delete this package.
$ python setup.py register
Create a source distribution
$ python setup.py sdist
This is create a tar.gz file under dist folder. If you like, copy that file to another host, unpacking it and install it with "python setup.py install".
Upload source distribution
$ python setup.py sdist upload
After modification of source codes and version number in setup.py
(say, modify it to 0.2) run last command again to refresh distribution on PyPI.
You can see it at "https://pypi.python.org/simple/
On another host, use "pip install
Publish in LAN
On the develop host
-
Edit source code;
-
Create new source code distribution package:
python setup.py install sdist
;
On the deploy host
-
Startup a virtual environment:
workon test
; -
Write a install script:
!/bin/bash
scp lichao@10.21.2.7:/home/lichao/docs/python_projects/donno/dist/donno-0.1.10.tar.gz . tar zxvf donno-0.1.10.tar.gz cd donno-0.1.10 python setup.py install cd ..