Skip to content

Commit 220c80c

Browse files
committed
How to setup a virtual environnement in Scala?
1 parent fa60d7e commit 220c80c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

_overviews/scala3-book/scala-for-python-devs.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,30 @@ Follow the links below for more details:
12911291
- Infix methods
12921292
- Macros and metaprogramming
12931293

1294+
1295+
## How to setup virtual environnement in Scala?
1296+
1297+
In Python there is a common practice of creating an isolated environnement for a particular project. For example, say we are working on application `myapp`, then using `venv` Python module we might do
1298+
1299+
```
1300+
cd myapp
1301+
python3 -m venv myapp-env
1302+
source myapp-env/bin/activate
1303+
pip install -r requirements.txt
1304+
```
1305+
1306+
This installs all the dependencies under project's directory `myapp/myapp-env` and alters the shell environmental variable `PATH` to look up dependencies from `myapp-env`.
1307+
1308+
Key difference in Scala is that project's dependencies are not installed per project, and hence potentially same dependency of the same version is duplicated across multiple projects, but instead dependencies are installed inside a central local repository which is reused across projects. Dependency of particular version is installed only once and then reused across different projects that require it. For example using `sbt` build tool dependencies are specified inside `build.sbt` file under `libraryDependencies`, then executing
1309+
1310+
```
1311+
cd myapp
1312+
sbt compile
1313+
```
1314+
1315+
downloads all the dependencies under local repository. The location of local repository can be queried with `show csrCacheDirectory` inside sbt shell. Note by using `sbt` each project gets its own isolated environment without the need for lock files or modification of the shell environnement. All the necessary paths are managed by the build tool itself.
1316+
1317+
12941318
[collections-classes]: {% link _overviews/scala3-book/collections-classes.md %}
12951319
[concurrency]: {% link _overviews/scala3-book/concurrency.md %}
12961320
[contextual]: {% link _overviews/scala3-book/ca-contextual-abstractions-intro.md %}

0 commit comments

Comments
 (0)