Is there a similar library to `os` in crystal?

i want to use the os functionality is there a similar library to os like python in crystal ?

There is the stdlib :grin: , what exactly feature from python os module are you trying to find in Crystal? You need to be more specific, otherwise the answer will be a plain and empty “Yes”.

e[2mShowing last frame. Use --error-trace for full trace.e[0m

In e[4mmyproject.cr:1:1e[0m

e[2m 1 | e[0me[1mrequire "stdlib"e[0m
     e[32;1m^e[0m
e[33;1mError: can't find file 'stdlib'e[0m

If you're trying to require a shard:
- Did you remember to run `shards install`?
- Did you make sure you're running the compiler in the same directory as your shard.yml?

how to install it ???

Microsoft Windows [Version 10.0.19045.2965]
(c) Microsoft Corporation. All rights reserved.

C:\Users\unknown>shards install stdlib
Resolving dependencies

C:\Users\unknown>

C:\Users\unknown\Documents\CODES\myproject>dir
 Volume in drive C has no label.
 Volume Serial Number is 5238-A1C3

 Directory of C:\Users\unknown\Documents\CODES\myproject

09/28/2023  06:01 PM    <DIR>          .
09/28/2023  06:01 PM    <DIR>          ..
09/28/2023  05:56 PM               150 .editorconfig
09/28/2023  05:56 PM                37 .gitignore
09/28/2023  06:02 PM    <DIR>          lib
09/28/2023  05:56 PM             1,084 LICENSE
09/28/2023  05:56 PM               601 README.md
09/28/2023  06:02 PM                24 shard.lock
09/28/2023  05:56 PM               153 shard.yml
09/28/2023  05:56 PM    <DIR>          spec
09/28/2023  05:56 PM    <DIR>          src
               6 File(s)          2,049 bytes
               5 Dir(s)  410,704,936,960 bytes free

C:\Users\unknown\Documents\CODES\myproject>


C:\Users\unknown\Documents\CODES\myproject>dir
 Volume in drive C has no label.
 Volume Serial Number is 5238-A1C3

 Directory of C:\Users\unknown\Documents\CODES\myproject

09/28/2023  06:01 PM    <DIR>          .
09/28/2023  06:01 PM    <DIR>          ..
09/28/2023  05:56 PM               150 .editorconfig
09/28/2023  05:56 PM                37 .gitignore
09/28/2023  06:02 PM    <DIR>          lib
09/28/2023  05:56 PM             1,084 LICENSE
09/28/2023  05:56 PM               601 README.md
09/28/2023  06:02 PM                24 shard.lock
09/28/2023  05:56 PM               153 shard.yml
09/28/2023  05:56 PM    <DIR>          spec
09/28/2023  05:56 PM    <DIR>          src
               6 File(s)          2,049 bytes
               5 Dir(s)  410,704,936,960 bytes free

C:\Users\unknown\Documents\CODES\myproject>shards install stdlib
Resolving dependencies

C:\Users\unknown\Documents\CODES\myproject>

it doesn’t work

The stdlib isn’t something you install. It’s already available to you. See Crystal 1.9.2. However as previously asked, what in particular are you looking to do?

e[2mShowing last frame. Use --error-trace for full trace.e[0m

In e[4mproject.cr:1:1e[0m

e[2m 1 | e[0me[1mrequire "stdlib"e[0m
     e[32;1m^e[0m
e[33;1mError: can't find file 'stdlib'e[0m

If you're trying to require a shard:
- Did you remember to run `shards install`?
- Did you make sure you're running the compiler in the same directory as your shard.yml?

i tried importing but this is what i get

Again, it’s not something you have to install/require explicitly. It’s just there for you inherently.

Maybe it would be best if you start off with Getting started - Crystal.

stdlib is like os ??? where are its functions can you give me a link to it ?

I provided a link to it earlier. What are you wanting to do exactly?

The stdlib is simply all the types/modules bundled with Crystal itself, which may or may not include what you want out of the box.

i want to deal with the windows apis

Okay, and what method from Python’s os module would help you accomplish that?

4 Likes

@solvecodes, you wrote “os” in lower case. This was the beginning of the misunderstanding.

For many Crystal users, os refers to Linux or Mac, and rarely to Windows. Worse, Python has a well-known module called os, which is not necessarily related to the Windows API, but people mistakenly thought you were asking for this.

It is only recently that Crystal has supported Windows, and calling the Windows API directly is currently not highly recommended by Microsoft. . So there is no tool that calls the Windows API as a standard library.

If you search the Internet and no one has created a WIN32API package, you may have to write your own bindings.

But what do you really want to do? If you want to make a simple GUI application, I suggest you try libui.

However, you may not be able to work with libui since Windows is really recently supported. In that case, if you ask how to use libui on Windows, someone, including me, will be able to help you.

Crystal has been stable on Windows for quite some time now, only recently moving towards being officially supported — that doesn’t mean you can’t or shouldn’t use it on Windows.

The functionality provided by Python’s os module[1] is already accessible in the standard library via Path/File/Dir, Process, System, and other APIs. Anything beyond that would be going into native API territory, but it depends on what exactly you want to do.


  1. I don’t think using Python’s os module as a reference for things in Crystal is the best example; the module is more or less a “god object” encapsulating implementation-specific methods that exist in the APIs listed above. ↩︎

3 Likes

If you really want Python os, those functions are kept in separate modules in Crystal.

And so on.