Python is a great programming language. The main reason python is so popular is the growing number of packages available to anyone. You can find those packages in many websites and in package managers. The official package manager for python is pypi , there are 2 versions – one for testing and one for production
Lets go over the process:
- Create the package
- Test the package locally
- Add it to git and github (optional)
- Deploy the package to the test package manager
- Test the package from pypitest
- Deploy the package to pypi production
- Test the package from pypi
- Update package version and re-deploy it
Create a github repository:
# echo "# simpstrlib" >> README.md # git init # git add README.md # git commit -m "first commit" # git remote add origin https://github.com/dev-area/simpstrlib.git # git push -u origin master
To create a package add a new directory and create a file __init__.py (you can leave it empty or add some package initialization)
# mkdir simpstrlib # cd simpstrlib # touch __init__.py # touch strlib.py
Add the following functions to strlib.py
def revStr(st): return st[::-1] def addstr(st1,st2): return st1 + ":" + st2
update git and github:
# git add . # git commit -m "simple" # git push
test the module using python or ipython shell (in the root package directory)
In [1]: import simpstrlib.strlib In [2]: s="hello" In [3]: simpstrlib.strlib.revStr(s) Out[3]: 'olleh' In [4]: simpstrlib.strlib.addstr(s,"world") Out[4]: 'hello:world'
Configure pypi
Create accounts for the test site and the production site
Create a file for your pypi accounts. You should name it .pypirc (note the dot) and place it in your home directory (~)
~/.pypirc
[distutils] index-servers = pypi pypitest [pypitest] username=liranbh password=12345 [pypi] username=liranbh password=12345
Configure the Package For pypi
First we need to add 2 files to the root directory of the module:
setup.cfg:
[metadata] description-file = README.md
And a setup script :
setup.py
from distutils.core import setup setup( name = 'simpstrlib', packages = ['simpstrlib'], # this must be the same as the name above version = '0.1', description = 'Simple String library', author = 'Liran BH', author_email = '[email protected]', url = 'https://github.com/dev-area/simpstrlib', download_url = 'https://github.com/dev-area/simpstrlib/tarball/0.1', keywords = ['string', 'reverse'], classifiers = [], )
Add tags and update github:
# git tag 0.1 -m "add tags" # git push --tags
Deploy The Package to pypitest:
First make sure you have an updated version of setuptools and twine packages:
# pip install -U pip setuptools twine
Now run this to deploy the package:
# python setup.py sdist # twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Testing The Package
To test the package , lets create a virtual environment :
# virtualenv -p /usr/local/bin/python3 env # source env/bin/activate (env) pip install -i https://testpypi.python.org/pypi simpstrlib ... Installing collected packages: simpstrlib Successfully installed simpstrlib-0.1 (env) python >>> import simpstrlib.strlib as slib >>> s="hello" >>> slib.revStr(s) 'olleh' >>>
Deploy The Package To Production
to deploy to pypi:
# twine upload dist/*
Using The Production Package
# virtualenv -p /usr/local/bin/python3 env # source env/bin/activate (env) pip install simpstrlib (env) python >>> import simpstrlib.strlib as slib >>> s="hello" >>> slib.revStr(s) 'olleh' >>>
Creating a new version
Update your code
Add a new git tag and push changes
# git tag 0.2 -m "new version" # git push --tags
Update setup.py and change the version number
from distutils.core import setup setup( name = 'simpstrlib', packages = ['simpstrlib'], # this must be the same as the name above version = '0.2', description = 'Simple String library', author = 'Liran BH', author_email = '[email protected]', url = 'https://github.com/dev-area/simpstrlib', download_url = 'https://github.com/dev-area/simpstrlib/tarball/0.2', keywords = ['string', 'reverse'], classifiers = [], )
run setup.py:
# python setup.py sdist
deploy to pypi test and production
# twine upload dist/simpstrlib-0.2.tar.gz # twine upload --repository-url https://test.pypi.org/legacy/ dist/simpstrlib-0.2.tar.gz
Now if you install the package again you will get version 0.2
1 thought on “Deploying a New Python Package to Pypi”
Comments are closed.
[…] How to deploy a new Python package to PyPI. […]