# How to find out the version of a package from its module

**URL:** <https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755>\
**Category:** General Usage\
**Created:** [April 17, 2020, 11:43am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755 "2020-04-17T11:43:38Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![fingolfin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fingolfin/32/6033_2.png) [@fingolfin](https://discourse.julialang.org/u/fingolfin)\
**Post date:** [April 17, 2020, 11:43am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/1 "2020-04-17T11:43:38Z")

</div>

Suppose I just did `using Foo`. How do I find out the version of `Foo.jl` given the module `Foo` ?

The only way I found was to know the UUID of the package and query Pkg. I am hoping I am just overlooking something simple? Ideally I’d be hoping for something like `Foo.version` or so.

Motivation: I’d like to keep my code compatible with both CxxWrap 0.9.1 and 0.10.1, but there are some differences I need to deal with, no matter what. So far, all ways to query the version I found require using Pkg.jl, the UUID, _and_ different code depending on whether one is using Julia 1.3 or 1.4 (or newer).

Any pointers appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 17, 2020, 11:57am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/2 "2020-04-17T11:57:29Z")

</div>

The only way to get the version of the _currently loaded package_ (as opposed to what’s in the active environment) is something like

```julia
julia> pkgversion(m::Module) = Pkg.TOML.parsefile(joinpath(dirname(string(first(methods(m.eval)).file)), "..", "Project.toml"))["version"]
pkgversion (generic function with 1 method)

julia> pkgversion(Atom)
"0.12.10"

```

Needs some error handling, of course. Also hits the file system, but I don’t think there’s a way around that anyways.

---

<div class="post-metadata">

**Author:** ![fingolfin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fingolfin/32/6033_2.png) [@fingolfin](https://discourse.julialang.org/u/fingolfin)\
**Post date:** [April 17, 2020, 12:18pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/3 "2020-04-17T12:18:48Z")

</div>

> [@pfitzseb](#):
>
> pkgversion(m::Module) = Pkg.TOML.parsefile(joinpath(dirname(string(first(methods(m.eval)).file)), “…”, “Project.toml”))[“version”]

First of, thanks for the quick and helpful reply. That is actually a bit nicer than what I have. I wonder why something like that isn’t part of Pkg.jl.

But actually, I was wondering why `Pkg.jl` doesn’t just inject the relevant info into the module when loading it? It could simply insert `Foo.version` (well, that probably would be a bad idea as package may already have a symbol `version` on the toplevel; but it could also store a dictionary with this info and then use that to implement a function that allows querying the version (or even a full `PackageSpec`) for the currently loaded version of a package. Surely it must have all this information at some point

I realize such functionality this is not in there, but it seems like a sensible feature request to me, isn’t it? I can submit it as an issue to Pkg.jl I guess? I’d even work on a PR for it, but I’d first like to know if the idea has any chance at all 😉

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 18, 2020, 8:11am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/4 "2020-04-18T08:11:44Z")

</div>

> [@fingolfin](#):
>
> it seems like a sensible feature request

I am not sure about this. Pkg and code loading work in a _declarative_ way: you specify what you need (packages and their compat bounds), the resolver takes care of it. Any alternative gets chaotic very quickly.

As for your specific problem, it is a common workaround to test for the presence of various functions/types/values/etc in other modules, and condition code on this, eg with `isdefined`. That said, the cleanest solution may be just picking a version of CxxWrap you want to support, and moving on. Earlier versions of your package remain usable with other versions.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 18, 2020, 2:18pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/5 "2020-04-18T14:18:07Z")

</div>

There is a registered package by @klacru that does this as well: [PkgVersion](https://github.com/KlausC/PkgVersion.jl).

```julia
julia> using PkgVersion, Atom

julia> PkgVersion.Version(Atom)
v"0.12.10"

```

---

<div class="post-metadata">

**Author:** ![fieker](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@fieker](https://discourse.julialang.org/u/fieker)\
**Post date:** [April 21, 2020, 11:23am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/6 "2020-04-21T11:23:28Z")

</div>

Just to support @fingolfin:  
we communicate with Julia by package name only (add Blah, using Blah)  
we communicate with the users via name and version (cool feature in version 0.10.3 of Blah)  
The uuid is neccessary for Project.toml (deps)  
The version is neccessary to Project.toml (compat)

Yet, there is no “legal” way within Julia to obtain either the uuid, nor the version. Even worse, versions of indirect dependencies are not even shown with Pkg.status by default…  
It is difficult to see why a package is not updated, let alone to see that it is not updated.

I like the package manager and the declarative way, but I still need to be able to see and test the information. It has been part of Julia for a long time to e.g. check the Julia version to enable/ disable features. I can test for signatures being present, but I cannot test for a new algorithm being there unless the API changes.

There are workarounds like the PkgVersion, or using undocumented features (Pkg.installed(), Pkg.dependencies()), but it feels like it should be exponsed in a legal way.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@cbrnr](https://discourse.julialang.org/u/cbrnr)\
**Post date:** [July 30, 2021, 11:06am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/7 "2021-07-30T11:06:22Z")

</div>

Maybe I misunderstood your question, but most packages provide their version in a `VERSION` variable. For example, `CxxWrap.VERSION` results in `v"1.6.2"` on my machine.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 30, 2021, 11:55am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/8 "2021-07-30T11:55:36Z")

</div>

Thats the Julia version.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@cbrnr](https://discourse.julialang.org/u/cbrnr)\
**Post date:** [July 30, 2021, 12:31pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/9 "2021-07-30T12:31:40Z")

</div>

😂 alright, I didn’t expect that! Would have been too easy I guess. So single-sourcing a version is not really any easier than in [Python](https://packaging.python.org/guides/single-sourcing-package-version/) (I had hoped it was).

---

<div class="post-metadata">

**Author:** ![fingolfin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fingolfin/32/6033_2.png) [@fingolfin](https://discourse.julialang.org/u/fingolfin)\
**Post date:** [July 30, 2021, 12:55pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/10 "2021-07-30T12:55:43Z")

</div>

Just for reference, this is the code I ended up with:

```julia
# pkgdir was added in Julia 1.4
if VERSION < v"1.4"
   pkgdir(m::Core.Module) = abspath(Base.pathof(Base.moduleroot(m)), "..", "..")
else
   import Base.pkgdir
end
pkgproject(m::Core.Module) = Pkg.Operations.read_project(Pkg.Types.projectfile_path(pkgdir(m)))
pkgversion(m::Core.Module) = pkgproject(m).version
const VERSION_NUMBER = pkgversion(@ __MODULE__ )

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 30, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/11 "2021-07-30T15:14:48Z")

</div>

Perhaps `VersionNumber(TOML.parsefile("../Project.toml")["version"])` is easier?

---

<div class="post-metadata">

**Author:** ![fingolfin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fingolfin/32/6033_2.png) [@fingolfin](https://discourse.julialang.org/u/fingolfin)\
**Post date:** [July 30, 2021, 3:36pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/12 "2021-07-30T15:36:00Z")

</div>

Depends on your use case? (I’d use `Pkg.TOML.parsefile`, though).

But I actually needed `pkgversion()` for other things, also `pkgproject` and `pkgdir`, so…

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 30, 2021, 4:13pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/13 "2021-07-30T16:13:40Z")

</div>

> [@fingolfin](#):
>
> Depends on your use case?

I meant, you are using a bunch of Pkg internals that can change at any point and then if you have this in a bunch of packages they will immediately stop working. That’s why I prefer what I wrote.

> [@fingolfin](#):
>
> (I’d use `Pkg.TOML.parsefile` , though).

Better to just use `TOML`.

---

<div class="post-metadata">

**Author:** ![chiraganand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiraganand/32/32787_2.png) [@chiraganand](https://discourse.julialang.org/u/chiraganand)\
**Post date:** [February 18, 2022, 9:17am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/14 "2022-02-18T09:17:58Z")

</div>

> [@fieker](#):
>
> I like the package manager and the declarative way, but I still need to be able to see and test the information. It has been part of Julia for a long time to e.g. check the Julia version to enable/ disable features. I can test for signatures being present, but I cannot test for a new algorithm being there unless the API changes.

Adding another use case: for development purposes. I (probably) found a bug in IndexedTables and I could not figure out which version am I running. PkgVersion.Version() eventually helped so that I know which branch of the code to look at.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [February 22, 2022, 3:37am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/15 "2022-02-22T03:37:27Z")

</div>

For what it’s worth I’d also find it quite useful if the Base loading code knew about package version numbers and baked them into modules at precompile time. (I’m not sure `Pkg` needs to be involved?)

I’m using a variant of the code posted above to do this manually for DataSets.jl. Here’s a generic version of that:

```julia
using TOML

const PACKAGE_VERSION = let
    project = TOML.parsefile(joinpath(pkgdir(@ __MODULE__ ), "Project.toml"))
    VersionNumber(project["version"])
end

```

---

<div class="post-metadata">

**Author:** ![fingolfin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fingolfin/32/6033_2.png) [@fingolfin](https://discourse.julialang.org/u/fingolfin)\
**Post date:** [June 9, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/16 "2022-06-09T13:44:09Z")

</div>

Seems Julia 1.9 adds `pkgversion(m::Module)`, see [Add `pkgversion(m::Module)` to get the version of the package that loaded a given module by IanButterworth · Pull Request #45607 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/45607). Yay!

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [October 27, 2023, 9:29am UTC](https://discourse.julialang.org/t/how-to-find-out-the-version-of-a-package-from-its-module/37755/17 "2023-10-27T09:29:57Z")

</div>

Ah finally! Thanks 😉

Btw. my use-case is a bit different: I **need** to log the version of my package(s) in files which are created by the software themselves (e.g. via `HDF5.jl`) so that we have some internal provenance information for future needs.
