Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Julia 1.0 Programming Cookbook

You're reading from  Julia 1.0 Programming Cookbook

Product type Book
Published in Nov 2018
Publisher Packt
ISBN-13 9781788998369
Pages 460 pages
Edition 1st Edition
Languages
Authors (2):
Bogumił Kamiński Bogumił Kamiński
Profile icon Bogumił Kamiński
Przemysław Szufel Przemysław Szufel
Profile icon Przemysław Szufel
View More author details
Toc

Table of Contents (18) Chapters close

Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Preface
1. Installing and Setting Up Julia 2. Data Structures and Algorithms 3. Data Engineering in Julia 4. Numerical Computing with Julia 5. Variables, Types, and Functions 6. Metaprogramming and Advanced Typing 7. Handling Analytical Data 8. Julia Workflow 9. Data Science 10. Distributed Computing 1. Other Books You May Enjoy Index

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.

Note

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: https://docs.julialang.org
  (_)     | (_) (_)   |
   _ _   _| |_  __ _  |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | |  Version 1.0.2 (2018-11-08)
 _/ |\__'_|_|_|\__'_| |  Official https://julialang.org/ release
|__/                  |
julia>

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))"
3628800
$

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
ENV["JULIA_EDITOR"] = "vim"
println("Setup successful")

Now, start Julia from the OS shell:

$ julia --banner=no
Setup successful
julia> RandomDevice()
RandomDevice(UInt128[0x000000000000000000000000153e4040])

julia>

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 https://docs.julialang.org/en/v1.0/manual/environment-variables/. 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.
You have been reading a chapter from
Julia 1.0 Programming Cookbook
Published in: Nov 2018 Publisher: Packt ISBN-13: 9781788998369
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}