Python 中 Solc 安装出现问题:SolcNotInstalled 错误

问题描述 投票:0回答:1

Stack Overflow 社区您好,

我在尝试运行涉及 Solidity 编译的 Python 脚本时遇到问题。具体来说,我收到了 SolcNotInstalled 错误。

部署尝试: 我尝试通过以下文档使用 web3.py 库部署智能合约。我完全按照文档中所示使用了提供的代码。我的预期是智能合约代码将成功部署,但它引发了 SolcNotInstalled 错误。

文档代码执行: 我执行了文档中提供的代码,没有进行任何修改。但是,执行导致了 SolcNotInstalled 错误。

环境设置: 我为我的项目创建了一个专用环境,并安装了所有必需的库和依赖项,包括 web3.py 和 solcx。

代码:

import sys
import time
import pprint

from web3.providers.eth_tester import EthereumTesterProvider
from web3 import Web3
from eth_tester import PyEVMBackend
from solcx import compile_source

from solcx import compile_standard, install_solc
install_solc("0.6.0")

def compile_source_file(file_path):
   with open(file_path, 'r') as f:
      source = f.read()

   return compile_source(source,output_values=['abi','bin'])

def deploy_contract(w3, contract_interface):
    tx_hash = w3.eth.contract(
        abi=contract_interface['abi'],
        bytecode=contract_interface['bin']).constructor().transact()

    address = w3.eth.get_transaction_receipt(tx_hash)['contractAddress']
    return address

w3 = Web3(EthereumTesterProvider(PyEVMBackend()))

contract_source_path = 'contract.sol'
compiled_sol = compile_source_file('contract.sol')

contract_id, contract_interface = compiled_sol.popitem()

address = deploy_contract(w3, contract_interface)
print(f'Deployed {contract_id} to: {address}\n')

store_var_contract = w3.eth.contract(address=address, abi=contract_interface["abi"])

gas_estimate = store_var_contract.functions.setVar(255).estimate_gas()
print(f'Gas estimate to transact with setVar: {gas_estimate}')

if gas_estimate < 100000:
     print("Sending transaction to setVar(255)\n")
     tx_hash = store_var_contract.functions.setVar(255).transact()
     receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
     print("Transaction receipt mined:")
     pprint.pprint(dict(receipt))
     print("\nWas transaction successful?")
     pprint.pprint(receipt["status"])
else:
     print("Gas cost exceeds 100000")
'''




Error Message:
Traceback (most recent call last):
  File "D:\Accumulate\new_02_02\documentation_1.py", line 33, in <module>
    compiled_sol = compile_source_file('contract.sol')
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Accumulate\new_02_02\documentation_1.py", line 18, in compile_source_file
    return compile_source(source,output_values=['abi','bin'])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Accumulate\new_02_02\env\Lib\site-packages\solcx\main.py", line 101, in compile_source
    return _compile_combined_json(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Accumulate\new_02_02\env\Lib\site-packages\solcx\main.py", line 257, in _compile_combined_json
    solc_binary = get_executable(version=solc_version) if solc_binary is None else solc_binary
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Accumulate\new_02_02\env\Lib\site-packages\solcx\install.py", line 186, in get_executable
    raise SolcNotInstalled(
solcx.exceptions.SolcNotInstalled: Solc is not installed. Call solcx.get_installable_solc_versions() to view for available versions and solcx.install_solc() to install.
python blockchain solidity smartcontracts solc
1个回答
0
投票

该问题可能与缺少 solc 编译器安装有关。

检查这是否是问题的快速方法是运行命令

which solc
,它应该返回 solc 编译器二进制文件的路径。如果没有显示任何内容,则表示尚未安装,需要安装。

以下是有关如何在不同操作系统上安装它的文档:https://docs.soliditylang.org/en/latest/installing-solidity.html

© www.soinside.com 2019 - 2024. All rights reserved.