As a next step, we will add our sample package to the PyPI repository. Before executing any command for publishing our package, we will need to create an account on Test PyPI.
Note that Test PyPI is a separate instance of the package index specifically for testing. In addition to the account with Test PyPI, we also need to add an API token to the account.
We will leave the details of creating an account and adding an API token to the account for you by following the instructions available on the Test PyPI website (https://test.pypi.org/).
To push the package to Test PyPI, we will need the Twine utility. We assume Twine is installed using the pip utility. To upload the masifutilv2 package, we will execute the following steps:
1. Create a distribution using the following command. This sdist utility will create a TAR ZIP file under a dist folder:
> python setup.py sdist
2. Upload the distribution file to Test PyPI. When prompted for a username and password, provide __token__ as the username and the API token as the password:
> twine upload --repository testpypi dist/masifutilv2-0.1.0.tar.gz
This command will push the package TAR ZIP file to the Test PyPI repository and the console output will be similar to the following:
Uploading distributions to https://test.pypi.org/legacy/
Enter your username: __token__
Enter your password:
Uploading masifutilv2-0.1.0.tar.gz
100%|█████████████████████|
5.15k/5.15k [00:02<00:00, 2.21kB/s]
We can view the uploaded file at https://test.pypi.org/project/masifutilv2/0.1.0/ after a successful upload.
Installing the package from PyPI
Installing the package from Test PyPI is the same as installing from a regular repository, except that we need to provide the repository URL by using the index-url arguments.
The command and the console output will be similar to the following:
> pip install --index-url https://test.pypi.org/simple/ --nodeps masifutilv2
This command will present console output similar to the following:
Looking in indexes: https://test.pypi.org/simple/
Collecting masifutilv2
Downloading https://test-files.pythonhosted.org/
packages/b7/e9/7afe390b4ec1e5842e8e62a6084505cbc6b9
f6adf0e37ac695cd23156844/masifutilv2-0.1.0.tar.gz (2.3 kB)
Building wheels for collected packages: masifutilv2
Building wheel for masifutilv2 (setup.py) ... done
Created wheel for masifutilv2: filename=masifutilv2-
0.1.0-py3-none-any.whl size=3497
sha256=a3db8f04b118e16ae291bad9642483874
f5c9f447dbee57c0961b5f8fbf99501
Stored in folder: /Users/muasif/Library/Caches/pip/
wheels/1c/47/29/95b9edfe28f02a605757c1
f1735660a6f79807ece430f5b836
Successfully built masifutilv2
Installing collected packages: masifutilv2
Successfully installed masifutilv2-0.1.0
As we can see in the console output, pip is searching for the module in Test PyPI. Once it finds the package with the name masifutilv2, it starts downloading and then installing it in the virtual environment.
In short, we have observed that once we create a package using the recommended format and style, then publishing and accessing the package is just a matter of using Python utilities and following the standard steps.
0 comments:
Post a Comment