Skip to content

PyPI: Publish a Python Package

Source: Notion | Last edited: 2024-09-09 | ID: f64b6d6e-cd4...


  1. Check Your Current Directory: Ensure you are in the correct directory where you want to create your package.
Terminal window
pwd
  1. Create a New Directory for Your Package: Choose a unique name for your package. For this example, we’ll use UniqueCryptoConfig.
Terminal window
mkdir UniqueCryptoConfig
cd UniqueCryptoConfig
  1. Create the Module Subdirectory: Inside the package directory, create a subdirectory for your module.
Terminal window
mkdir CryptoConfig
  1. Create the __init__.py File: This file makes Python treat the directory as a package.
Terminal window
touch CryptoConfig/__init__.py
  1. Create the Configuration File: Create a file named unique_crypto_config.py to hold your configurations.
Terminal window
touch CryptoConfig/unique_crypto_config.py

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)
}

Create a setup.py file in the root of your package directory:

Terminal window
touch setup.py

Open 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',
)

Create a README.md file to provide information about your package:

Terminal window
touch README.md

Add a brief description of your package in README.md:

# UniqueCryptoConfig
A unique package for crypto configurations, including pairs and granularity mappings.
## Installation
```bash
pip install UniqueCryptoConfig
from CryptoConfig.unique_crypto_config import CRYPTO_PAIRS, GRANULARITY_MAP
print(CRYPTO_PAIRS)
print(GRANULARITY_MAP)

The CRYPTO_PAIRS dictionary contains mappings of various cryptocurrency pairs to their respective symbols.

The GRANULARITY_MAP dictionary provides time intervals for different granularities, represented as timedelta objects.

This project is licensed under the MIT License - see the LICENSE file for details.

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Terry Li - terry@eonlabs.com

GitHub 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
  1. Clean Previous Builds: Remove any previous build artifacts to ensure a clean build.
Terminal window
rm -rf build dist *.egg-info
  1. Increment the Version Number: Open your setup.py file and increment the version number. For example, change version='0.1' to version='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',
)
  1. Build Your Package: Run the following command to build your package:
Terminal window
python setup.py sdist bdist_wheel
  1. Verify the New Build: Check the contents of the dist directory to ensure the new version is built correctly.
Terminal window
ls dist/

You should see files like UniqueCryptoConfig-0.2-py3-none-any.whl and UniqueCryptoConfig-0.2.tar.gz.

  1. 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 unzip command to inspect the .whl file:
Terminal window
unzip -p dist/UniqueCryptoConfig-0.2-py3-none-any.whl UniqueCryptoConfig-0.2.dist-info/METADATA | grep Version

This should output:

Version: 0.2

Step 6: Create a PyPI Account and Generate an API Token

Section titled “Step 6: Create a PyPI Account and Generate an API Token”
  1. Create a PyPI Account:
  • Go to PyPI and click on “Register”.
  • Follow the instructions to create a new account.
  1. 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.
  1. Share the PyPI Token: Share the token with me by pasting it here. This will be used to configure your .pypirc file.
  1. Create a .pypirc File: Create a .pypirc file in your home directory to store your PyPI credentials.
Terminal window
touch ~/.pypirc
  1. Add Your PyPI Token to ****.pypirc: Open the .pypirc file in a text editor and add the following content, replacing YOUR-PYPI-TOKEN with the token you copied:
[distutils]
index-servers =
pypi
[pypi]
username = __token__
password = YOUR-PYPI-TOKEN
  1. Upload Your Package: Use twine to upload your package to PyPI:
Terminal window
python -m twine upload dist/*

If you encounter issues, use the --verbose option for more detailed output:

Terminal window
python -m twine upload --verbose dist/*
  1. Install Your Package: After publishing, you can install your package using:
Terminal window
python -m pip install UniqueCryptoConfig
  1. 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)

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:

Terminal window
# Check your current directory
pwd
# Create a new directory for your package
mkdir UniqueCryptoConfig
cd UniqueCryptoConfig
# Create the module subdirectory
mkdir CryptoConfig
# Create the __init__.py file
touch CryptoConfig/__init__.py
# Create the configuration file
touch 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 file
touch setup.py
# Add the setup configuration to setup.py
# (Manually edit the file to add the setup configuration)
# Create the README.md file
touch README.md
# Add the README content to README.md
# (Manually edit the file to add the README content)
# Install required tools
python -m pip install setuptools twine wheel
# Clean previous build directories
rm -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 again
python setup.py sdist bdist_wheel
# Verify the new build
ls dist/
# Check the version in the built package
unzip -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 token
touch ~/.pypirc
# Add the following content to ~/.pypirc
# [distutils]
# index-servers =
# pypi
#
# [pypi]
# username = __token__
# password = YOUR-PYPI-TOKEN
# Upload the package to PyPI
python -m twine upload dist/*
# If you encounter issues, use verbose mode
python -m twine upload --verbose dist/*
# Install the package
python -m pip install UniqueCryptoConfig
# Use the package in your code
from 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.