PyPI: Publish a Python Package
Source: Notion | Last edited: 2024-09-09 | ID: f64b6d6e-cd4...
Step 1: Prepare Your Environment
Section titled “Step 1: Prepare Your Environment”- Check Your Current Directory: Ensure you are in the correct directory where you want to create your package.
pwd- Create a New Directory for Your Package:
Choose a unique name for your package. For this example, we’ll use
UniqueCryptoConfig.
mkdir UniqueCryptoConfigcd UniqueCryptoConfig- Create the Module Subdirectory: Inside the package directory, create a subdirectory for your module.
mkdir CryptoConfig- Create the
__init__.pyFile: This file makes Python treat the directory as a package.
touch CryptoConfig/__init__.py- Create the Configuration File:
Create a file named
unique_crypto_config.pyto hold your configurations.
touch CryptoConfig/unique_crypto_config.pyStep 2: Add Your Configuration Code
Section titled “Step 2: Add Your Configuration Code”Open unique_crypto_config.py and add your configuration data:
from datetime import timedelta
CRYPTO_PAIRS = { 'SYNTHETICUSDT': 'SYNTHETICUSDT', 'Bitcoin (BTC)': 'BTCUSDT', 'Ethereum (ETH)': 'ETHUSDT', # ... other pairs ... 'Tia (TIA)': 'TIAUSDT'}
GRANULARITY_MAP = { '1s': timedelta(seconds=1), '1m': timedelta(minutes=1), '3m': timedelta(minutes=3), '5m': timedelta(minutes=5), '15m': timedelta(minutes=15), '30m': timedelta(minutes=30), '1h': timedelta(hours=1), '2h': timedelta(hours=2), '4h': timedelta(hours=4), '6h': timedelta(hours=6), '8h': timedelta(hours=8), '12h': timedelta(hours=12), '1d': timedelta(days=1)}Step 3: Create the setup.py File
Section titled “Step 3: Create the setup.py File”Create a setup.py file in the root of your package directory:
touch setup.pyOpen setup.py and add the following content:
from setuptools import setup, find_packages
setup( name='UniqueCryptoConfig', version='0.1', # Start with version 0.1 packages=find_packages(), description='A unique package for crypto configurations', author='Terry Li', author_email='terry@eonlabs.com', classifiers=[ 'Programming Language :: Python :: 3', 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', ], python_requires='>=3.6',)Step 4: Add a README File
Section titled “Step 4: Add a README File”Create a README.md file to provide information about your package:
touch README.mdAdd a brief description of your package in README.md:
# UniqueCryptoConfig
A unique package for crypto configurations, including pairs and granularity mappings.
## Installation
```bashpip install UniqueCryptoConfigfrom CryptoConfig.unique_crypto_config import CRYPTO_PAIRS, GRANULARITY_MAP
print(CRYPTO_PAIRS)print(GRANULARITY_MAP)Configuration Details
Section titled “Configuration Details”Crypto Pairs
Section titled “Crypto Pairs”The CRYPTO_PAIRS dictionary contains mappings of various cryptocurrency pairs to their respective symbols.
Granularity Map
Section titled “Granularity Map”The GRANULARITY_MAP dictionary provides time intervals for different granularities, represented as timedelta objects.
License
Section titled “License”This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Section titled “Contributing”Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Author
Section titled “Author”Terry Li - terry@eonlabs.com
Repository
Section titled “Repository”#### Step 5: Build Your Package
1. **Install Required Tools**: If you haven't already, install `setuptools`, `twine`, and `wheel`:
```bash python -m pip install setuptools twine wheel- Clean Previous Builds: Remove any previous build artifacts to ensure a clean build.
rm -rf build dist *.egg-info- Increment the Version Number:
Open your
setup.pyfile and increment the version number. For example, changeversion='0.1'toversion='0.2'.
from setuptools import setup, find_packages
setup( name='UniqueCryptoConfig', version='0.2', # Incremented version number packages=find_packages(), description='A unique package for crypto configurations', author='Terry Li', author_email='terry@eonlabs.com', classifiers=[ 'Programming Language :: Python :: 3', 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', ], python_requires='>=3.6',)- Build Your Package: Run the following command to build your package:
python setup.py sdist bdist_wheel- Verify the New Build:
Check the contents of the
distdirectory to ensure the new version is built correctly.
ls dist/You should see files like UniqueCryptoConfig-0.2-py3-none-any.whl and UniqueCryptoConfig-0.2.tar.gz.
- Check the Version in the Built Package:
You can inspect the metadata of the built package to ensure the version is correct. For example, you can use the
unzipcommand to inspect the.whlfile:
unzip -p dist/UniqueCryptoConfig-0.2-py3-none-any.whl UniqueCryptoConfig-0.2.dist-info/METADATA | grep VersionThis should output:
Version: 0.2Step 6: Create a PyPI Account and Generate an API Token
Section titled “Step 6: Create a PyPI Account and Generate an API Token”- Create a PyPI Account:
- Go to PyPI and click on “Register”.
- Follow the instructions to create a new account.
- Generate a PyPI Token:
- Go to your PyPI account settings and create an API token.
- Click on “Add API token” and give it a name (e.g.,
UniqueCryptoConfig-token). - Copy the token that is generated. Make sure to copy it now, as you won’t be able to see it again.
- Share the PyPI Token:
Share the token with me by pasting it here. This will be used to configure your
.pypircfile.
Step 7: Configure Authentication
Section titled “Step 7: Configure Authentication”- Create a
.pypircFile: Create a.pypircfile in your home directory to store your PyPI credentials.
touch ~/.pypirc- Add Your PyPI Token to ****
.pypirc: Open the.pypircfile in a text editor and add the following content, replacingYOUR-PYPI-TOKENwith the token you copied:
[distutils]index-servers = pypi
[pypi]username = __token__password = YOUR-PYPI-TOKENStep 8: Upload Your Package
Section titled “Step 8: Upload Your Package”- Upload Your Package:
Use
twineto upload your package to PyPI:
python -m twine upload dist/*If you encounter issues, use the --verbose option for more detailed output:
python -m twine upload --verbose dist/*Step 9: Install and Use Your Package
Section titled “Step 9: Install and Use Your Package”- Install Your Package: After publishing, you can install your package using:
python -m pip install UniqueCryptoConfig- Import Your Configurations: You can then import your configurations in your code like this:
from CryptoConfig.unique_crypto_config import CRYPTO_PAIRS, GRANULARITY_MAP
print(CRYPTO_PAIRS)print(GRANULARITY_MAP)Summary
Section titled “Summary”By following these steps, you will have successfully built, authenticated, published, installed, and used your Python package. Here’s a quick recap of the commands:
# Check your current directorypwd
# Create a new directory for your packagemkdir UniqueCryptoConfigcd UniqueCryptoConfig
# Create the module subdirectorymkdir CryptoConfig
# Create the __init__.py filetouch CryptoConfig/__init__.py
# Create the configuration filetouch CryptoConfig/unique_crypto_config.py
# Add your configuration code to unique_crypto_config.py# (Manually edit the file to add the code)
# Create the setup.py filetouch setup.py
# Add the setup configuration to setup.py# (Manually edit the file to add the setup configuration)
# Create the README.md filetouch README.md
# Add the README content to README.md# (Manually edit the file to add the README content)
# Install required toolspython -m pip install setuptools twine wheel
# Clean previous build directoriesrm -rf build dist *.egg-info
# Increment the version number in setup.py# (Manually edit the file to change version='0.1' to version='0.2')
# Build the package againpython setup.py sdist bdist_wheel
# Verify the new buildls dist/
# Check the version in the built packageunzip -p dist/UniqueCryptoConfig-0.2-py3-none-any.whl UniqueCryptoConfig-0.2.dist-info/METADATA | grep Version
# Create a PyPI account and generate an API token# (Follow the instructions to create an account and generate a token)
# Share the PyPI token with me# (Paste the token here)
# Create .pypirc file and add PyPI tokentouch ~/.pypirc# Add the following content to ~/.pypirc# [distutils]# index-servers =# pypi## [pypi]# username = __token__# password = YOUR-PYPI-TOKEN
# Upload the package to PyPIpython -m twine upload dist/*
# If you encounter issues, use verbose modepython -m twine upload --verbose dist/*
# Install the packagepython -m pip install UniqueCryptoConfig
# Use the package in your codefrom CryptoConfig.unique_crypto_config import CRYPTO_PAIRS, GRANULARITY_MAP
print(CRYPTO_PAIRS)print(GRANULARITY_MAP)By following these steps, you can ensure that your package is properly set up, authenticated, published, and ready for use.