2

I installed py-solc-x via pip3 install py-solc-x in the terminal but when I run the program, I still get an importerror. What's going on here?

My code:

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)
TylerH
  • 20,799
  • 66
  • 75
  • 101
Alditrus
  • 87
  • 1
  • 5
  • 1
    This probably doesn't have a general answer and is based on your setup. Probably 2 different Python versions. – Jason S Nov 03 '21 at 04:02
  • https://stackoverflow.com/questions/70326346/vscode-doesnt-recognize-py-solc-x-doesnt-library/70526404#70526404 – Yilmaz Jan 15 '22 at 01:39

7 Answers7

4

you should install a virtual env first then use pip to install py-solc-x.

python3 -m venv env 
source env/bin/activate
pip3 install py-solc-x 
Joel John
  • 41
  • 1
  • 1
    If you are new to Python it is easier to make a habit of using virtual environments now, before you start coding, as opposed to later when you begin working on multiple projects. Virtual environments make it easy to create Python and package specific versions of your application. – Joel John Nov 11 '21 at 14:29
  • source : The term 'source' is not recognized as the name of a cmdlet – rsc05 Jun 05 '22 at 20:18
  • This did not work for me! Can you please help @Joel John – rsc05 Jun 08 '22 at 13:22
  • Note that these instructions for macOS and Linux. Please read https://packaging.python.org/ on how to use virtualenvs on Windows. – Mikko Ohtamaa Sep 30 '22 at 00:08
3

Might check this post, Seems to cover a few things you are missing that are not covered in the course video. In a nutshell...

add

install_solc

to the first line so it looks like this

from solcx import compile_standard, install_solc

Then add

install_solc("0.6.0")

Above this line

compile_sol = compile_standard(

So it looks like this

install_solc("0.6.0")

compile_sol = compile_standard(

At this stage in the course your code should look like this...

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)

You might also want to checkout the GitHub for the course. There you will find an index for all the lessons. If you click a lesson you will find a link to the code at the top of each lesson... if you follow the link you can check the issues tab for issues raised to Patric for that lesson... Here is the issues link for this lesson

For myself when I do courses like this I like to clone the repository into another dir named 1-clone (so it is on top of everything else and not mixed into my other files/folders).

cd into the web3_py_simple_storage dir Patrick has you make at the start of the course and then

mkdir 1-clone
cd 1-clone
git clone https://github.com/PatrickAlphaC/web3_py_simple_storage
cd web3_py_simple_storage

This way you will have everything right there for you to check your code against.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dcamy
  • 39
  • 1
2

If other people taking the solidity course face this same issue described here, I have the following suggestions that finally worked for me after struggling to sort this out for 4 days:

  1. Make sure your Operating System is not too old for solc / solc-x! For example, on older MacBooks (mine's from 2009), OS versions older than Catalina (10.13 or 10.14 and older) will not support/work for solc. What I did was to install a Catalina Patcher on top of my High Sierra. High Sierra is simply too old. This same issue will come up later in the course in the Brownie section.

  2. Solc has some dependencies required - on Mac you need XCode - check that you have it installed (I didnt have it so it made it impossible to instal solc) and it is the right version for your OS and if not, update it. For Catalina, you need Xcode.11. Same with Command Line Tools.

  3. You need to work in a virtual environment, indeed, otherwise the file won't compile (don't know why, though, I find it intriguing - new to CS and programming).

P.S. these worked for me after all the other solutions and suggestions failed to work, so make sure you first try everything that was recommended here. Although it makes sense to first check if you have the right dependencies installed and the appropriate OS.

1

For my case, pip3 packages are installed to another Python version that I'm using in VS Code. Could you please execute this command in terminal:

pip3 show py-solc-x

And look for the folder. Here, my selected Python version was 3.10.2 in green box and I've changed it to 3.8.0 since my package is installed in there. For other IDE's or environments, try changing your Python version that you're executing on.

enter image description here

This is how I made python find solcx lib.

Fethi Tekyaygil
  • 355
  • 2
  • 15
0

You have two different python installed. Instead of python, use python3 to compile.

python3 file_name
blockByblock
  • 366
  • 2
  • 4
  • 14
0

VS Terminal on Windows 10

  1. Create a new file and write the following code:
import os, sys
print(sys.executable)
  1. run the python code and copy the printed file directory.
  2. copy the file in my case it was "C:\Users...\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.ex"
  3. write a new line in the terminal as "...PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.ex -m pip install py-solc-x"

This has solved the problem for me

rsc05
  • 3,626
  • 2
  • 36
  • 57
0

To actually solve this problem is by actually creating a python environment using pipenv after that install py-solc-x again

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '23 at 18:31