I came across the youtube video for accessing external package by specifying in .cabal
in a project. But how to access the package in a single file.
Suppose I want to write a simple file and do some stuff in it. Can I import an external package such as System.Random
? Can I compile it using ghc
without error?
Note: I am not using the project in this case. So I am not using cabal
. Only a single file and compiling using ghc or loading in ghci
The System.Random
module [Hackage] is defined in the random
package [Hackage].
You can add random
to the dependencies in the cabal file:
-- …
library
-- …
build-depends:
base >= 4.7 && < 5
, random >=1.0 && <2.0
-- …
If you build it with cabal build
or Haskell stack, so stack build
, it will download the package and expose it to the modules of your project, so you can import it.
I am not using the project in this case. So I am not using cabal. Only a single file and compiling using ghc or loading in ghci
If you don't use cabal
(or tools like Haskell stack that essentially built on top of cabal
), there is not much need to work with a .cabal
file.
In that case you can call the compiler with a list of packages to expose:
ghc -package random MyFile.hs
Then it looks in the package database, you can define a different one where you download and build the packages that you want to expose to your project.