# How to use packages in class

**URL:** <https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, class, Package\
**Created:** [August 21, 2023, 9:51pm UTC](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174 "2023-08-21T21:51:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![atashinchi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@atashinchi](https://verificationacademy.com/forums/u/atashinchi)\
**Post date:** [August 21, 2023, 9:51pm UTC](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174/1 "2023-08-21T21:51:47Z")

</div>

I saw in the thread below that packages cannot be directly imported into classes:  
[https://verificationacademy.com/forums/systemverilog/why-it-illegal-import-package-contents-class-scope](https://verificationacademy.com/forums/systemverilog/why-it-illegal-import-package-contents-class-scope)

But I didn’t understand what is the solution. If I have a package with an enum inside and I want to reference that enum in a class, how can I achieve this? Do I import outside of the class instead? If so, what is the ‘scope’ of that import?

```verilog
class some_class extends some_other_class;
   //import my_pkg::*; // not allowed
   struct {
      enum_from_my_pkg_e my_enum;
      // other stuff
   } my_struct;

   // other class stuff
endclass

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 21, 2023, 10:23pm UTC](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174/2 "2023-08-21T22:23:58Z")

</div>

_In reply to [atashinchi](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174):_  
The solution is importing the package in the same scope where you are defining the class. That is usually in another package.

---

<div class="post-metadata">

**Author:** ![atashinchi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@atashinchi](https://verificationacademy.com/forums/u/atashinchi)\
**Post date:** [August 22, 2023, 12:53am UTC](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174/3 "2023-08-22T00:53:50Z")

</div>

Thanks Dave. In a somewhat related note: I have a .svh file with interfaces. I will

```verilog
`include

```

this file in some other .sv file (since I cannot put interfaces in packages). If an interface itself references an enum from another package, do I import the package inside the interface or outside?

```verilog
interface my_intf_i;
   import some_pkg::*; // is this allowed?
   some_enum_from_pkg_e my_enum;
   // other interface stuff
   // modport stuff
endinterface

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 22, 2023, 1:21am UTC](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174/4 "2023-08-22T01:21:58Z")

</div>

_In reply to [atashinchi](https://verificationacademy.com/forums/t/how-to-use-packages-in-class/42174/3):_

You can import a package anywhere you can declare a variable (except within a class scope).

The best place to put an **import** statement is inside a module/interface/package. If your module/interface ports need identifiers from a package, you can put the import statement as part of its header declaration.

Putting import statements outside a module/interface/package (which means it is in the $unit scope) is a bad idea for larger projects–it pollutes the namespace.
