{"id":10073,"library":"py-solc-x","title":"Py Solc X","description":"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.","status":"active","version":"2.0.5","language":"en","source_language":"en","source_url":"https://github.com/ApeWorX/py-solc-x","tags":["solidity","compiler","ethereum","blockchain","smart contracts","web3"],"install":[{"cmd":"pip install py-solc-x","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Most functionality is accessed via the top-level `solcx` module.","symbol":"solcx","correct":"import solcx"},{"note":"For direct compilation of Solidity source code.","symbol":"compile_source","correct":"from solcx import compile_source"}],"quickstart":{"code":"import solcx\nimport os\n\nsolidity_code = '''\npragma solidity ^0.8.0;\n\ncontract SimpleStorage {\n    uint256 public storedData;\n\n    constructor(uint256 initialData) {\n        storedData = initialData;\n    }\n\n    function set(uint256 x) public {\n        storedData = x;\n    }\n\n    function get() public view returns (uint25n) {\n        return storedData;\n    }\n}\n'''\n\n# Ensure a solc version is installed\n# Using os.environ.get for an example, in real use specify a version like '0.8.10'\ntarget_version = os.environ.get('SOLC_VERSION', '0.8.10')\n\nif not solcx.get_installed_solc_versions():\n    print(f\"No solc versions found. Installing {target_version}...\")\n    solcx.install_solc(target_version)\n\n# Select the desired solc version. This is crucial.\nsolcx.set_solc_version(target_version)\nprint(f\"Using solc version: {solcx.get_solc_version()}\")\n\n# Compile the Solidity code\ncompiled_sol = solcx.compile_source(\n    solidity_code,\n    output_values=['abi', 'bin']\n)\n\n# Extract contract data\ncontract_name = '<stdin>:SimpleStorage'\ncontract_abi = compiled_sol[contract_name]['abi']\ncontract_bytecode = compiled_sol[contract_name]['bin']\n\nprint(\"\\n--- Compiled Contract ABI ---\")\nprint(contract_abi)\nprint(\"\\n--- Compiled Contract Bytecode ---\")\nprint(contract_bytecode[:60] + '...') # Print first 60 chars of bytecode for brevity","lang":"python","description":"This quickstart demonstrates how to install a specific `solc` version, set it as the active compiler, and then compile a simple Solidity contract source code using `py-solc-x`. It outputs the contract's ABI and bytecode."},"warnings":[{"fix":"Review how `solc` versions are parsed and compared in your application. Ensure they are compatible with `packaging.Version`'s parsing logic. Direct usage of `solcx.set_solc_version` should still work with standard version strings, but be aware of stricter internal checks.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure network access to `binaries.soliditylang.org` is permitted. If manual binary management is required, consider using `solcx.set_solc_path()` after downloading the binary yourself.","message":"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.","severity":"gotcha","affected_versions":">=2.0.5"},{"fix":"Upgrade to `py-solc-x` v2.0.1 or newer to gain access to `solcx.select_pragma_version` for easier pragma-based version selection.","message":"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.","severity":"gotcha","affected_versions":"<2.0.1"},{"fix":"Upgrade to `py-solc-x` v1.1.0 or newer when compiling Solidity contracts written for version 0.8.0 or later to ensure correct ABI generation.","message":"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.","severity":"gotcha","affected_versions":"<1.1.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Call `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')`.","cause":"The `solc` compiler binary required for compilation is not found in the expected locations or has not been installed by `py-solc-x`.","error":"solcx.exceptions.SolcNotInstalled: Solc is not installed. Please install it with solcx.install_solc() or set your solc path with solcx.set_solc_path()."},{"fix":"Inspect 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`.","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.","error":"solcx.exceptions.SolcError: An error occurred during compilation. Command: ['/path/to/solc', '--abi', ...] Output: ... Error: Source file requires different compiler version (current compiler is ...)."},{"fix":"Ensure 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.","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()`.","error":"solcx.exceptions.VersionMismatchError: Pragma \"^0.8.0\" does not match current solc version \"0.7.6\""}]}