Skip to content

Commit

Permalink
Properly treat venv path in Windows tests
Browse files Browse the repository at this point in the history
Since this is useful outside PyCall testing, I included this
functionality in python_cmd function instead of inlining it to the
test.
  • Loading branch information
tkf committed Oct 21, 2018
1 parent f9f53e6 commit 9be4dd7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/pyinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions test/test_venv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9be4dd7

Please sign in to comment.