Creating a Python package is really usefull for versioning and distribuiton to our help methods or even our comproject complete, today let’s user Poetry to create a simple project (GUID Generator) and show how to deploy for a Pypi-Server
2 – Creating the Poetry project
poetry new guid-generator
3 – Virtual Enviroment
Poetry by defaul creates a virtual enviroment for our project, let’s activate it!
#activating
poetry shell
#desactivating
exit
4 – Install a Poetry dependency
poetry install xxxx
5 – Inside the guidgenerator fold create our file main.py and let’s code!
def get_new_uuid4():
from uuid import uuid4
return uuid4()
5.1 – Inside the guidgenerator path inside the file __init__.py code it:
from guidgenerator.main import *
__version__ = '0.1.0'
6 – Let’s install all dependecies for our project
poetry install
After the installation we can see our project already packaged with a python structure:
7 – Done! we can open a python command tool just to test our project, just type python and a python command line opens, just import our method and call it :
8- Voila! Our project is working so let’s deploy it to pypi server or a self hosted pypyi-server (I gonna create another post for the installation)
9- Publishing our package for self hosted pypy-server:
9.1 – Let’s add our pypy-server repository:
poetry config repositories.mypypi http://xx.com
9.2 – Let’s push it to our repository:
poetry publish -r mypypi
9.3 – After the command it gonna ask for username and password if you don’t have it just leave it blank and tape ENTER, if everything is ok we gonna receive the messaged of success:
10 – Now let’s just try to install our package directly from our pypi-server:
pip install guidgenerator --extra-index-url http://xx.com --trusted-host xx.com
11 – Voila!, our package and available for using!
Referecence: https://python-poetry.org/docs/basic-usage