Can't run utop after installation - ocaml

I've just installed utop on openSUSE but when i type utop in terminal i have
If 'utop' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf utop
Typing eval 'opam config env' gives me this:
OPAM_SWITCH_PREFIX='/home/jadw1/.opam/default'; export OPAM_SWITCH_PREFIX;
CAML_LD_LIBRARY_PATH='/home/jadw1/.opam/default/lib/stublibs:/usr/lib64/ocaml/stublibs:/usr/lib64/ocaml'; export CAML_LD_LIBRARY_PATH;
OCAML_TOPLEVEL_PATH='/home/jadw1/.opam/default/lib/toplevel'; export OCAML_TOPLEVEL_PATH;
MANPATH='/usr/local/man:/usr/share/man:/home/jadw1/.opam/default/man'; export MANPATH;
PATH='/home/jadw1/.opam/default/bin:/home/jadw1/bin:/usr/local/bin:/usr/bin:/bin:/usr/lib/mit/sbin'; export PATH;

You're supposed to type this:
$ eval `opam config env`
Not this:
$ opam config env
What happens is that opam config env writes out some shell commands that you want to execute. That's what the eval does. If you see output like you saw, it means you left out the eval.

eval is a command that constructs commands from the arguments it's given and executes them.
eval 'opam config env', with apostrophes, is therefore equivalent to just running opam config env directly, which just writes out a sequence of shell commands.
If you replace the apostrophes with backticks, however, it will first execute the quoted command, then pass its output to eval and execute that.
eval `opam config env`
is therefore more or less equivalent to running opam config env, then copying and pasting the output back into the console, which you could also do.

Related

How can I switch the opam compiler in an easy way and what is the --set-switch flag?

Consider:
$ opam switch set ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1
# Run eval $(opam env --switch=ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1) to update the current shell environment
$ eval $(opam env --switch=ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1)
[NOTE] To make opam select the switch ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1 in the current shell, add --set-switch or set OPAMSWITCH
But then it doesn't tell me at all where --set-switch should go. Why? Where?
Related:
Switch environment in OPAM
It's confusing why it gives you a command, but then the command doesn't work. If you browse the opam man page, it says (opam-switch):
opam switch set sets the default switch globally. The shell hook, when enabled, synchronises the current shell session with this switch, unless the current directory is a local switch, when that local switch is used instead. You can always use eval $(opam env --switch=SWITCH --set-switch) to specify the switch explicitly, which overrides the shell hook.
Then it works if you do:
eval $(opam env --switch=ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1 --set-switch)
See:
$ opam switch
# switch compiler description
coq-8.10 ocaml-base-compiler.4.07.1 coq-8.10
default ocaml.4.14.0 default
→ ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1 ocaml-variants.4.07.1+flambda ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1
It is really annoying and confusing.

OCaml: Can't find version

When I enter ocaml --version it returns
/usr/bin/ocaml: unknown option '--version'.
I installed it using OPAM and ran the instructions
opam init
eval $(opam env)
eval opam env
opam switch create 4.07.0
and everything seemed to go fine. Entering which ocaml returns a correct-looking path. And when entering a terminal session it seems to work fine. I don't see this error mentioned anywhere when I search for it.
OCaml tools are weird and usually do not follow the POSIX guidelines of using double dashes for command line options. At this point probably for historic reasons and because the standard Arg module parses command line arguments this way. In any case, this will do the trick:
ocaml -version

How to source a file in Tox

How do you configure Tox to source a file before it runs your test command?
I tried the obvious:
commands = source /path/to/my/setup.bash; ./mytestcommand
But Tox just reports the ERROR: InvocationError: could not find executable 'source'
I know Tox has a setenv parameter, but I want to use my setup.bash and not have to copy and paste its contents into my tox.ini.
tox uses exec system call to run commands, not shell; and of course exec doesn't know how to run source. You need to explicitly run the command with bash, and you need to whitelist bash to avoid warnings from tox. That is, your tox.ini should be somewhat like this:
[testenv]
commands =
bash -c 'source /path/to/my/setup.bash; ./mytestcommand'
whitelist_externals =
bash

ocaml command line cannot find “topfind”

I've installed opam, run opam init, run opam switch 4.06.0 which created a 4.06.0 directory inside ~/.opam, run "eval opam confing env" which exports $OCAML_TOPLEVEL_PATH as ~/.opam/4.06.0/lib/toplevel amongst other things, when launching ocaml I get the dreaded:
$ ocaml
OCaml version 4.06.0
Cannot find file topfind.
Unknown directive `camlp4o'.
#
I've looked at this and this neither of which address my issue and I'm at my wits' end (first time setting up OCaml). This is my ~/.ocamlinit:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind"
#camlp4o
#thread
#require "core.top"
#require "core.syntax"
EDIT: Looks like I hadn't installed core, installing core resolved that but now amongst the slew of import diagnostics I get:
Exception:
Invalid_argument
"The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".
And then a bit further down:
Raised at file "pervasives.ml", line 33, characters 25-45
Called from file "toplevel/toploop.ml", line 468, characters 4-128
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
Camlp4 Parsing version 4.06.0
You should run
eval `opam config env`
Note the backticks. They are usually located to the left of the key 1 on most modern keyboards. The command should not output anything, if you see any output it means that you're running it incorrectly. You have to run this command to activate the opam installation every time you start a new shell (unless you've put this command in your shell initialization scripts, like .bashrc)
If the problem persists, then make sure, that you have installed the ocamlfind package,
opam install ocamlfind
What seemed to work for me:
make sure core is installed (opam install core)
make sure camlp4 is installed (opam install camlp4)
Insert Topfind.don't_load ["compiler-libs.toplevel"];; in-between #use "topfind";; and #require "core.top";;, as per this. It is an issue that doesn't appear to be fixed in the latest version of core (0.9.2).

Calling Python package from command line / PyCharm

I am creating a Python package, I anticipate that it will be called both by command line and from other scripts. Here is a simplified version of my file structure:
GREProject/
__init__.py
__main__.py
Parsing.py
Parseing.py contains a method, parse(), it takes two arguments, an input file and an output file. I am trying to figure out the proper code for "__main__.py" so that when the following is called from the command line or terminal the arguments will be passed to "parse()":
Python GREProject -i input.file -o output.file
I have tried this numerous ways but they have all met with failure, I do believe I need the "-m" flag for the interpreter but more than that I don't know. Example with the flag:
Python -m GREProject -i input.file -o output.file
When running the later command I receive the following error:
Import by filename is not supported.
Presumably from this line:
from . import Parsing
Ok, turns out this was a problem with my IDE, PyCharm. No idea why I recieved this error but I have setting that fixed it:
Import by filename is not supported.
For the record here are the options I set in my Pycharm project
Script:
GREProject
Script parameters:
-i .\GREProject\pr2.nyc1 -o .\GREProject\Test.pkl
Enviroment variables:
PYTHONUNBUFFERED=1
Python interpreter:
Python 2.7.11 (c:\Python27\python.exe)
Interpreter options:
-m
Working directory:
C:\Users\probert.dan\PycharmProjects
Here is an explanation of the options:
Script: This is the script to run, by default PyCharm will only insert absolute references to .py files, nothing prevents you from manually typing in a relative reference, in this case it is the GREProjects folder.
Script Parameters: These are passed onto the script itself, in this case I am telling my script that the input file is ".\GREProject\pr2.nyc1" which means, look the file "pr2.nyc1" in the "GREProject" directory below the current working directory.
Environment variables: This was set by PyCharm and left unchanged.
Python interpreter: My active interpreter.
Interpreter options: The option here tells python that we are calling a module, python then knows to access the "__main__.py" file.
Working directory: The directory the script is run from, I chose the directory above "GREProject"
For reference here is the contents of my "__main__.py file":
from . import Parsing
import argparse
parser = argparse.ArgumentParser(description='Parse flags.')
parser.add_argument('-i', help='Import file.')
parser.add_argument('-o', help='(Optional) Output file.')
arguments = parser.parse_args()
Parsing.parse(arguments.i, arguments.o)
It is also important to note that debugging in PyCharm is not possible like this. Here is the solution to debugging: Intellij/Pycharm can't debug Python modules