Julia: failed to run command with arguments via string variable

I am trying to use run () or success () to execute a Python script from Julia.

I can run it normally if I specify the command manually:

julia> run(`python sample.py`) woo! sample 

However, if I try to start it with a string argument, it unexpectedly does not work.

 julia> str = "python sample.py" "python sample.py" julia> run( `$str` ) ERROR: could not spawn `'python sample.py'`: no such file or directory (ENOENT) in _jl_spawn at process.jl:217 in spawn at process.jl:348 in spawn at process.jl:389 in run at process.jl:478 

Specifying the full path for sample.py gives the same result. Oddly enough, python just works as a string:

 julia> str = "python" "python" julia> run( `$str` ) Python 2.7.3 (default, Feb 27 2014, 19:58:35) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 

Am I doing something wrong?

thanks

+6
source share
1 answer

This is due to specialized command interpolation . It treats each interpolated part as an independent part of the command. While a little unintuitive, it allows you to completely forget about all the difficulties of quoting, spaces, etc.

When you run(`$str`) , it treats str as the name of the whole command, so it complains that it cannot find an executable file called "python sample.py" . If you want to run "python" with the argument "sample.py", you will need two interpolations:

 cmd = "python" arg = "sample.py" run(`$cmd $arg`) 

This allows your argument to have a space and will still be treated as the first argument.

If you really want to use a string like "python sample.py" , you can break it down by its space:

 str = "python sample.py" run(`$(split(str))`) # strongly unadvised 

But note that this will be very fragile for the argument name. If you ever want to run a file called "My Documents / Sample .py", it will break, while the first interpolation will work.

+7
source

Source: https://habr.com/ru/post/972037/


All Articles