Python wrapper around the solc Solidity compiler
npx @tessl/cli install tessl/pypi-py-solc@2.2.0A comprehensive Python wrapper around the solc Solidity compiler, enabling developers to compile Solidity smart contracts directly from Python applications. Provides both standard JSON compilation and legacy combined JSON compilation methods, supports various solc versions (>=0.4.2), includes functionality for linking compiled code, and features an experimental solc binary installation system.
pip install py-solcsemantic_version>=2.6.0solc executable (>=0.4.2)import solcFor specific functionality:
from solc import (
compile_source,
compile_files,
compile_standard,
link_code,
get_solc_version,
get_solc_version_string,
install_solc
)from solc import compile_standard
# Standard JSON compilation
result = compile_standard({
'language': 'Solidity',
'sources': {
'Foo.sol': {
'content': '''
pragma solidity ^0.4.24;
contract Foo {
function foo() public pure returns (uint) {
return 42;
}
}
'''
}
},
'settings': {
'outputSelection': {
'*': {
'*': ['abi', 'metadata', 'evm.bytecode', 'evm.bytecode.sourceMap']
}
}
}
})
print(result['contracts']['Foo.sol']['Foo']['abi'])from solc import compile_source, compile_files
# Compile from source string
result = compile_source('''
pragma solidity ^0.4.24;
contract Foo {
function foo() public pure returns (uint) {
return 42;
}
}
''')
print(result['Foo']['abi'])
# Compile from files
result = compile_files(['/path/to/contract.sol'])from solc import get_solc_version, install_solc
# Check current solc version
version = get_solc_version()
print(f"Solc version: {version}")
# Install specific version
install_solc('v0.4.24')Modern compilation interface providing comprehensive control over compilation settings and outputs, supporting the Solidity standard JSON format.
def compile_standard(input_data, allow_empty=False, **kwargs):
"""
Compile using Solidity standard JSON interface.
Args:
input_data (dict): Standard JSON input data for compilation
allow_empty (bool): Whether to allow compilation with no contracts found
**kwargs: Additional arguments passed to solc wrapper
Returns:
dict: Standard JSON compilation output
Raises:
ContractsNotFound: When no contracts found and allow_empty=False
SolcError: When compilation fails
"""Legacy compilation methods using the combined JSON output format, supporting older workflows and integrations.
def compile_source(source, allow_empty=False, output_values=ALL_OUTPUT_VALUES, **kwargs):
"""Compile Solidity source code from a string."""
def compile_files(source_files, allow_empty=False, output_values=ALL_OUTPUT_VALUES, **kwargs):
"""Compile Solidity source files."""Links compiled bytecode with library addresses to resolve library dependencies.
def link_code(unlinked_data, libraries):
"""
Link compiled bytecode with library addresses.
Args:
unlinked_data (str): Unlinked bytecode containing library placeholders
libraries (dict): Dictionary mapping library names to addresses
Returns:
str: Linked bytecode with library placeholders replaced by actual addresses
"""Gets version information from the solc compiler binary for compatibility checking and debugging.
def get_solc_version_string(**kwargs):
"""Get the solc version string."""
def get_solc_version(**kwargs):
"""Get the solc version as semantic version object."""Experimental functionality for installing specific versions of the solc compiler binary across different platforms.
def install_solc(identifier, platform=None):
"""
Install specified version of solc compiler.
Args:
identifier (str): Version identifier (e.g., 'v0.4.24')
platform (str): Target platform ('linux', 'darwin', 'win32')
Note:
This is experimental functionality. Installed binary location:
$HOME/.py-solc/solc-{version}/bin/solc
"""Comprehensive error handling for compilation failures and system issues.
class SolcError(Exception):
"""Base exception for solc compilation errors."""
class ContractsNotFound(SolcError):
"""Raised when no contracts are found during compilation."""All compilation functions support import remappings for cleaner project organization:
from solc import compile_files
result = compile_files(
['contract.sol'],
import_remappings=["zeppelin=/path/to/zeppelin-solidity"]
)The **kwargs parameter in compilation functions supports solc command-line options:
from solc import compile_source
result = compile_source(
source_code,
allow_paths="/path/to/imports",
optimize=True,
optimize_runs=200
)