# Import OS in script

**URL:** <https://forum.inductiveautomation.com/t/import-os-in-script/98765>\
**Category:** Ignition\
**Created:** [November 18, 2024, 11:25pm UTC](https://forum.inductiveautomation.com/t/import-os-in-script/98765 "2024-11-18T23:25:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephanie\_Aguiar](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Stephanie\_Aguiar](https://forum.inductiveautomation.com/u/Stephanie_Aguiar)\
**Post date:** [November 18, 2024, 11:25pm UTC](https://forum.inductiveautomation.com/t/import-os-in-script/98765/1 "2024-11-18T23:25:54Z")

</div>

Hello,

I am trying to use the os functions in my scripts to access the latest file from a folder. My code works perfectly fine when I run it on the script console but once I try to run it on the timer scripts I receive this error. Does anyone know what could be wrong? Can I not use os on timer scripts?

 ![Screenshot 2024-11-18 152126](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/7/c7aa07f1ba3a56845a5c996ca91d0727354654b7.jpeg)

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [November 18, 2024, 11:38pm UTC](https://forum.inductiveautomation.com/t/import-os-in-script/98765/2 "2024-11-18T23:38:30Z")

</div>

Keep in mind that the script console runs in client scope and the gateway scripts run in Gateway scope. This does have implications.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 18, 2024, 11:42pm UTC](https://forum.inductiveautomation.com/t/import-os-in-script/98765/3 "2024-11-18T23:42:20Z")

</div>

Your immediate problem is caused by gateway event scripts using legacy Python scoping rules in 8.1 and prior versions; this means the imports defined in the outer scope are not available inside the defined functions.  
The correct solution _to that problem_ is to move everything you're doing into the project script library, and make your gateway timer script a one-line call to `someLibrary.someFunction()`; the move into the project library will get you to regular scoping rules that make sense.

Your _second_ problem is that Jython's standard library isn't great. You should import and use Java's standard library construct (e.g. in this case one of the static methods from [`Files`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html)); it will be faster, give you less inscrutable errors, and in general cause you less pain over time.

In other words, the order of preference for a maintainable script over time is:  
0. Don't use a script 😄

1. Ignition's built in `system` library
2. Java's standard library constructs via import, e.g. `from java.x import y` or `import java.x.y`
3. "Well known" third party Java libraries, like Google Guava or Apache Collections, that Ignition includes in all scopes. These are technically subject to change, but in practice seldom do outside of major Ignition versions.
4. In a distant last place, Jython standard library imports.

Note that only option 1 is within scope of our official support.
