Is it possible to use a package function without loading the package?

In R, I would do something like this:

dt = data.table::fread("data.csv")

My understanding is that in Julia I would need to do:

using DataFrames; using CSV;
df = CSV.File("data.csv") |> DataFrame;

The code in Julia is relatively (very) slow vs R for various reason and I understand some people are looking at improving it, but that is not the topic of my question here. My question is:

is there a way to use CSV.File and DataFrame without loading the whole package (like in R) and not having to run using DataFrames; using CSV;?

Thank you.
(I just started Julia this week)

I don’t think so: to use definitions from a module you need to load its code and bring it into scope, which you do with using or import (see here for the differences between the two).

I guess the important questions are: what problem do you have with using the package? Why would you want to avoid it?

2 Likes

I think that R also quietly loads the package, eg ?"::" says that

The package namespace will be loaded if it was not loaded before the call

If you just care about namespace management, use something like

using DataFrames: DataFrame
import CSV

so that no other symbols end up in the namespace.

7 Likes

It is to avoid conflict with functions that have the same name which actually happens in R. In R you have the following:

> dt = data.table::fread("Downloads/dataset.csv")
> dt = fread("Downloads/dataset.csv")
Error in fread("Downloads/dataset.csv") : could not find function "fread"

Meaning fread is not in scope.
But I guess that if I use CSV.File and DataFrames.DataFrame I am making it clear which function from which package I am using.
Thank you for your answers (which I believe answer my question).

If you just do using DataFrames: DataFrames (note that this is the same name as the module - you’re quite literally only bringing the module name into scope, not the exported names), you can do DataFrames.DataFrame(...) to qualify which you mean.

2 Likes

Is there a difference between import DataFrames and using DataFrames: DataFrames?

No.

2 Likes