Parseerror importing OpenZeppelin contracts in Pycharm

Hey there,

I use this python web3.py code to compile my solidity contract, but I get a parseerror "raise SolcError(
solcx.exceptions.SolcError: ParserError: Source "node_modules/openzeppelin/contracts/token/ERC20/ERC20.sol" not found:". Funnily, it used to always work perfectly and from one day to the other it doesnt...

I am using Pycharm. Please see project folder structure and my solidity contract below. I had to change the solidity compiler to 19 so that I was able to deploy on ganache.
The reason I try to do it with web3.py is because I want to learn the foundations instead of just jumping to a framework like brownie or foundry. As I'm a python dev Id love to make it happen with python, but seems like I have to change to Foundry, as Ape is too new still and brownie has been ended.

Steps I took:

  • I deleted the node_modules and reimported it
  • I even created a totally new pycharm project
  • I updated my pycharm, closed it, opened it.
  • I googled and the only solution I got was using juan blanco's solidity extension for vscode, but as I use pycharm, its not a help.
  • I checked for the ERC20.sol contract in my nodemodules and I have it in my directory.

My script "compile_contracts_v2.py":

# * Goal of the script: compile my sol contracts and saved them as json
# * I need these json files to then interact with the ganache blockchain

import json
from solcx import compile_standard, compile_files, install_solc, set_solc_version
import os

# Install specific version of solc
install_solc('0.8.19')
set_solc_version('0.8.19')

# * collin's approach to get byte code and abi

#read solidity file -> works
with open("../../contracts/MockDAI.sol", "r") as file:
    mockDAI_file = file.read()

#does not work because of the open zeppelin bullshit import...

compiled_sol = compile_standard({
        "language": "Solidity",
        "sources": {"MockDAI.sol": {"content": mockDAI_file}},
        "settings": {
            "outputSelection": {
                "*": {"*": ["abi", "metadata", "evm.bytecode","evm.sourceMap"]}
            }
        }
    },
       solc_version='0.8.19',
    )

print(compiled_sol)

my project folder structure:

project-root/
├── contracts/
│   └── MockDAI.sol
├── node_modules/
│   └── @openzeppelin/
│       └── contracts/
│           └── token/
│               └── ERC20/
│                   └── ERC20.sol
├── scripts/
│   └── ganache_deployment_testing/
│       └── compile_contracts_v2.py
├── package.json
└── package-lock.json

my solidity contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "../node_modules/openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MockDAI is ERC20 {
    constructor() ERC20("Mock DAI", "DAI") {
        _mint(msg.sender, 1000000 * 10 ** decimals());
    }
}

Thanks a lot in advance!!

1 Like