|
| 1 | +# Langbase Python SDK: Setup Guide |
| 2 | + |
| 3 | +This document provides instructions for setting up the development environment, testing the SDK, and publishing it to PyPI. |
| 4 | + |
| 5 | +## Local Development Setup |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Python 3.7 or higher |
| 10 | +- pip (Python package installer) |
| 11 | +- virtualenv (recommended) |
| 12 | + |
| 13 | +### Setting Up the Development Environment |
| 14 | + |
| 15 | +1. **Clone the repository**: |
| 16 | + ```bash |
| 17 | + git clone https://github.com/LangbaseInc/langbase-sdk-python |
| 18 | + cd langbase-sdk-python |
| 19 | + ``` |
| 20 | + |
| 21 | +2. **Create and activate a virtual environment**: |
| 22 | + ```bash |
| 23 | + python -m venv venv |
| 24 | + |
| 25 | + # On Unix/macOS |
| 26 | + source venv/bin/activate |
| 27 | + |
| 28 | + # On Windows |
| 29 | + venv\Scripts\activate |
| 30 | + ``` |
| 31 | + |
| 32 | +3. **Install development dependencies**: |
| 33 | + ```bash |
| 34 | + pip install -e ".[dev]" |
| 35 | + # Or |
| 36 | + pip install -r requirements-dev.txt |
| 37 | + ``` |
| 38 | + |
| 39 | +4. **Create a `.env` file**: |
| 40 | + ```bash |
| 41 | + cp .env.example .env |
| 42 | + ``` |
| 43 | + |
| 44 | + Then edit the `.env` file to include your API keys. |
| 45 | + |
| 46 | +5. Format the code: |
| 47 | + ```bash |
| 48 | + black . |
| 49 | + isort . |
| 50 | + ``` |
| 51 | + |
| 52 | +6. Run the tests: |
| 53 | + |
| 54 | +## Running Tests |
| 55 | + |
| 56 | +The SDK uses pytest for testing. To run the tests: |
| 57 | + |
| 58 | +```bash |
| 59 | +# Run all tests |
| 60 | +pytest |
| 61 | + |
| 62 | +# Run specific tests |
| 63 | +pytest tests/test_langbase.py |
| 64 | + |
| 65 | +# Run with coverage |
| 66 | +pytest --cov=langbase |
| 67 | +``` |
| 68 | + |
| 69 | +## Building the Package |
| 70 | + |
| 71 | +To build the package: |
| 72 | + |
| 73 | +```bash |
| 74 | +python -m build |
| 75 | +``` |
| 76 | + |
| 77 | +This will create both source distributions and wheel distributions in the `dist/` directory. |
| 78 | + |
| 79 | +## Testing the Package Locally |
| 80 | + |
| 81 | +You can test the package locally without publishing to PyPI: |
| 82 | + |
| 83 | +```bash |
| 84 | +# Install in development mode |
| 85 | +pip install -e . |
| 86 | +``` |
| 87 | + |
| 88 | +Then you can run examples: |
| 89 | + |
| 90 | +``` |
| 91 | +./venv/bin/python examples/pipes/pipes.run.py |
| 92 | +``` |
| 93 | + |
| 94 | +## Publishing to PyPI |
| 95 | + |
| 96 | +### Prerequisites |
| 97 | + |
| 98 | +- A PyPI account |
| 99 | +- twine package (`pip install twine`) |
| 100 | + |
| 101 | +### Steps to Publish |
| 102 | + |
| 103 | +1. **Make sure your package version is updated**: |
| 104 | + - Update the version number in `langbase/__init__.py` |
| 105 | + |
| 106 | +2. **Build the package**: |
| 107 | + ```bash |
| 108 | + python -m build |
| 109 | + ``` |
| 110 | + |
| 111 | +If it doesn't work, try installing the latest version of `build`: |
| 112 | + |
| 113 | +```bash |
| 114 | +pip install build |
| 115 | +``` |
| 116 | + |
| 117 | +And then run: |
| 118 | + |
| 119 | +```bash |
| 120 | +./venv/bin/python -m build |
| 121 | +``` |
| 122 | + |
| 123 | +3. **Check the package**: |
| 124 | + ```bash |
| 125 | + twine check dist/* |
| 126 | + ``` |
| 127 | + |
| 128 | +4. **Upload to TestPyPI (optional but recommended)**: |
| 129 | + ```bash |
| 130 | + twine upload --repository-url https://test.pypi.org/legacy/ dist/* |
| 131 | + ``` |
| 132 | + |
| 133 | +5. **Test the TestPyPI package**: |
| 134 | + ```bash |
| 135 | + pip install --index-url https://test.pypi.org/simple/ langbase |
| 136 | + ``` |
| 137 | + |
| 138 | +6. **Upload to PyPI**: |
| 139 | + ```bash |
| 140 | + twine upload dist/* |
| 141 | + ``` |
| 142 | + |
| 143 | +## Automating Releases with GitHub Actions |
| 144 | + |
| 145 | +For automated releases, you can use GitHub Actions. Create a workflow file at `.github/workflows/publish.yml` with the following content: |
| 146 | + |
| 147 | +```yaml |
| 148 | +name: Publish to PyPI |
| 149 | + |
| 150 | +on: |
| 151 | + release: |
| 152 | + types: [published] |
| 153 | + |
| 154 | +jobs: |
| 155 | + build-and-publish: |
| 156 | + runs-on: ubuntu-latest |
| 157 | + steps: |
| 158 | + - uses: actions/checkout@v3 |
| 159 | + - name: Set up Python |
| 160 | + uses: actions/setup-python@v4 |
| 161 | + with: |
| 162 | + python-version: '3.x' |
| 163 | + - name: Install dependencies |
| 164 | + run: | |
| 165 | + python -m pip install --upgrade pip |
| 166 | + pip install build twine |
| 167 | + - name: Build and publish |
| 168 | + env: |
| 169 | + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} |
| 170 | + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} |
| 171 | + run: | |
| 172 | + python -m build |
| 173 | + twine upload dist/* |
| 174 | +``` |
| 175 | +
|
| 176 | +## Project Structure |
| 177 | +
|
| 178 | +The project follows this structure: |
| 179 | +
|
| 180 | +``` |
| 181 | +langbase-python/ |
| 182 | +├── langbase/ # Main package |
| 183 | +│ ├── __init__.py # Package initialization |
| 184 | +│ ├── langbase.py # Main client implementation |
| 185 | +│ ├── request.py # HTTP request handling |
| 186 | +│ ├── errors.py # Error classes |
| 187 | +│ ├── types.py # Type definitions (not used) |
| 188 | +│ └── utils.py # Utility functions |
| 189 | +│ └── workflow.py # Workflow implementation |
| 190 | +├── tests/ # Test package |
| 191 | +│ ├── __init__.py # Test package initialization |
| 192 | +│ ├── test_client.py # Tests for the client |
| 193 | +│ ├── test_request.py # Tests for request handling |
| 194 | +│ ├── test_errors.py # Tests for error classes |
| 195 | +│ └── test_utils.py # Tests for utility functions |
| 196 | +│ └── test_workflow.py # Tests for workflow |
| 197 | +├── examples/ # Example scripts |
| 198 | +├── setup.py # Package setup script |
| 199 | +├── pyproject.toml # Project configuration |
| 200 | +├── requirements.txt # Package dependencies |
| 201 | +├── requirements-dev.txt # Development dependencies |
| 202 | +├── LICENSE # MIT license |
| 203 | +└── README.md # Main documentation |
| 204 | +``` |
| 205 | + |
| 206 | +## Contributing |
| 207 | + |
| 208 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 209 | + |
| 210 | +1. Fork the repository |
| 211 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 212 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 213 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 214 | +5. Open a Pull Request |
| 215 | + |
| 216 | +## Troubleshooting |
| 217 | + |
| 218 | +### Common Issues |
| 219 | + |
| 220 | +1. **Package not found after installation**: |
| 221 | + - Make sure your virtual environment is activated |
| 222 | + - Try running `pip list` to confirm installation |
| 223 | + |
| 224 | +2. **Build errors**: |
| 225 | + - Make sure you have the latest `build` package: `pip install --upgrade build` |
| 226 | + - Check for syntax errors in your code |
| 227 | + |
| 228 | +3. **Test failures**: |
| 229 | + - Run specific failing tests to get more details |
| 230 | + - Check for API key issues if integration tests are failing |
| 231 | + |
| 232 | +### Getting Help |
| 233 | + |
| 234 | +If you encounter issues not covered here, please open an issue on GitHub with detailed information about the problem, including: |
| 235 | + |
| 236 | +- Your Python version |
| 237 | +- Your operating system |
| 238 | +- Any error messages |
| 239 | +- Steps to reproduce the issue |
0 commit comments