Isolated environment

is it possible in crystal to create a virtual environment / isolated environment like venv in python?

If I am guessing correctly, what you really need might be the following:

shards init
vi shards.yml

Edit the dependencies:

dependencies:
  kitty:
    github: tabbycat/kitty
  pug:
    github: dog/pug

Then install them:

shards install

See

https://crystal-lang.org/reference/1.12/man/shards/index.html

1 Like

so crystal doesn’t have something like venv in python?

I haven’t heard much about it.

Crystal projects are already pretty isolated from one another, with the exception of the standard library and the compiler itself. Could you elaborate a bit more on what you’re looking for @gilarb ?

The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories.

venv — Creation of virtual environments — Python 3.12.3 documentation

This is how Crystal works by default. There are no global dependencies. The dependencies for each application are installed in the lib folder of each application.

4 Likes

The idea of using Crystal’s virtual environment is interesting.
I sometimes wish I could use Crystal for short programs and data analysis like I use Python, etc.

Compiling Crystal takes a lot of time. Usually, it involves a long preparation step before it runs quickly. But Python can start immediately without this step, which is desirable for data analysis and quick tasks. Julia is also good for these types of jobs. Crystal has a interpreter these days, but it’s not very fast. Yet, there are those who are not very smart and for some reason want to use crystals for everything (that’s me).

An easy way would be to create a file named “~/myshards.yml” and copy it to your working folder when you need it.

1 Like

no, I’m just asking, I just started studying crystal, so idk how it’s works, I thought it was similar with “how python works”.

1 Like

Thanks for the explanation @ysbaddaden

ahh now I’m understand, I just started studying crystal, I thought it was similar with “how python works”, Thank you for the help @kojix2

1 Like

Yea usually its best to compile your Crystal scripts in release mode and then toss the binary into some directory that is in $PATH. Then you’ll get the quick performance of it already being built. Granted this really only makes sense for the scripts that are somewhat reusable, and not one-off.

Probably a bit of an anti-pattern, but this technically IS possible via customizing the CRYSTAL_PATH environment variable. I.e. vendor in the lib(s) into some directory, add that directory to CRYSTAL_PATH, being sure to add it last after the two default paths. Then from here you can require "my_lib" in any project (local to your computer) and it’ll be available.

Ultimately not that useful but :person_shrugging:.

2 Likes

In fact, this exactly what i did, it quite useful, at least for me, i copy several useful (for debug purpose) package there, and require it only when i need it.

2 Likes