5/5
## Using the Vyper compiler as a package In this lesson, we will learn how to use the Vyper compiler as a package. We begin by opening a file named `deploy_favorites.py` and then edit the `print` statement to read: ```python print("Let's read in the Vyper code and deploy it!") ``` We then proceed to open our `favorites.vy` file, which contains Vyper code, in read-only mode by using the following code: ```python with open("favorites.vy", "r") as favorites_file: favorites_code = favorites_file.read() ``` We then need to compile the Vyper code. We've already installed Vyper and can use it in our project. To do so, we must first add Vyper as a dependency to our project by running the following command in our terminal: ```bash uv add vyper ``` We then need to synchronize our dependencies to our project. We can do this by typing the following command in our terminal: ```bash uv sync ``` To call the Vyper compiler and compile our Vyper code, we can run the following code: ```python from vyper import compile_code compilation_details = compile_code(favorites_code, output_formats=["bytecode"]) ``` We have successfully compiled our Vyper contract and stored the compilation output in a variable called `compilation_details`. We can then print the compilation details by adding the following line of code: ```python print(compilation_details) ``` We can run our Python script in our terminal with the following command: ```bash python deploy_favorites.py ``` We'll see the output of the compilation details including the bytecode section. This bytecode section will include the same hex codes we saw when running Vyper from the command line.
A practical guide to using the Vyper compiler as a Python package. This lesson walks you through installing the Vyper package as a dependency, importing and using the compile_code function, and reading Vyper code from a file.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 6, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 6, 2025