I want to call some existing code (written in Python) inside my Ethereum smart contract (written in solidity). Is it possible to do that? if so, how can we do it?

  • No, it is not possible. More generally, it is not possible to execute anything not located on the blockchain (either in your contract or in other contracts). So you'll need to copy that Python code into your contract, and then implement a compiler which will be able to take a mixture of Solidity and Python and turn it into valid EVM bytecode. Of course, the Python part of your code will still not be able to do anything "outside the blockchain" (for example, read some file on your local machine, or fetch data from some web server). Commented Dec 31, 2019 at 8:37
  • So your best alternative is to simply port your Python code to Solidity. You may wanna post here first what it is that you want to do (in Python), and someone might be able to point you to an already existing Solidity implementation of that. Commented Dec 31, 2019 at 8:40

2 Answers 2


EVM is isolated, which means there is no I/O to the outside world. However, you could program it in the way that your python code reacts on events published on-chain. This will not give you the ACID properties of a on-chain transaction, but it can be a valid solution depending on the usecase.


The EVM is internally consistent, deterministic and replicable, now and in the future. Its only inputs are signed transactions. These include both messages from users and transaction ordering blocks from miners.

A contract cannot inspect an external source since there is no way to confirm the correctness of such a step in the future. That is, there is no way to use an API to get data because there is no way construct such a thing with assurance that nodes will be able to replicate and confirm the data in the future.

A solution to that problem is an Oracle which is a user that will periodically sign an input. Instead of consulting an external source, the contract consults the Oracle's input. Oracles can be privileged users in a case-specific design or general-purpose relays that consult sources and report with a callback process, e.g. Oraclize.

Oracles push data into contracts, which is opposite flow versus contracts consulting something external.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.