Skip to content

Commit 9be4dd7

Browse files
committed
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.
1 parent f9f53e6 commit 9be4dd7

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/pyinit.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,28 @@ function pythonhome_of(pyprogramname::AbstractString)
110110
end
111111

112112
"""
113-
python_cmd(args::Cmd = ``) :: Cmd
113+
python_cmd(args::Cmd = ``; venv) :: Cmd
114114
115115
Create an appropriate `Cmd` for running Python program with command
116116
line arguments `args`.
117+
118+
# Keyword Arguments
119+
- `venv::String`: The path of a virtualenv to be used instead of the
120+
default environment with which PyCall isconfigured.
117121
"""
118-
function python_cmd(args::Cmd = ``)
119-
cmd = `$pyprogramname $args`
122+
function python_cmd(args::Cmd = ``; venv::Union{Nothing, String} = nothing)
123+
if venv == nothing
124+
py = pyprogramname
125+
else
126+
# See:
127+
# https://github.com/python/cpython/blob/3.7/Lib/venv/__init__.py#L116
128+
if Compat.Sys.iswindows()
129+
py = joinpath(venv, "Scripts", "python.exe")
130+
else
131+
py = joinpath(venv, "bin", "python")
132+
end
133+
end
134+
cmd = `$py $args`
120135

121136
# For Windows:
122137
env = copy(ENV)

test/test_venv.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ end
3636
mktempdir() do path
3737
# Create a new virtualenv
3838
run(PyCall.python_cmd(`-m venv $path`))
39-
newpython = joinpath(path, "bin", "python")
40-
if Compat.Sys.iswindows()
41-
newpython *= ".exe"
39+
newpython = PyCall.python_cmd(venv=path).exec[1]
40+
if !isfile(newpython)
41+
@info """
42+
Python executable $newpython does not exists.
43+
This directory contains only the following files:
44+
$(join(readdir(dirname(newpython)), '\n'))
45+
"""
4246
end
4347
@test isfile(newpython)
4448

0 commit comments

Comments
 (0)