# How to deal with name conflict between functions from different packages?

**URL:** <https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [January 15, 2024, 9:38am UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820 "2024-01-15T09:38:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [January 15, 2024, 9:38am UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/1 "2024-01-15T09:38:58Z")

</div>

Packages `A` and `B` contain functions with different names such as `func_of_A` and `func_of_B` respectively, and functions with the same name such as `func_in_both`.

At first, I only needed to use the function `func_in_both` of package `A`, so I wrote the following code in the julia script:

```julia
using A

func_in_both()

```

Later, I needed to use function `func_of_B`, so I added two lines of code to the script:

```julia
using A
using B

func_in_both()
func_of_B()

```

Then came the trouble. The previous function `func_in_both` now become invalid because of a name conflict.

I know now I can change the first function call to `A.func_in_both()`, but what if I have called many functions from different packages with conflicting names with functions in `B` for many times (i.e., package `B` contains many functions that have conflicting names with previously called functions from other packages), and I can’t remember which package each one came from?

If I have to debug previous code from time to time while writing later code, It would hardly be a euphoric programming experience.

Is this due to my improper workflow with Julia (I often suspect that my workflow is not optimal)? Or is there any other measure to deal with this issue?

Thanks for attention!

---

<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:** [January 15, 2024, 9:44am UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/2 "2024-01-15T09:44:31Z")

</div>

Code that starts with

```julia
using Package

```

is frequent in the Julia ecosystem (and even the docs), but it’s a bad practice because you may forget where the name comes from. That’s why I usually follow [BlueStyle](https://github.com/invenia/BlueStyle#module-imports), which recommends this instead:

```julia
using Package: func

```

If you apply that to an existing package, it will throw many errors, but at least that way you can debug them one by one until all the names are properly qualified.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 15, 2024, 10:14am UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/3 "2024-01-15T10:14:46Z")

</div>

I prefer `import Package as P` (where `P` is an arbitrary abbreviated name).

Then I use `const` to assign further shorthands:

```julia
import Package as P
const f = P.full_function_name
const g = ...

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 15, 2024, 10:18am UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/4 "2024-01-15T10:18:57Z")

</div>

If your packages share many names for separate functions (as opposed to contributing methods to shared functions), that is a rare and legitimate headache. In this case, you cannot rely on bare `using .A` to access export lists. You should instead start importing specified names, with either `using .A: x` or `import .A: x`. Those aren’t very similar to bare `using`/`import`, just think of them as only importing the names to the right of the `:`. Note that the name `A` is not automatically imported either, you’d have to specify it on the right if it didn’t already exist via a `module` block. A couple differences between `using` and `import` with specified names: 1) `import` lets you add methods to imported functions without qualifying the module name, 2) `import` lets the names show up in `names(A; imported=true)`. To resolve conflicting names, you can qualify them with the module name or rename them for the import, whichever is neater. Note that unlike assignments, renamed imports don’t behave as separate variables; the original name and new name will always bind to the same instance, no matter which is reassigned directly.

```julia
julia> module A export x; x=0 end # creates module name so no need to import
Main.A

julia> module B export x; x=1 end
Main.B

julia> using .A, .B # bad

julia> x
WARNING: both B and A export "x"; uses of it in module Main must be qualified
ERROR: UndefVarError: `x` not defined

julia> A.x, B.x # if you prefer to specify only the module names
(0, 1)

julia> using .A: x as Ax # if you prefer renaming specified names

julia> using .B: x as Bx

julia> Ax, Bx
(0, 1)

julia> A2x = Ax # assign new variable
0

julia> setglobal!(A, :x, 5) # reassign original name in its module
5

julia> A.x, Ax, A2x # rename was reassigned too, new variable was not
(5, 5, 0)

```

Since it sounds like you’re struggling with far more than the occasional name conflict, you can check overlap between 2 modules’ export lists:

```julia
julia> intersect(names(A), names(B))
1-element Vector{Symbol}:
 :x

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 15, 2024, 11:12am UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/5 "2024-01-15T11:12:21Z")

</div>

> [@gdalle](#):
>
> but it’s a bad practice because you may forget where the name comes from

That’s one reason why it’s bad but the major problem is really that your code can break due to name conflicts after updating dependencies, as outlined in the original post.

A bare `using` is fine for exploratory REPL work and throwaway scripts but it has no place in libraries and other long-term code.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [January 15, 2024, 3:59pm UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/6 "2024-01-15T15:59:46Z")

</div>

Thank you! Now I find that it may take a little more effort to use Julia gracefully. 🤔

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [January 15, 2024, 4:05pm UTC](https://discourse.julialang.org/t/how-to-deal-with-name-conflict-between-functions-from-different-packages/108820/7 "2024-01-15T16:05:35Z")

</div>

Thank you for your detailed answer! 🤝 🤝
