What is nim's equivalent of Python's `sys.executable`?
Asked Answered
F

3

5

Following is the content of foo.py

import sys
print(sys.executable)

When I execute this, I can get the full path of the the Python interpreter that called this script.

$ /mingw64/bin/python3.9.exe foo.py
/mingw64/bin/python3.9.exe

How to do this in nim (nimscript)?

Frerichs answered 22/10, 2021 at 10:18 Comment(0)
G
2

The question mentions NimScript, which has other uses in the Nim ecosystem, but can also be used to write executable scripts instead of using, e.g., Bash or Python. You can use the selfExe proc to get the path to the Nim executable which is running a NimScript script:

#!/usr/bin/env -S nim --hints:off
mode = ScriptMode.Silent

echo selfExe()

After saving the above as test.nims and using chmod +x to make the file executable, the script can be invoked to show the path to the current Nim executable:

$ ./test.nims
/home/.choosenim/toolchains/nim-1.4.8/bin/nim
Gillyflower answered 23/10, 2021 at 8:33 Comment(0)
J
4

If you want to do that in Nim (not NimScript), you can take compiler executable path using https://nim-lang.org/docs/os.html#getCurrentCompilerExe

import os

echo getCurrentCompilerExe()
Jumble answered 25/10, 2021 at 12:21 Comment(0)
N
2

Nim is compiled, so I assume you want to get the path of the application's own binary? If so, you can do that with:

import std/os

echo getAppFilename()
Natalianatalie answered 22/10, 2021 at 10:20 Comment(0)
G
2

The question mentions NimScript, which has other uses in the Nim ecosystem, but can also be used to write executable scripts instead of using, e.g., Bash or Python. You can use the selfExe proc to get the path to the Nim executable which is running a NimScript script:

#!/usr/bin/env -S nim --hints:off
mode = ScriptMode.Silent

echo selfExe()

After saving the above as test.nims and using chmod +x to make the file executable, the script can be invoked to show the path to the current Nim executable:

$ ./test.nims
/home/.choosenim/toolchains/nim-1.4.8/bin/nim
Gillyflower answered 23/10, 2021 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.