|
| 1 | +using PyCall, Test |
| 2 | + |
| 3 | + |
| 4 | +function test_venv_has_python(path) |
| 5 | + newpython = PyCall.python_cmd(venv=path).exec[1] |
| 6 | + if !isfile(newpython) |
| 7 | + @info """ |
| 8 | + Python executable $newpython does not exists. |
| 9 | + This directory contains only the following files: |
| 10 | + $(join(readdir(dirname(newpython)), '\n')) |
| 11 | + """ |
| 12 | + end |
| 13 | + @test isfile(newpython) |
| 14 | +end |
| 15 | + |
| 16 | + |
| 17 | +function test_venv_activation(path) |
| 18 | + newpython = PyCall.python_cmd(venv=path).exec[1] |
| 19 | + |
| 20 | + # Run a fresh Julia process with new Python environment |
| 21 | + code = """ |
| 22 | + $(Base.load_path_setup_code()) |
| 23 | + using PyCall |
| 24 | + println(PyCall.pyimport("sys").executable) |
| 25 | + println(PyCall.pyimport("sys").exec_prefix) |
| 26 | + println(PyCall.pyimport("pip").__file__) |
| 27 | + """ |
| 28 | + # Note that `pip` is just some arbitrary non-standard |
| 29 | + # library. Using standard library like `os` does not work |
| 30 | + # because those files are not created. |
| 31 | + env = copy(ENV) |
| 32 | + env["PYCALL_JL_RUNTIME_PYTHON"] = newpython |
| 33 | + jlcmd = setenv(`$(Base.julia_cmd()) --startup-file=no -e $code`, env) |
| 34 | + if Sys.iswindows() |
| 35 | + # Marking the test broken in Windows. It seems that |
| 36 | + # venv copies .dll on Windows and libpython check in |
| 37 | + # PyCall.__init__ detects that. |
| 38 | + @test_broken begin |
| 39 | + output = read(jlcmd, String) |
| 40 | + sys_executable, exec_prefix, mod_file = split(output, "\n") |
| 41 | + newpython == sys_executable |
| 42 | + end |
| 43 | + else |
| 44 | + output = read(jlcmd, String) |
| 45 | + sys_executable, exec_prefix, mod_file = split(output, "\n") |
| 46 | + @test newpython == sys_executable |
| 47 | + @test startswith(exec_prefix, path) |
| 48 | + @test startswith(mod_file, path) |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | + |
| 53 | +@testset "virtualenv activation" begin |
| 54 | + pyname = "python$(pyversion.major).$(pyversion.minor)" |
| 55 | + if Sys.which("virtualenv") === nothing |
| 56 | + @info "No virtualenv command. Skipping the test..." |
| 57 | + elseif Sys.which(pyname) === nothing |
| 58 | + @info "No $pyname command. Skipping the test..." |
| 59 | + else |
| 60 | + mktempdir() do tmppath |
| 61 | + if PyCall.pyversion.major == 2 |
| 62 | + path = joinpath(tmppath, "kind") |
| 63 | + else |
| 64 | + path = joinpath(tmppath, "ϵνιℓ") |
| 65 | + end |
| 66 | + run(`virtualenv --python=$pyname $path`) |
| 67 | + test_venv_has_python(path) |
| 68 | + |
| 69 | + newpython = PyCall.python_cmd(venv=path).exec[1] |
| 70 | + venv_libpython = PyCall.find_libpython(newpython) |
| 71 | + if venv_libpython != PyCall.libpython |
| 72 | + @info """ |
| 73 | + virtualenv created an environment with incompatible libpython: |
| 74 | + $venv_libpython |
| 75 | + """ |
| 76 | + return |
| 77 | + end |
| 78 | + |
| 79 | + test_venv_activation(path) |
| 80 | + end |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | + |
| 85 | +@testset "venv activation" begin |
| 86 | + # In case PyCall is built with a Python executable created by |
| 87 | + # `virtualenv`, let's try to find the original Python executable. |
| 88 | + # Otherwise, `venv` does not work with this Python executable: |
| 89 | + # https://bugs.python.org/issue30811 |
| 90 | + sys = PyCall.pyimport("sys") |
| 91 | + if hasproperty(sys, :real_prefix) |
| 92 | + # sys.real_prefix is set by virtualenv and does not exist in |
| 93 | + # standard Python: |
| 94 | + # https://github.com/pypa/virtualenv/blob/16.0.0/virtualenv_embedded/site.py#L554 |
| 95 | + candidates = [ |
| 96 | + PyCall.venv_python(sys.real_prefix, "$(pyversion.major).$(pyversion.minor)"), |
| 97 | + PyCall.venv_python(sys.real_prefix, "$(pyversion.major)"), |
| 98 | + PyCall.venv_python(sys.real_prefix), |
| 99 | + PyCall.pyprogramname, # must exists |
| 100 | + ] |
| 101 | + python = candidates[findfirst(isfile, candidates)] |
| 102 | + else |
| 103 | + python = PyCall.pyprogramname |
| 104 | + end |
| 105 | + |
| 106 | + if PyCall.conda |
| 107 | + @info "Skip venv test with conda." |
| 108 | + elseif !success(PyCall.python_cmd(`-c "import venv"`, python=python)) |
| 109 | + @info "Skip venv test since venv package is missing." |
| 110 | + else |
| 111 | + mktempdir() do tmppath |
| 112 | + if PyCall.pyversion.major == 2 |
| 113 | + path = joinpath(tmppath, "kind") |
| 114 | + else |
| 115 | + path = joinpath(tmppath, "ϵνιℓ") |
| 116 | + end |
| 117 | + # Create a new virtual environment |
| 118 | + run(PyCall.python_cmd(`-m venv $path`, python=python)) |
| 119 | + test_venv_has_python(path) |
| 120 | + test_venv_activation(path) |
| 121 | + end |
| 122 | + end |
| 123 | +end |
0 commit comments