# How to avoid function name conflict without using the package identifier every time

**URL:** <https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186>\
**Category:** General Usage\
**Created:** [March 21, 2022, 2:31am UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186 "2022-03-21T02:31:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubaiyat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubaiyat/32/27423_2.png) [@rubaiyat](https://discourse.julialang.org/u/rubaiyat)\
**Post date:** [March 21, 2022, 2:31am UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/1 "2022-03-21T02:31:23Z")

</div>

Hello everyone,

I was wondering if there was a way to avoid conflicts with a function name between packages without using the package identifier every time. To explain what I mean, when I write the following:

```julia
using DataFramesMeta
using ShiftedArrays

rev_df = DataFrame(firm_id = ["A", "A", "A", "A", "B", "B", "B", "B"] , 
	rev = [100, 200, 300, 400, 50, 67, 75, 90],
	yr = [2001,2002,2003,2004,2001,2002,2003,2004])

using IterTools

new_rev_df = @chain rev_df begin 
	groupby(:firm_id)
	@transform :rev_lag = lag(:rev)
end

```

I get the output:

```julia
julia> new_rev_df = @chain rev_df begin 
           groupby(:firm_id)
           @transform :rev_lag = lag(:rev)
       end
WARNING: both IterTools and DataFramesMeta export "groupby"; uses of it in module Main must be qualified
ERROR: UndefVarError: groupby not defined
Stacktrace:
 [1] top-level scope
   @ REPL[5]:2

```

Now this can be resolved by the following:

```julia
new_rev_lag = @chain rev_df begin 
	DataFramesMeta.groupby(:firm_id)
	@transform :rev_lag = lag(:rev)
end

```

This works because I’ve specified before groupby that I’m calling the function from DataFramesMeta.

My issue is, in my code I have to use `IterTools` just once, whereas I have to use `DataFramesMeta`’s groupby() function many, many times. So having to write `DataFramesMeta.groupby()` every time I want to do a grouping actually ends up being quite tedious.

Is there a way around this? The simplest I could think of was to load `IterTools` when I need it, then “unload” the package so that there’s no conflict when the `DataFramesMeta's groupby()` function needs to be used. As far as I can tell, Julia doesn’t let the user unload a package like this, although I might be missing something.

I’d appreciate some sort of workaround that anyone is aware of. Thanks!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [March 21, 2022, 5:43am UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/2 "2022-03-21T05:43:30Z")

</div>

You could also import only the one method from `IterTools` that you need, with something like

```julia
using DataFramesMeta
using IterTools: chain

```

See [Modules · The Julia Language](https://docs.julialang.org/en/v1/manual/modules/#using-and-import-with-specific-identifiers,-and-adding-methods) for why that works

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 21, 2022, 7:28am UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/3 "2022-03-21T07:28:19Z")

</div>

Putting a line like this should help:

```julia
const groupby = DataFramesMeta.groupby

```

  

Here is a completely reproducible example which does not require 3rd-party packages:

```julia
julia> module Foo
       export groupby
       groupby() = "Foo.groupby"
       end
Main.Foo

julia> module Bar
       export groupby
       groupby() = "Bar.groupby"
       end
Main.Bar

julia> using .Foo

julia> using .Bar

julia> groupby()
WARNING: both Bar and Foo export "groupby"; uses of it in module Main must be qualified
ERROR: UndefVarError: groupby not defined
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

```

```julia
julia> const groupby = Bar.groupby
groupby (generic function with 1 method)

julia> groupby()
"Bar.groupby"

```

---

<div class="post-metadata">

**Author:** ![rubaiyat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubaiyat/32/27423_2.png) [@rubaiyat](https://discourse.julialang.org/u/rubaiyat)\
**Post date:** [March 23, 2022, 4:17pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/4 "2022-03-23T16:17:07Z")

</div>

This works, thanks.

EDITED: To be accurate, I need to do this:

```julia
using IterTools: product
product(rev_df.rev, rev_df.yr)

```

If instead I did this:

```julia
using IterTools: product 
IterTools.product(rev_df.rev, rev_df.yr)

```

then I get the following error:

```julia
julia> IterTools.product(rev_df.rev, rev_df.yr)
ERROR: UndefVarError: IterTools not defined
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

```

In both of the above cases the conflict with DataFramesMeta.groupby goes away, but in the former case I can use IterTools.product() as well.

---

<div class="post-metadata">

**Author:** ![rubaiyat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubaiyat/32/27423_2.png) [@rubaiyat](https://discourse.julialang.org/u/rubaiyat)\
**Post date:** [March 23, 2022, 4:20pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/5 "2022-03-23T16:20:56Z")

</div>

This works as well, thanks!

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [March 23, 2022, 4:28pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/6 "2022-03-23T16:28:51Z")

</div>

There is the `import X as Y` syntax, personally I think this is a great but underused feature. You could replace `using IterTools` by `import IterTools as IT` and then use `IT.product` etc.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 23, 2022, 4:37pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/7 "2022-03-23T16:37:26Z")

</div>

Which version of Julia introduced this? I believe 1.5 and below do not support it.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 23, 2022, 4:38pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/8 "2022-03-23T16:38:12Z")

</div>

The `using X: y` has imo some unintuitive behavior. `using DataFrames: groupby` brings `groupby` into `Main` but _not_ `DataFrames`. This is a useful stopgap is module names conflict with other names.

```julia
julia> using DataFrames: groupby;

julia> groupby
groupby (generic function with 1 method)

julia> DataFrames
ERROR: UndefVarError: DataFrames not defined

```

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [March 23, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/9 "2022-03-23T19:01:58Z")

</div>

I think so. For older julia versions I recommend the pattern `import IterTools; const IT=IterTools`.

---

<div class="post-metadata">

**Author:** ![rubaiyat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rubaiyat/32/27423_2.png) [@rubaiyat](https://discourse.julialang.org/u/rubaiyat)\
**Post date:** [March 23, 2022, 7:29pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/10 "2022-03-23T19:29:32Z")

</div>

Ah okay makes sense. That’s why IterTools.product() was throwing an error about how IterTools wasn’t defined, but using product() worked fine.

---

<div class="post-metadata">

**Author:** ![mikkoku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikkoku/32/16274_2.png) [@mikkoku](https://discourse.julialang.org/u/mikkoku)\
**Post date:** [March 24, 2022, 5:32pm UTC](https://discourse.julialang.org/t/how-to-avoid-function-name-conflict-without-using-the-package-identifier-every-time/78186/12 "2022-03-24T17:32:14Z")

</div>

This should also work

```julia
using DataFrameMeta
groupby
using IterTools

```

Now `groupby` is the one from `DataFramesMeta`.
