# Canonical way to check if a project is a package?

**URL:** https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827
**Category:** Package Management
**Tags:** package, pkg, project
**Created:** [November 25, 2022, 8:14pm UTC](https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827 "2022-11-25T20:14:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)
#### Post date: [November 25, 2022, 8:14pm UTC](https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827/1 "2022-11-25T20:14:52Z")

</div>

I’m trying to write some code to detect if a Project.toml defines a package or not (making a better error output in this issue [Requesting better error message for non-packages · Issue #60 · GunnarFarneback/LocalRegistry.jl · GitHub](https://github.com/GunnarFarneback/LocalRegistry.jl/issues/60)). Right now, I’m just checking whether it has a UUID (after loading it via `Pkg.Types.read_project`). I can upgrade that check by making sure there’s a file called `$(project_dir)/src/$(package_name)[.jl]`, but really, to be most correct, I need to make sure that file defines a module with the same name, and I’m not sure that I know how to do that. So:

a) Is there already a canonical way to check this?  
b) How can I check if that module definition occurs?  
c) Is that check sufficient?

---

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [November 25, 2022, 9:20pm UTC](https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827/2 "2022-11-25T21:20:10Z")

</div>

This is quite possibly too naive a thought but:

> [@grahamas](#):
>
> I need to make sure that file defines a module with the same name

Could you try activating the `Project.toml` for that given project or package (let’s say the name is `YourPackage`) and then run the following snippet:

```julia
try 
    using YourPackage
    using Pkg

    pkg_name = "YourPackage"
    m = Pkg.Operations.Context().env.manifest

    v_key = findfirst(v->v.name == pkg_name, m)
    if isnothing(v_key)
        error("Version not found! Is this a valid Julia package?")
    else
        v = m[v_key].version
        println("Package version is $v")
     end
catch e
    println("$e: Is this a valid Julia package?")
end

```

This solution was inspired by a stack overflow post here: [julia - Determine version of a specific package - Stack Overflow](https://stackoverflow.com/questions/25635508/determine-version-of-a-specific-package)

It does assume a manifest exists for the environment however so that may derail this solution potentially. To my understanding, this would at least solve b), for a) maybe see this discourse post for more: [How to test if package is installed - #4 by Tamas\_Papp](https://discourse.julialang.org/t/how-to-test-if-package-is-installed/3508/4) and this one: [Test for existence of a variable - #39 by Tamas\_Papp](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/39) for c) I would say that the checks may have to be up to more how you structure or want to administrate your local registry… I would say that could be sufficient personally, but I haven’t run a registry myself yet. 😛

---

<div class="post-metadata">

### Author: ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)
#### Post date: [November 25, 2022, 10:23pm UTC](https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827/3 "2022-11-25T22:23:24Z")

</div>

Thanks for the suggestion (and the links). I think you’re right that that will work, but I’d rather avoid `using YourPackage` at any point. That can be an annoyingly long step for some packages.

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [November 25, 2022, 11:07pm UTC](https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827/4 "2022-11-25T23:07:31Z")

</div>

Could you scan Project.toml for a uuid?

---

<div class="post-metadata">

### Author: ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)
#### Post date: [December 5, 2022, 11:11pm UTC](https://discourse.julialang.org/t/canonical-way-to-check-if-a-project-is-a-package/90827/5 "2022-12-05T23:11:49Z")

</div>

We ended up doing three things: 1) check a UUID exists, 2) check a version exists, 3) check `Project/src/Project.jl` exists (with some allowances for the extension)
