Book Image

Julia 1.0 Programming Cookbook

By : Bogumił Kamiński, Przemysław Szufel
Book Image

Julia 1.0 Programming Cookbook

By: Bogumił Kamiński, Przemysław Szufel

Overview of this book

Julia, with its dynamic nature and high-performance, provides comparatively minimal time for the development of computational models with easy-to-maintain computational code. This book will be your solution-based guide as it will take you through different programming aspects with Julia. Starting with the new features of Julia 1.0, each recipe addresses a specific problem, providing a solution and explaining how it works. You will work with the powerful Julia tools and data structures along with the most popular Julia packages. You will learn to create vectors, handle variables, and work with functions. You will be introduced to various recipes for numerical computing, distributed computing, and achieving high performance. You will see how to optimize data science programs with parallel computing and memory allocation. We will look into more advanced concepts such as metaprogramming and functional programming. Finally, you will learn how to tackle issues while working with databases and data processing, and will learn about on data science problems, data modeling, data analysis, data manipulation, parallel processing, and cloud computing with Julia. By the end of the book, you will have acquired the skills to work more effectively with your data
Table of Contents (18 chapters)
Title Page
Copyright and Credits
About Packt

How to customize Julia on startup

Julia has multiple parameters that can be used to tune its behavior during startup. In this recipe, we explain three ways in which you can change them:

  • Using command line options
  • Using scripts run at startup
  • Using environment variables

Also, we discuss several important and non-obvious use cases.

Getting ready

Before we begin, make sure that you have the Julia executable in your search path, as explained in the Installing Julia from binaries recipe. Also, create a hello.jlfile in your working directory, containing the following line:

println("Hello " * join(ARGS, ", "))

We will later run this file on Julia startup.


In the GitHub repository for this recipe, you will find the commands.txt file that contains the presented sequence of shell and Julia commands, the hello.jl file described above and the startup.jl file that is discussed in the recipe.

Now, open your favorite terminal to execute commands.

How to do it...

In this recipe, we will show various options for controlling how the julia process is started: running scripts on startup, running a single command, and configuring a startup script for Julia installation. They are discussed in the consecutive subsections.

Running a script on Julia startup

We want to run the hello.jl file on Julia startup while passing some switches and arguments to it.

In order to execute it, write the command following $ in your OS shell and press Enter:

$ julia -i hello.jl Al Bo Cyd
Hello Al, Bo, Cyd
   _       _ _(_)_    | Documentation:
  (_)     | (_) (_)   |
   _ _   _| |_  __ _  |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | |  Version 1.0.2 (2018-11-08)
 _/ |\__'_|_|_|\__'_| |  Official release
|__/                  |

Notice that we remain in the Julia command line after the script finishes its execution because we have passed through the -i switch. The Al, Bo, and Cyd arguments got passed as the ARGS variable to the printing command contained in the hello.jl file.

Now, press Ctrl + D or write exit() and press Enter to exit Julia and go back to the shell prompt.


Running a single Julia command on startup

If we just want to run a single command, we can use the -e switch.

Write the command following $ in your shell:

$ julia -e "println(factorial(10))"

In this example code, we have calculated 10! and printed the result to standard output. Notice that Julia exited immediately back to the shell because we did not pass the -i switch.

Running a script every time Julia starts

If there are commands that you repeatedly pass to Julia after startup, you can automate this process by creating a startup file.

First, put the following statements in the ~/.julia/config/startup.jl file, using your favorite editor:

using Random
println("Setup successful")

Now, start Julia from the OS shell:

$ julia --banner=no
Setup successful
julia> RandomDevice()


We can see that the startup script was automatically executed as it has printed a greeting message and we have access to the RandomDevice constructor that is defined in the Random module. Without the specifier using Random, trying to execute the RandomDevice() expression in the Julia command line would throw an UndefVarError exception. Additionally, we used the --banner=no switch to suppress printing the Julia banner on startup.


How it works...

There are four methods you can use to parameterize the booting of Julia:

  • By setting startup switches
  • By passing a startup file
  • By defining the ~/.julia/config/startup.jl file
  • By setting environment variables

The general structure of running Julia from the command line is this:

$ julia [switches] -- [programfile] [args...]

The simplest way to control Julia startup is to pass switches to it. You can get the full list of switches by running the following in the console:

$ julia --help

In the previous examples, we have used three switches: -i, -e, and--banner=no.

The first of these switches (-i) keeps Julia in interactive mode after executing the commands. If we did not use the -i switch then Julia would terminate immediately after finishing the passed commands.

You can pass Julia a file to execute directly or a command to run following the-eswitch. The latter option is useful if you want to execute a short expression. However, if we want to start some commands repeatedly every time Julia starts, we can save them in the ~/.julia/config/startup.jl file. It will be run every time Julia starts unless you set the --startup-file=no command line switch.

In the preceding path, ~ is your home directory. Under Linux, this should simply work. Under Windows, you can check its location by running homedir() in the Julia command line (typically, it will be theC:\Users\[username]directory).

What are some useful things to put into ~/.julia/config/startup.jl?

In the recipe, we put the using Random statement in ~/.julia/config/startup.jl to load the Random package by default, since we routinely need it in our daily work. The second command, ENV["JULIA_EDITOR"]="vim", specifies a default editor used by Julia. Use of the editor in Julia is explained in the Useful options for interaction with Julia recipe.


There's more...

The full list of environment variables that are scanned by Julia is described at They can all be set in the shell and then are read by Julia. In Julia, environment variables can be accessed and changed via the ENV dictionary.

See also

  • The use of an external editor in Julia is described in the Useful options for interaction with Julia recipe.
  • An explanation of how to work with packages is given in the Managing packages recipe.
  • A more advanced topic is passing commands on startup to multiple processes. It is described in the Setting up Julia to use multiple cores recipe.