Skip to content

Commit a1c7a1a

Browse files
authored
How to setup a virtual environnement in Scala?
1 parent e36fb38 commit a1c7a1a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

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

1294+
## How to setup a virtual environnement in Scala?
1295+
1296+
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
1297+
1298+
```
1299+
cd myapp
1300+
python3 -m venv myapp-env
1301+
source myapp-env/bin/activate
1302+
pip install -r requirements.txt
1303+
```
1304+
1305+
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`. Another popular approach is to use Pipenv
1306+
1307+
```
1308+
cd myapp
1309+
pipenv shell
1310+
pipenv install
1311+
```
1312+
1313+
which makes use of `Pipfile` and corresponding lock file to manage dependencies.
1314+
1315+
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 we specify dependencies inside `build.sbt` file (which is conceptually similar to `Pipfile`) under `libraryDependencies`, then executing
1316+
1317+
```
1318+
cd myapp
1319+
sbt compile
1320+
```
1321+
1322+
downloads all the dependencies under the local repository. The location of the 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.
1323+
12941324
[collections-classes]: {% link _overviews/scala3-book/collections-classes.md %}
12951325
[concurrency]: {% link _overviews/scala3-book/concurrency.md %}
12961326
[contextual]: {% link _overviews/scala3-book/ca-contextual-abstractions-intro.md %}

0 commit comments

Comments
 (0)