5/5
## Defining the Compiler Version in Vyper with Pragma Before writing any functional code in a Vyper smart contract, the very first step is to declare the specific compiler version the contract is intended for. This is a mandatory requirement that ensures your code is processed correctly, utilizing the features and syntax rules associated with that particular version. Specifying the compiler version guarantees compatibility and predictable behavior, preventing potential compilation errors or unexpected runtime issues that could arise from using an incompatible version. Different versions of the Vyper compiler may introduce new features, change existing syntax, or contain important bug fixes. By explicitly stating the version, you instruct the compiler (like the one integrated into the Remix IDE) on exactly how to interpret and compile your `.vy` file. **The Standard Declaration** For this course, and as a generally recommended practice for clarity, we will use the following syntax to declare the Vyper compiler version: ```vyper # pragma version 0.4.0 ``` Let's break down this line: * `#`: In Vyper, similar to Python, the hash symbol signifies the beginning of a comment. * `pragma version`: This specific phrase within a comment acts as a special directive. The Vyper compiler recognizes it as an instruction rather than just an explanatory note. * `0.4.0`: This is the precise version number of the Vyper compiler that the contract code is written for. This directive tells the compiler that the code *must* be compiled using exactly version `0.4.0`. Compilation will fail if attempted with any other version, such as `0.3.10` or `0.4.1`. We will primarily use version `0.4.0` throughout this course because it includes several new and useful features that we will explore. Be aware that much of the existing Vyper code found in the wild might use older versions (like `0.3.10`). The code developed in this course, leveraging `0.4.0` features, will likely not be compatible with these older versions. **Alternative Syntaxes** While `# pragma version 0.4.0` is the preferred method for this course and production code, Vyper does recognize a few alternative syntaxes for declaring the version: 1. **Omitting `pragma`:** ```vyper # version 0.4.0 ``` 2. **Using the Caret (`^`) for Version Range:** ```vyper # pragma version ^0.4.0 ``` The caret `^` generally indicates compatibility with versions greater than or equal to `0.4.0` but less than the next minor version (`0.5.0`). This allows compilation with patch releases (e.g., `0.4.1`) but not minor or major updates. While valid, using version ranges is generally discouraged for production smart contracts. 3. **Using the `@` symbol:** ```vyper @version 0.4.0 ``` This is another recognized format, but sticking to the `# pragma version` syntax is recommended for consistency. **Best Practice for Production Code** When developing smart contracts intended for deployment (production code), it is strongly advised to specify an *exact* compiler version without using the caret (`^`): ```vyper # pragma version 0.4.0 ``` Locking the version ensures that the contract compiles and behaves precisely as it was tested. Using version ranges introduces the risk that the contract might later be compiled with a newer patch version containing subtle, untested changes or even regressions. Specifying the exact version maximizes predictability and security for your deployed smart contracts.
A foundational overview to Defining the Compiler Version in Vyper with Pragma - Learn the mandatory first step in Vyper: declaring the compiler version using `# pragma`. Understand its importance, syntax variations, and why specifying an exact version is best practice for production.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Using 0.4.1 of Vyper
Last updated on April 21, 2025
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with 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 11, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with 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 11, 2025