You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While Jupyter notebooks are a great way to get started with the language, eventually you will want to use more powerful tools. Visual Studio Code (VS Code) in particular, is the most popular open source editor for programming, and has a huge set of extensions and strong industry support.
28
28
29
-
While you can use source code control, run terminals/REPLs, without VS Code, we will concentrate on using it as a full IDE for all of these features.
29
+
While you can use source code control, run terminals and the REPL ("Read-Evaluate-Print Loop"), without VS Code, we will concentrate on using it as a full IDE for all of these features.
30
30
31
31
## Installing VS Code
32
32
@@ -73,7 +73,7 @@ A key feature of VS Code is that it can synchronize your extensions and settings
73
73
- Ensure you have a [GitHub account](https://github.com/), which will be useful for {doc}`further lectures <../software_engineering/version_control>`
74
74
- Choose `Turn On Settings Sync...` in the gear icon at the bottom of the screen
- You can stick with all of the defaults, and read more in the [instructions](https://code.visualstudio.com/docs/editor/settings-sync)
79
79
@@ -134,6 +134,7 @@ That code is also accessible within the REPL. Executing the function there,
134
134
Because the REPL and the files are synchronized, you can modify functions and simple choose `<shift-Enter>` to update their definitions before analyzing the results in the REPL or in the file itself.
135
135
136
136
137
+
(adding_packages)=
137
138
### Adding Packages
138
139
139
140
Next we will go through simple use of the plotting and package management.
@@ -152,174 +153,142 @@ Add code in the `.jl` file for a simple plot, and it will be shown on a separate
152
153
:width: 100%
153
154
```
154
155
155
-
** Note:** The If you use the Julia REPL within VS Code, then it will automaticallly activate that project file. Otherwise, you will have to start your REPL with the `julia --project` from within that folder, or you will need to manually call `using Pkg; Pkg.activate()` after launching Julia.
156
+
### Executing Files
156
157
157
-
(repl_main)=
158
-
## The REPL
159
-
Note that the REPL could also be started with `> Julia: Start REPL`, which should provide a prompt at the bottom of the window.
160
-
161
-
Previously, we discussed basic use of the Julia REPL ("Read-Evaluate-Print Loop").
162
-
163
-
Here, we'll consider some more advanced features.
164
-
165
-
### Shell Mode
158
+
First we will reorganize our file so that it is a set of functions with a call at the end rather than a script. Replace the code with
159
+
```{code-block} none
160
+
using Plots, Random
161
+
162
+
f(x) = x + 1
163
+
function plot_results()
164
+
x = 1:5
165
+
y = f.(x)
166
+
plot(x, y)
167
+
print(y)
168
+
end
169
+
170
+
# execute main function
171
+
plot_results()
172
+
```
166
173
167
-
Hitting `;` brings you into shell mode, which lets you run bash commands (PowerShell on Windows)
174
+
While it is fine to use scripts with code not organized in functions for exploration, you will want to organize any serious computations inside of a function. While this may not matter for every problem, this will let the compiler avoid global variables and highly optimize the results.
168
175
169
-
```{code-cell} julia
170
-
; pwd
176
+
```{note}
177
+
The behavior of global variables accessed in loops in the `REPL`, Debugger, inline code evaluations, and Jupyter notebooks is different from executed files. See the documentation on [soft scoping](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#On-Soft-Scope) for more background. A good heuristic which will avoid these issues is to (1) never use loops outside of a function when you write `.jl` files; and (2) if you ever use the `global` keyword, assume something is wrong and you should put something in a function.
171
178
```
172
179
173
-
You can also use Julia variables from shell mode
180
+
You can execute a `.jl` file in several ways.
174
181
175
-
```{code-cell} julia
176
-
x = 2
182
+
Within a terminal, you can provide the path to the file. For example,
183
+
```{code-block} none
184
+
julia --threads --project auto hello.jl
177
185
```
178
186
179
-
```{code-cell} julia
180
-
; echo $x
181
-
```
187
+
See the [REPL](repl_main) section for more details on the commandline options.
182
188
183
-
### Package Mode
189
+
Alternatively, within VS Code itself you can use the `<Ctrl-F5>` to run the new file.
184
190
185
-
Hitting `]` brings you into package mode.
186
191
187
-
*`] add Expectations` will add a package (here, `Expectations.jl`).
188
-
* Likewise, `] rm Expectations` will remove that package.
189
-
*`] st` will show you a snapshot of what you have installed.
190
-
*`] up` will (intelligently) upgrade versions of your packages.
191
-
*`] precompile` will precompile everything possible.
192
-
*`] build` will execute build scripts for all packages.
193
-
* Running `] preview` before a command (i.e., `] preview up`) will display the changes without executing.
192
+
(debugger)=
193
+
### Using the Debugger
194
194
195
-
You can get a full list of package mode commands by running
195
+
To debug your function, first click to the left of a line of code to create a breakpoint (seen as a red dot).
196
196
197
-
```{code-cell} julia
198
-
] ?
197
+
Next, use `<Ctrl-Shift-D>` or select the bug tab on the left in Julia to see
198
+
```{figure} /_static/figures/debugger_1.png
199
+
:width: 100%
199
200
```
200
201
201
-
On some operating systems (such as OSX) REPL pasting may not work for package mode, and you will need to access it in the standard way (i.e., hit `]` first and then run your commands).
202
-
203
-
### Help Mode
204
-
205
-
Hitting `?` will bring you into help mode.
206
-
207
-
The key use case is to find docstrings for functions and macros, e.g.
202
+
Then choose the `Run and Debug` option and it will execute `plot_results()` at the bottom of the file, and then stop inside at the breakpoint.
208
203
209
-
```{code-block} julia
210
-
? print
204
+
```{figure} /_static/figures/debugger_2.png
205
+
:width: 100%
211
206
```
212
207
213
-
Note that objects must be loaded for Julia to return their documentation, e.g.
208
+
You can use the toolbar at the top to step through the code, as described in the [documentation](https://www.julia-vscode.org/docs/dev/userguide/debugging/).
214
209
215
-
```{code-block} julia
216
-
? @test
217
-
```
218
210
219
-
will fail, but
211
+
As an alternative way to start the debugger, you can instead debug a call within the REPL with commands like `@run plot_results()`.
220
212
221
-
```{code-block} julia
222
-
using Test
213
+
```{note}
214
+
The Julia Debugger runs some of the code in an interpreted mode that might be far slower than typical compiled and optimized code. For this reason, it may not be possible to use it in all the same ways you might use something like the Matlab debugger. However, if you organize your code appropriately, you may find that the [compile mode](https://www.julia-vscode.org/docs/stable/userguide/debugging/#Compile-mode-1) enables you to concentrate on debugging only some of the functions, while letting slow functions remain compiled.
223
215
```
224
216
225
-
```{code-block} julia
226
-
? @test
227
-
```
228
-
229
-
will succeed.
230
-
231
-
232
-
(jl_packages)=
233
-
## Package Environments
234
217
235
-
Julia's package manager lets you set up Python-style "virtualenvs," or subsets of packages that draw from an underlying pool of assets on the machine.
218
+
(repl_main)=
219
+
## The REPL
220
+
Even if you are working primarily in `.jl` and/or Jupyer Notebooks in Julia, you will need to become comfortable with the REPL. We saw some initial use of this when [adding packages](adding_packages) and exploring the code above, but the REPL has many [more features](https://docs.julialang.org/en/v1/stdlib/REPL/#The-Julia-REPL).
236
221
237
-
This way, you can work with (and specify) the dependencies (i.e., required packages) for one project without worrying about impacts on other projects.
222
+
### Starting a REPL
223
+
There are several ways to start the REPL.
224
+
- Within VS Code, executing code inline will start it as required.
225
+
- In the command palette of VS Code, choose `> Julia: Start REPL`
226
+
- Outside of VS Code, if julia was installed on your system path, you can simply type `julia` or with other options
238
227
239
-
* An `environment` is a set of packages specified by a `Project.toml` (and optionally, a `Manifest.toml`).
240
-
* A `registry` is a git repository corresponding to a list of (typically) registered packages, from which Julia can pull (for more on git repositories, see {doc}`version control <../software_engineering/version_control>`).
241
-
* A `depot` is a directory, like `~/.julia`, which contains assets (compile caches, registries, package source directories, etc.).
228
+
The command line options for starting Julia are set to decent defaults for terminals running within VS Code, but you will want to set them yourself if starting the REPL otherwise.
242
229
243
-
Essentially, an environment is a dependency tree for a project, or a "frame of mind" for Julia's package manager.
230
+
One common choice is to choose the number of threads that Julia should have available. By default it is only a single thread, while `--threads auto` will tell Julia to create one for each processor on your machine. VS Code uses `--threads auto` by default.
244
231
245
-
* We can see the default (`v1.1`) environment as such
232
+
The most important choice is the `--project` toggle which determines whether you want to activate an existing project (or create a new one) when starting the interpreter. Since a key feature of Julia is to have fully reproducible environments, you will want to do this whenever possible.
246
233
247
-
```{code-cell} julia
248
-
] st
234
+
```{note}
235
+
A key difference between Julia and some other package managers is that it is capable of having different versions of each package for different projects - which ensures all projects are fully reproducible by you, your future self, and any other collaborators. While there is a global set of packages available (e.g. `IJulia.jl` to ensure Jupyter support) you should try to keep the packages in different projects separated. See the documentation on [environments](https://docs.julialang.org/en/v1/manual/code-loading/#Environments-1) and the [package manager](https://pkgdocs.julialang.org/v1/getting-started/) for more.
249
236
```
250
237
251
-
* We can also create and activate a new environment
238
+
If you start the terminal without activating a project, you can activate it afterwards with `] activate .` or `using Pkg; Pkg.activate()`.
252
239
253
-
```{code-cell} julia
254
-
] generate ExampleEnvironment
255
-
```
256
-
257
-
* And go to it
240
+
To see this in action, within an external terminal we will open it using both methods, and then use `] st` to see which project is activated and which packages are available.
258
241
259
-
```{code-cell} julia
260
-
; cd ExampleEnvironment
242
+
First, with `julia --threads auto` we see that the globally installed packages are available at first, but that the existing `Project.toml` and `Manifest.toml` in that folder are chosen after we choose `] activate .`
243
+
```{figure} /_static/figures/repl_1.png
244
+
:width: 100%
261
245
```
262
246
263
-
* To activate the directory, simply
264
-
265
-
```{code-cell} julia
266
-
] activate .
247
+
Next, with `julia --threads auto --project` the project is automatically activated
248
+
```{figure} /_static/figures/repl_1.png
249
+
:width: 100%
267
250
```
268
251
269
-
where "." stands in for the "present working directory".
270
-
271
-
* Let's make some changes to this
272
252
273
-
```{code-cell} julia
274
-
] add Expectations Parameters
275
-
```
253
+
Finally, if you choose the `--project` option in a folder which doesn't have an existing project file, it will create them as requierd.
276
254
277
-
Note the lack of commas
255
+
A few other features of the REPL include,
278
256
279
-
* To see the changes, simply open the `ExampleEnvironment` directory in an editor like Atom.
257
+
### More Features and Modes
280
258
281
-
The Project TOML should look something like this
259
+
Hitting `;` brings you into shell mode, which lets you run bash commands (PowerShell on Windows)
**Note**The TOML files are independent of the actual assets (which live in `~/.julia/packages`, `~/.julia/dev`, and `~/.julia/compiled`).
267
+
The key use case is to find docstrings for functions and macros, e.g.
301
268
302
-
You can think of the TOML as specifying demands for resources, which are supplied by the `~/.julia` user depot.
269
+
```{code-block} julia
270
+
? print
271
+
```
303
272
304
-
* To return to the default Julia environment, simply
305
273
306
-
```{code-cell} julia
307
-
] activate
308
-
```
274
+
(jl_packages)=
275
+
## Package Environments
309
276
310
-
without any arguments.
277
+
As discussed, the Julia package manager allowed you to create fully reproducible environments (in the same spirit as Python's and Conda virtual environments).
311
278
312
-
* Lastly, let's clean up
279
+
As we saw before, `]` brings you into package mode. Some of the key choices are
313
280
314
-
```{code-cell} julia
315
-
; cd ..
316
-
```
281
+
*`] instantiate` (or `using Pkg; Pkg.instantiate()` in the normal julia mode) will check if you have all of the packages and versions mentioned in the `Project.toml` and `Manifest.toml` files, and install as required.
282
+
- This feature will let you reproduce the entire environment and, if a `Manifest.toml` is available, the exact package versions used for a project. For example, these lecture notes use [Project.toml](https://github.com/QuantEcon/lecture-julia.notebooks/blob/main/Project.toml) and [Manifest.toml](https://github.com/QuantEcon/lecture-julia.notebooks/blob/main/Manifest.toml) - which you likely instantiated during installation after downloading these notebooks.
283
+
*`] add Expectations` will add a package (here, `Expectations.jl`) from the activated project file (or the global environment if none is activated)
284
+
* Likewise, `] rm Expectations` will remove that package.
285
+
*`] st` will show you a snapshot of what you have installed.
286
+
*`] up` will upgrade versions of your packages to the latest versions possible given the graph of compatibility used in each
317
287
318
-
```{code-cell} julia
319
-
; rm -rf ExampleEnvironment
288
+
```{note}
289
+
On some operating systems (such as OSX) REPL pasting may not work for package mode, and you will need to access it in the standard way (i.e., hit `]` first and then run your commands).
320
290
```
321
291
322
-
323
292
## More Options and Configuration Choices
324
293
325
294
VS Code and the related ecosystem have an enormous number of features and additional options.
@@ -340,11 +309,12 @@ While not required for these lectures, consider installing the following extensi
340
309
- Install [LaTeX Workshop](https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop) and (optionally) the [Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker) for spell checking
341
310
-
342
311
- To get started, just add magic comments at the top of the latex file (removing the `!BIB` line if there is no bibtex reference) and then `F5` or the equivalent command to compile:
343
-
```{code-cell} none
344
-
% !TEX program = pdflatex
345
-
% !BIB program = bibtex
346
-
% !TEX enableSynctex = true
347
-
```
312
+
313
+
```{code-block} none
314
+
% !TEX program = pdflatex
315
+
% !BIB program = bibtex
316
+
% !TEX enableSynctex = true
317
+
```
348
318
### Font Choices
349
319
350
320
Beyond their general use, the integrated terminals will use fonts installed within VS Code. Given that Julia code supports mathematical notation, the extra support in good fonts can be helpful.
@@ -360,4 +330,4 @@ If you ever need to use clusters or work with reproducible [containers](https://
360
330
-[Remote Extensions Pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack): Tools to access remote servers, local containers. Install [OpenSSH](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse) if required.
361
331
-[SFTP](https://marketplace.visualstudio.com/iems?itemName=liximomo.sftp): Secure copying of files to supported cloud services.
362
332
363
-
Windows users will find good support to access a local linux installation with the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10#simplified-installation-for-windows-insiders) and the associated [VS Code Extension](https://code.visualstudio.com/docs/remote/wsl-tutorial).
333
+
Windows users will find good support to access a local linux installation with the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) and the associated [VS Code Extension](https://code.visualstudio.com/docs/remote/wsl-tutorial).
0 commit comments