'ImportError: No module named solcx

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)


Solution 1:[1]

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 

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

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.

Solution 5:[5]

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

python3 file_name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joel John
Solution 2 Dharman
Solution 3 Fethi Tekyaygil
Solution 4
Solution 5 blockByblock