From 9be4dd71cc4923c5d6e74158b41ba7e7f5eb5ac8 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sun, 21 Oct 2018 11:58:59 -0700 Subject: [PATCH] Properly treat venv path in Windows tests Since this is useful outside PyCall testing, I included this functionality in python_cmd function instead of inlining it to the test. --- src/pyinit.jl | 21 ++++++++++++++++++--- test/test_venv.jl | 10 +++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/pyinit.jl b/src/pyinit.jl index 472d9a88..1f368368 100644 --- a/src/pyinit.jl +++ b/src/pyinit.jl @@ -110,13 +110,28 @@ function pythonhome_of(pyprogramname::AbstractString) end """ - python_cmd(args::Cmd = ``) :: Cmd + python_cmd(args::Cmd = ``; venv) :: Cmd Create an appropriate `Cmd` for running Python program with command line arguments `args`. + +# Keyword Arguments +- `venv::String`: The path of a virtualenv to be used instead of the + default environment with which PyCall isconfigured. """ -function python_cmd(args::Cmd = ``) - cmd = `$pyprogramname $args` +function python_cmd(args::Cmd = ``; venv::Union{Nothing, String} = nothing) + if venv == nothing + py = pyprogramname + else + # See: + # https://github.com/python/cpython/blob/3.7/Lib/venv/__init__.py#L116 + if Compat.Sys.iswindows() + py = joinpath(venv, "Scripts", "python.exe") + else + py = joinpath(venv, "bin", "python") + end + end + cmd = `$py $args` # For Windows: env = copy(ENV) diff --git a/test/test_venv.jl b/test/test_venv.jl index 101b3044..a0331f71 100644 --- a/test/test_venv.jl +++ b/test/test_venv.jl @@ -36,9 +36,13 @@ end mktempdir() do path # Create a new virtualenv run(PyCall.python_cmd(`-m venv $path`)) - newpython = joinpath(path, "bin", "python") - if Compat.Sys.iswindows() - newpython *= ".exe" + newpython = PyCall.python_cmd(venv=path).exec[1] + if !isfile(newpython) + @info """ + Python executable $newpython does not exists. + This directory contains only the following files: + $(join(readdir(dirname(newpython)), '\n')) + """ end @test isfile(newpython)