What it does
PyPortfolioOpt is a Python library for portfolio optimization, including mean-variance, Black-Litterman and hierarchical allocation methods. It separates estimated returns, risk models and optimization objectives. The MIT-licensed package is free to install, with examples for turning percentage weights into whole-share allocations.
PyPortfolioOpt makes the assumptions behind an allocation separate components. Expected returns describe the forecast; the covariance model describes how asset returns vary together. An EfficientFrontier object then combines those inputs with an objective and any added restrictions. You can supply your own estimates or calculate them from historical price data.
The example workflow loads prices, estimates returns and covariance, calls an optimizer, and cleans the resulting weights. save_weights_to_file exports them for later use. DiscreteAllocation takes those weights, recent prices and a total portfolio value to calculate whole-share quantities and leftover cash. It does not place trades.
Constraints can express a minimum portfolio score or a tracking-error limit against benchmark weights. A maximum number of holdings is more demanding: it needs a suitable mixed-integer solver, and the FAQ notes that this formulation does not work directly with every objective, including the standard maximum-Sharpe setup.
Use pip install PyPortfolioOpt in a Python environment and check the solver dependencies for the intended formulation. No PyPortfolioOpt login is required. The project distributes the library under the MIT licence; market data and any separately licensed solver remain their own inputs. Allocation estimates are sensitive to the return and risk assumptions, so changing an estimator can materially change the weights.
Strengths
- Lets return estimators and risk models be replaced independently
- Exports percentage weights and calculates whole-share allocations
- Supports custom score and benchmark-related constraints
Watch out for
- Limiting the number of holdings requires a suitable mixed-integer solver
- Optimized weights can be sensitive to estimated returns and covariance
Best for
- Python analysts comparing portfolio-allocation assumptions
- Developers converting model weights into a budgeted share allocation
Sources
- PyPortfolioOpt estimators, weight exports and discrete allocation (Oct 27, 2026)
- PyPortfolioOpt installation and optimization example (Oct 27, 2026)
- PyPortfolioOpt constraints and mixed-integer solver limits (Oct 27, 2026)
- PyPortfolioOpt MIT licence (Oct 27, 2026)
