Last active
January 3, 2023 01:21
-
-
Save Luthaf/368a23981c8ec095c3eb to your computer and use it in GitHub Desktop.
Using pip to install python dependencies for Julia.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using PyCall | |
# Change that to whatever packages you need. | |
const PACKAGES = ["pyyaml"] | |
# Import pip | |
try | |
@pyimport pip | |
catch | |
# If it is not found, install it | |
get_pip = joinpath(dirname(@__FILE__), "get-pip.py") | |
download("https://bootstrap.pypa.io/get-pip.py", get_pip) | |
run(`$(PyCall.python) $get_pip --user`) | |
end | |
@pyimport pip | |
args = UTF8String[] | |
if haskey(ENV, "http_proxy") | |
push!(args, "--proxy") | |
push!(args, ENV["http_proxy"]) | |
end | |
push!(args, "install") | |
push!(args, "--user") | |
append!(args, PACKAGES) | |
pip.main(args) |
Calling pip.main
does not works at least as of pip v19.0.3, but probably starting around v10. See https://stackoverflow.com/questions/12332975/installing-python-module-within-code. Here's what I do instead:
import PyCall: pyimport
# See https://stackoverflow.com/questions/12332975/installing-python-module-within-code.
const PIP_PACKAGES = ["taichi", "matplotlib"]
sys = pyimport("sys")
subprocess = pyimport("subprocess")
subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", "--upgrade", "--force-reinstall", PIP_PACKAGES...])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've tried this and the
@pyimport pip
fails on Travis, it then tries to install PIP and decides it already exists. Any idea?