Create a Project Environment
Overview
A project environment isolates your project's packages from the global Julia installation, preventing version conflicts.
Before setting up your workspace in VS Code, you should have already installed VS Code and the Julia extension. If you have not done so, please see the following link for instructions: Setting up VS Code.
To work with ENERGY 2100 in VS Code, you will need to: 1) activate the model's Julia environment and 2) load ENERGY 2100's package and global structures by following the instructions below:
Activate the Model's Julia Environment
To run a policy file in the Julia REPL, follow the instructions below.
Step 1 - Open the ENERGY 2100 model folder in VS Code (File\Open Folder).
- Start the Julia REPL from the Command Palette:
- Open the Command Palette (View\Command Palette) or (Ctrl+Shift+P)
- Start the Julia REPL (type Julia: Start REPL).
- Activate the folder as a Julia project environment and install project dependencies (which are specified in the model's Project.toml and Manifest.toml):
|
julia> using Pkg julia> Pkg.activate("C:\\ModelFolderName") julia> Pkg.instantiate() |
Alternatively, you could use Pkg.activate(".") to activate the current project if you have opened VS code in the model's root folder:
|
julia> using Pkg julia> Pkg.activate(".") julia> Pkg.instantiate() |
|
|
|
You must use two backslashes (\\) in the folder path for Julia to find the correct path. |
Verify that you activated the correct Julia environment by checking the package status:
|
julia> Pkg.status() |
If the correct environment was activated, this command will return "EnergyModel". Output should look something like:
|
Project EnergyModel v0.1.0 Status `C:\2020CanadaPoplarNH3\Project.toml` [336ed68f] CSV v0.10.16 [a93c6f00] DataFrames v1.8.1 [1313f7d8] DataFramesMeta v0.15.6 [864edb3b] DataStructures v0.19.3 [f67ccb44] HDF5 v0.17.2 [0f8b85d8] JSON3 v1.14.3 [98e50ef6] JuliaFormatter v2.3.0 [92481ed7] LinearRegression v0.2.1 [e6f89c97] LoggingExtras v1.2.0 [98105f81] LoggingFormats v1.6.0 [1914dd2f] MacroTools v0.5.16 ⌅ [aea7be01] PrecompileTools v1.2.1 [21216c6a] Preferences v1.5.2 [54e16d92] PrettyPrinting v0.4.2 [2230646a] QuickMenus v0.1.1 [ae029012] Requires v1.3.1 ⌃ [295af30f] Revise v3.13.2 ⌃ [90137ffa] StaticArrays v1.9.17 ⌃ [0c614874] TerminalPager v0.6.10 [a759f4b9] TimerOutputs v0.5.29 [a5390f91] ZipFile v0.10.1 [ade2ca70] Dates v1.11.0 [8ba89e20] Distributed v1.11.0 [b77e0a4c] InteractiveUtils v1.11.0 [76f85450] LibGit2 v1.11.0 [56ddb016] Logging v1.11.0 [de0858da] Printf v1.11.0 [3fa0cd96] REPL v1.11.0 [a4e569a6] Tar v1.10.0 [8dfed614] Test v1.11.0 [3f19e933] p7zip_jll v17.4.0+2 |
By activating the Julia environment, Julia finds the Project.toml and Manifest.toml files that define the scope and packages of the current project, and tells Julia to look for packages in the local folder rather than the default global environment.
Activating a Julia environment in VS Code means setting a specific project folder as the active project for the Julia REPL and language server. This ensures that using or import commands load the exact versions of packages specified in that project's Project.toml and Manifest.toml files, ensuring reproducibility and avoiding conflicts.
The Julia REPL will automatically open in the terminal, and you should see a julia> prompt.
|
|
🛈 |
If you ever need to re-open the Julia REPL in an activated environment, open the Command Palette and type Julia: Start REPL. |
|
|
🛈 |
Alternative ways to activate a Julia environment: click the "Julia env" button on the bottom-left status bar in VS Code, or type ] activate . in the Julia REPL to activate the current directory. When you open a folder containing a Project.toml file in VS Code, the Julia extension will often activate it automatically. |
Now that you have successfully activated the model's environment and packages, you are ready to load the package and global structures you want to use into VS Code's workspace.
Load ENERGY 2100 Package and Global Structures into Workspace
To work with the model in VS Code, you need to explicitly bring the model's package and global structures you want to use into the workspace.
To prepare VS Code to run policies and view variables, follow the instructions below:
- Load ENERGY 2100's Julia package, named EnergyModel:
|
using EnergyModel |
|
|
🛈 |
Alternatively, you could import the EnergyModel using an alias, such as "M". Example: julia> import EnergyModel as M |
- Import ENERGY 2100's modules, constants, or functions you want to use in the REPL.
The structures listed below are common imports required to run a policy file and view variables:
|
import EnergyModel.DB import EnergyModel.ReadDisk import EnergyModel.Select import EnergyModel.ReadDiskAndSets import EnergyModel.Yr |
Alternatively, we could import all these structures in one line using the following:
import EnergyModel: DB, ReadDisk, Select, ReadDiskAndSets, Yr
|
|
🛈 |
Click here for a more complete list of possible modules, constants, and functions that are available to import from the EnergyModel project environment. |
- Load additional packages as desired.
For viewing variables in an easy-to-read format and writing variables to CSV files, load the following:
|
using DataFrames, DataFramesMeta, CSV |
Use Model data, Variables, and Functions
All data, variables, and functions defined in EnergyModel are now available to use in the REPL. You are now able to test policy files and view variables.