我在终端中通过
pip3 install py-solc-x
安装了 py-solc-x,但是当我运行该程序时,我仍然遇到导入错误。这是怎么回事?
我的代码:
from solcx import compile_standard
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputselection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
print(compiled_sol)
你应该先安装一个虚拟环境,然后使用 pip 安装 py-solc-x。
python3 -m venv env
source env/bin/activate
pip3 install py-solc-x
可能会检查这篇文章,似乎涵盖了课程视频中未涵盖的一些您遗漏的内容。简而言之...
添加
install_solc
第一行看起来像这样
from solcx import compile_standard, install_solc
然后添加
install_solc("0.6.0")
这条线以上
compile_sol = compile_standard(
所以看起来像这样
install_solc("0.6.0")
compile_sol = compile_standard(
在课程的这个阶段,您的代码应该如下所示...
from solcx import compile_standard, install_solc
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# Compile our Solidity
install_solc("0.6.0")
compile_sol = compile_standard(
{
"language": "solidity",
"source": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
print(compile_sol)
您可能还想查看课程的GitHub。在那里你会找到所有课程的索引。如果你点击一节课,你会在每节课的顶部找到一个指向代码的链接……如果你点击这个链接,你可以查看问题选项卡,了解针对该课向 Patric 提出的问题……这是 issues 链接 这节课
对于我自己,当我做这样的课程时,我喜欢将存储库克隆到另一个名为 1-clone 的目录中(因此它位于其他所有内容之上,而不是混入我的其他文件/文件夹中)。
cd 到 web3_py_simple_storage 目录 Patrick 让你在课程开始时制作然后
mkdir 1-clone
cd 1-clone
git clone https://github.com/PatrickAlphaC/web3_py_simple_storage
cd web3_py_simple_storage
这样一来,您将拥有所有用于检查代码的工具。
如果参加 solidity 课程的其他人遇到这里描述的相同问题,我有以下建议,在努力解决这个问题 4 天后终于对我有用:
确保您的操作系统对于 solc / solc-x 来说不是太旧!例如,在较旧的 MacBook(我的 2009 年的)上,早于 Catalina 的操作系统版本(10.13 或 10.14 及更早版本)将不支持/工作 solc。我所做的是在我的 High Sierra 上安装 Catalina Patcher。 High Sierra 实在是太老了。同样的问题将在稍后的布朗尼部分的课程中出现。
Solc 需要一些依赖项 - 在 Mac 上你需要 XCode - 检查你是否安装了它(我没有安装它所以无法安装 solc)并且它是适合你的操作系统的版本,如果没有,请更新它。对于 Catalina,您需要 Xcode.11。与命令行工具相同。
你确实需要在虚拟环境中工作,否则文件将无法编译(不知道为什么,但我觉得它很有趣 - CS 和编程的新手)。
附言在所有其他解决方案和建议都失败后,这些对我有用,因此请确保您首先尝试此处推荐的所有内容。尽管首先检查您是否安装了正确的依赖项和适当的操作系统是有意义的。
你安装了两个不同的python。不用python,用python3编译。
python3 file_name
import os, sys
print(sys.executable)
这解决了我的问题
要真正解决这个问题是在安装 py-solc-x 之后使用 pipenv 实际创建一个 python 环境