Welcome! Please see the About page for a little more info on how this works.

0 votes
in REPL by

I'm learning clojure trought the book "The Clojure Wokshop - Packt" and i have stucked for a few days in this exercise:

(My IDE is IntelliJ and i'm using Windows).

Exercise 4.10: Importing Data from a CSV File

1. Create a folder somewhere convenient on your computer.

I decided to create an entirely new project given how many attempts i tried.

Is there any difference here while choosing between Leiningen or Deps? I most of the time use Leiningen, but should i use Deps because i am gonna use a deps.edn file?

image

2. Download the match_scores_1991-2016_UNINDEXED.csv file to the folder you created. (here on github)

But where should i download this file? Into the src file inside the project file or any file works? Is there any difference?

I decided to save inside de src.

print

3. In your editor, in the same folder, create a deps.edn file with the following contents:

{:deps
 {org.clojure/data.csv {:mvn/version "0.1.4"}
  semantic-csv {:mvn/version "0.2.1-alpha1"}}}

So, i created a deps.edn file.

deps.edn

4. Verify that everything is working by evaluating the following expression in your REPL:

user> (require '[clojure.data.csv :as csv])
nil
user> (require '[clojure.java.io :as io])
nil
user> (with-open [r (io/reader "match_scores_1991-2016_unindexed_csv.csv")]
         (first (csv/read-csv r)))

When creating the REPL, should I select "Run with Intellij project classpath", right?

https://i.imgur.com/1ZTvirB.png

But when i am gonna evaluate the testing expressions, it shows an error while evaluating the second one and third one.

error

The error while evaluating the "clojure.data.csv :as csv" is this one:

Execution error (FileNotFoundException) at csv-example.core/eval1549 (form-init2604783929697477049.clj:1).
Could not locate clojure/data/csv__init.class, clojure/data/csv.clj or clojure/data/csv.cljc on classpath.

What am i missing? Have been trying for days to solve this but i didn't found any answer.

Thank you!

3 Answers

+1 vote
by
selected by
 
Best answer

It looks like the dependency for data.csv is not being picked up. My guess is that you created a leiningen project (given that there is a project.clj in your project folder). You then created a deps.edn, and put the dependencies in there. Out of the box, leiningen will not pick up deps.edn (there are plugins to allow you to do this). So, when you ran your project from the IDE, my bet is it used the project.clj which didn't have the data.csv dependencies (probably only clojure).

You can try going to your project folder where the deps.edn is in a terminal and running clj there, which should give you a repl based on the deps.edn file, and should evalute the require correctly.

You can also just put the dependency for data.csv into the :dependencies key of the leiningen project.clj file. The other option is to let clj resolve depend, but still use leiningen. Plugins like lein-tools-deps do this.

I personally would just stick with leiningen, or if the tutorial is pushing clj, follow that exactly.

by
Thank you Tom! I solved the problem by adding:

[org.clojure/data.csv   "1.0.0"]
[semantic-csv           "0.2.1-alpha1"]

To the :dependencies key inside the project.clj file.
0 votes
by

Regardless of your project tool, I would recommend putting these files in your project like so:

csv_example/                    ;; project root
csv_example/deps.edn            ;; if using Deps
csv_example/project.clj         ;; if using Leiningen
csv_example/src                 ;; src root
csv_example/src/csv_example/core.clj
csv_example/match_scores_1991-2016_unindexed_csv.csv

Issue #1 - project configuration files (deps.edn or project.clj) should always at the root (not under src). I think the IntelliJ classpath may only work as expected if you have imported the project as a Deps or Leiningen project (and in those cases you would need either a deps.edn or project.clj at the project root). In all of those cases, you should see clojure, data.csv, and semantic-csv show up as libs in the project tree under External Libs if it's working properly.

Issue #2 - relative file paths in the code (like your call to reader) will be resolved relative to the current directory (which your IDE will default to the project root, so don't put it under the src dir).

by
Thank you for your answer Alex!
0 votes
by

Asked and answered on StackOverflow: https://stackoverflow.com/questions/65543714/problems-while-creating-a-deps-edn-file (so folks can read some history of the issue and what has already been suggested).

...