In Python, packages may have a __main__.py
file. If they do you can execute the code inside of this file in the command line by using:
python -m python_package_name
.
It can also accept arguments:
python -m python_package_name arg1 arg2
.
This is useful for using python packages in shell scripts.
I know about julia -e "some_julia_code"
but it would be nice if I didn’t have to write the Julia code in the shell or point to a julia file containing the code I want to execute. Does Julia have something similar?
Well, you have to do something like:
julia -e "using julia_package_name; main()"
if you want to load a package and execute the function main() of the package. Not so different, and no special syntax required.
2 Likes
Can this accept command line arguments?
You can certainly pass arguments to a script from the command line, see https://docs.julialang.org/en/v1/manual/faq/#How-do-I-pass-options-to-julia-using-#!/usr/bin/env?
The arguments you pass will be available in the global variable ARGS
. There are several packages that help with argument parsing should you need it.
Example:
$ julia -e "@show ARGS" 111
ARGS = ["111"]
You can replace the -e "@show ARGS"
part here with your script file etc.
1 Like
You can create a simple shell script and put in a folder which is on your PATH
variable.
For example, for unix the script will be something like:
#!/bin/bash
package="$1"
arguments="${@:2}"
exec julia -q -e "using $package; $package.main()" $arguments
Naming the script juliap
, you can use like:
juliap SomePackage x y -z --foo
Not sure how to write this script on windows though.
1 Like