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 hidden or 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) |
You need to put this file as your package deps/build.jl
file, that will be run when someone install this package.
I've tried this and the @pyimport pip
fails on Travis, it then tries to install PIP and decides it already exists. Any idea?
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
Hello, how to use this file ? Is it necessary to put it somewhere and it will be automatically executed when an user install my package ?
My package depends upon a python package that is not available in conda (but it is trough pip) and I don' know where to start to guarantee its presence to users..