Python wrapper and version management tool for the solc Solidity compiler.
npx @tessl/cli install tessl/pypi-py-solc-x@2.0.0A comprehensive Python wrapper and version management tool for the Solidity compiler (solc). py-solc-x enables developers to programmatically install, manage, and use multiple versions of the Solidity compiler with full cross-platform support for Linux, macOS, and Windows.
pip install py-solc-ximport solcxCommon usage patterns:
from solcx import compile_source, compile_files, install_solc, set_solc_versionSpecific imports:
from solcx import (
compile_source,
compile_files,
compile_standard,
get_solc_version,
install_solc,
get_installed_solc_versions,
set_solc_version
)import solcx
# Install solc if not already installed
solcx.install_solc('0.8.19')
solcx.set_solc_version('0.8.19')
# Compile Solidity source code
source_code = '''
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
constructor(uint256 _initialData) {
storedData = _initialData;
}
function set(uint256 _data) public {
storedData = _data;
}
function get() public view returns (uint256) {
return storedData;
}
}
'''
# Compile from source string
compiled_sol = solcx.compile_source(source_code)
contract_interface = compiled_sol['<stdin>:SimpleStorage']
print("ABI:", contract_interface['abi'])
print("Bytecode:", contract_interface['bin'])
# Compile from files
# compiled_sol = solcx.compile_files(['contracts/MyContract.sol'])py-solc-x provides three main layers of functionality:
compile_source(), compile_files(), and compile_standard() that handle common compilation workflowsThe package automatically handles cross-platform differences, binary path management, and version resolution, making it simple to integrate Solidity compilation into Python applications and development workflows.
Core compilation functions for compiling Solidity source code from strings, files, or using the standard JSON input/output interface. Supports all major solc compilation options including optimization, EVM target versions, and output customization.
def compile_source(source: str, **kwargs) -> dict: ...
def compile_files(source_files: Union[List, Path, str], **kwargs) -> dict: ...
def compile_standard(input_data: dict, **kwargs) -> dict: ...
def link_code(unlinked_bytecode: str, libraries: dict, **kwargs) -> str: ...
def get_solc_version(with_commit_hash: bool = False) -> Version: ...Install, manage, and switch between different versions of the Solidity compiler. Supports downloading precompiled binaries and compiling from source on Linux/macOS. Includes pragma-based version selection and automatic version resolution.
def install_solc(version: Union[str, Version] = "latest", **kwargs) -> Version: ...
def compile_solc(version: Optional[Union[str, Version]] = None, **kwargs) -> Version: ...
def get_installable_solc_versions() -> List[Version]: ...
def get_compilable_solc_versions(**kwargs) -> List[Version]: ...
def get_installed_solc_versions(**kwargs) -> List[Version]: ...
def get_solcx_install_folder(**kwargs) -> Path: ...
def import_installed_solc(**kwargs) -> List[Version]: ...
def set_solc_version(version: Union[str, Version], **kwargs) -> None: ...
def set_solc_version_pragma(pragma_string: str, **kwargs) -> Version: ...
def install_solc_pragma(pragma_string: str, **kwargs) -> Version: ...Comprehensive exception hierarchy for handling compilation errors, installation failures, version mismatches, and configuration issues. Provides detailed error information including command output and diagnostic data.
class SolcError(Exception): ...
class ContractsNotFound(SolcError): ...
class SolcInstallationError(Exception): ...
class SolcNotInstalled(Exception): ...
class UnknownOption(AttributeError): ...
class UnknownValue(ValueError): ...
class UnexpectedVersionError(Exception): ...
class UnsupportedVersionError(ValueError): ...
class DownloadError(Exception): ...
class UnexpectedVersionWarning(Warning): ...Direct access to the Solidity compiler binary with full command-line argument support. Provides low-level control over solc execution for advanced use cases and custom compilation workflows.
import solcx.wrapper
def solc_wrapper(**kwargs) -> Tuple[str, str, List, subprocess.Popen]: ...
def get_solc_version(solc_binary: Union[Path, str], **kwargs) -> Version: ...
def get_version_str_from_solc_binary(solc_binary: Union[Path, str]) -> str: ...The wrapper module is available as solcx.wrapper and provides direct access to solc binary execution.
from typing import Union, List, Dict, Optional, Any
from pathlib import Path
from packaging.version import Version
# Common type aliases used throughout the API
VersionType = Union[str, Version]
PathType = Union[Path, str]
SourceFiles = Union[List, Path, str]
ImportRemappings = Optional[Union[Dict, List, str]]
CompilerOutput = Dict[str, Any]