I’m working on a project at the moment that involves many packages and modules, as well as many different files. I have a “setup.jl” file that adds all the packages (
Pkg.add("Pkg_name"); using Pkg_name;) and includes all the files (
include("./filename")). That way, I can simply start Julia and run the command
include("setup.jl") and I can use all of the code.
I however don’t like how I import packages. Essentially, I am using a try-catch system, where I try
using Pkg_name and the catch is
Pkg.add("Pkg_name"); using Pkg_name. I would like to call a function which would do that for me. That way, I wouldn’t have to write the try-catch for every package and I could simply write a for loop that goes through all the packages and imports them. The problem is with “using” because it uses a package identifier and I’m not sure how to convert a string (the package name) into a package identifier. I tried
using $Pkg_name, as well as
using Symbol(Pkg_name) but both do not work. Here is what I have in mind in a nutshell:
# Function to import packages function use_pkg( pkg_name ) try using pkg_name; catch err; Pkg.add("pkg_name"); using pkg_name; end end # Defining list of packages to import pkg_list = ["CSV", "Random", "Plots"]; # There would be more packages of course # Importing all packages for i = 1:length( pkg_list ) if !in( pkg_list[i], keys( Pkg.installed( ) ) ) use_pkg( pkg_list[i] ); end end
Does anyone have any idea on how to do this?
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
OS: Windows (x86_64-w64-mingw32)
CPU: Intel® Core™ i7-4710HQ CPU @ 2.50GHz
LLVM: libLLVM-6.0.0 (ORCJIT, haswell)