Py Solc X
py-solc-x is a Python wrapper and version management tool for the `solc` Solidity compiler. It simplifies the installation, management, and use of multiple `solc` versions, allowing developers to compile Solidity code directly from Python. The current version is 2.0.5, and it sees frequent minor updates for bug fixes and occasional feature releases.
Common errors
-
solcx.exceptions.SolcNotInstalled: Solc is not installed. Please install it with solcx.install_solc() or set your solc path with solcx.set_solc_path().
cause The `solc` compiler binary required for compilation is not found in the expected locations or has not been installed by `py-solc-x`.fixCall `solcx.install_solc('X.Y.Z')` to download and install a specific `solc` version, where 'X.Y.Z' is the desired version (e.g., '0.8.10'). Alternatively, if `solc` is already installed elsewhere, use `solcx.set_solc_path('/path/to/solc')`. -
solcx.exceptions.SolcError: An error occurred during compilation. Command: ['/path/to/solc', '--abi', ...] Output: ... Error: Source file requires different compiler version (current compiler is ...).
cause The Solidity source code contains errors (syntax, missing imports, semantic issues) or the active `solc` compiler version does not satisfy the `pragma solidity` declaration in the contract.fixInspect the full `Output:` and `Error:` messages from the `SolcError` for specific details from the `solc` compiler. Correct any Solidity syntax errors, ensure all imported files are accessible, and verify that the `solcx.set_solc_version()` call selects a `solc` compiler version that matches your contract's `pragma solidity`. -
solcx.exceptions.VersionMismatchError: Pragma "^0.8.0" does not match current solc version "0.7.6"
cause The `pragma solidity` directive in your contract specifies a version range (e.g., `^0.8.0`) that is not satisfied by the `solc` version currently set via `solcx.set_solc_version()`.fixEnsure that the `solc` version you have selected with `solcx.set_solc_version()` (or that `py-solc-x` automatically selects) falls within the range specified by your contract's `pragma solidity`. You might need to `solcx.install_solc('X.Y.Z')` and then `solcx.set_solc_version('X.Y.Z')` to use a compatible compiler.
Warnings
- breaking Version 2.0.0 introduced significant internal changes to version handling, now utilizing the `packaging.Version` library. Code that relied on string-based comparisons or custom parsing of `solc` versions might behave differently or break. Ensure your version strings are compatible.
- gotcha Starting with v2.0.5, `solc` binaries are consistently fetched from `binaries.soliditylang.org`. Users operating in restrictive network environments or those expecting binaries from a different source (e.g., GitHub releases, if previously configured) might encounter download issues.
- gotcha Prior to v2.0.1, the `select_pragma_version` method was not publicly exposed. If your application needs to programmatically determine and set the correct `solc` version based on a contract's `pragma solidity` statement, this functionality was more difficult to implement.
- gotcha Older versions of `py-solc-x` (specifically prior to v1.1.0) had issues correctly handling the ABI format for Solidity 0.8.0 and newer versions. This could lead to incorrect ABI output or compilation failures for modern contracts.
Install
-
pip install py-solc-x
Imports
- solcx
import solcx
- compile_source
from solcx import compile_source
Quickstart
import solcx
import os
solidity_code = '''
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
constructor(uint256 initialData) {
storedData = initialData;
}
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint25n) {
return storedData;
}
}
'''
# Ensure a solc version is installed
# Using os.environ.get for an example, in real use specify a version like '0.8.10'
target_version = os.environ.get('SOLC_VERSION', '0.8.10')
if not solcx.get_installed_solc_versions():
print(f"No solc versions found. Installing {target_version}...")
solcx.install_solc(target_version)
# Select the desired solc version. This is crucial.
solcx.set_solc_version(target_version)
print(f"Using solc version: {solcx.get_solc_version()}")
# Compile the Solidity code
compiled_sol = solcx.compile_source(
solidity_code,
output_values=['abi', 'bin']
)
# Extract contract data
contract_name = '<stdin>:SimpleStorage'
contract_abi = compiled_sol[contract_name]['abi']
contract_bytecode = compiled_sol[contract_name]['bin']
print("\n--- Compiled Contract ABI ---")
print(contract_abi)
print("\n--- Compiled Contract Bytecode ---")
print(contract_bytecode[:60] + '...') # Print first 60 chars of bytecode for brevity