Installation & Setup
Integrating uniswap-python into your project is straightforward, but it requires a properly configured Python environment and a connection to the Ethereum blockchain.
Prerequisites
- Python Version:
uniswap-pythonrequires Python 3.7.2 or higher due to its reliance on modern typing extensions and async features in underlying libraries. - C++ Compiler (Optional but Recommended): The underlying
web3.pyand cryptography libraries sometimes require a C++ compiler to build extensions from source. On Linux, ensurebuild-essentialis installed. On macOS, ensure Xcode Command Line Tools are installed.
Method 1: Installing via PIP (Recommended)
For most users, installing the latest stable release from PyPI is the best approach. It is highly recommended to use a virtual environment (venv, pipenv, or conda) to avoid dependency conflicts.
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows use: .venv\Scripts\activate
# Install the package
pip install uniswap-python
Method 2: Installing from Source (Git)
If you need the absolute latest features, bug fixes, or experimental L2 support that hasn't been officially published to PyPI yet, you can install directly from the master branch on GitHub:
pip install git+https://github.com/uniswap-python/uniswap-python.git
Method 3: Installing for Development (Poetry)
If you plan to contribute to the library, run the test suite, or modify the source code locally, you should use Poetry, which this project uses for dependency management.
# Clone the repository
git clone https://github.com/uniswap-python/uniswap-python.git
cd uniswap-python
# Install dependencies (including dev dependencies like pytest and Sphinx)
poetry install
Configuring your Web3 Provider
uniswap-python is a client library; it does not run an Ethereum node itself. To interact with the blockchain, quote prices, or submit transactions, you must connect to an RPC (Remote Procedure Call) provider.
Why do I need a provider?
Ethereum is a decentralized network. A provider acts as your gateway, reading contract states (like liquidity pool reserves) and broadcasting your signed transactions.
Choosing a Provider
You can use a free tier from major infrastructure providers:
Setting the Provider Environment Variable
The easiest way to configure your provider is to set the PROVIDER environment variable. The library will automatically detect this and initialize the Web3 connection.
Linux/macOS:
export PROVIDER="https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
Windows (Command Prompt):
set PROVIDER="https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
Using .env files:
If you prefer, you can use the python-dotenv package. Create a file named .env in your project root:
PROVIDER=https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID
The unipy CLI tool automatically loads .env files.
Troubleshooting Installation
ModuleNotFoundError: No module named 'web3': Ensure you activated your virtual environment before installing the package.- Errors installing
eth-hashorpycryptodome: These cryptography packages require compiling C code. Ensure you have the necessary build tools installed for your operating system.
Once installed and configured, proceed to the Getting Started guide.