Skip to content

Instantly share code, notes, and snippets.

@Luthaf
Last active January 3, 2023 01:21
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Luthaf/368a23981c8ec095c3eb to your computer and use it in GitHub Desktop.
Save Luthaf/368a23981c8ec095c3eb to your computer and use it in GitHub Desktop.
Using pip to install python dependencies for Julia.
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)
@sylvaticus
Copy link

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..

@Luthaf
Copy link
Author

Luthaf commented Apr 22, 2017

You need to put this file as your package deps/build.jl file, that will be run when someone install this package.

@cortner
Copy link

cortner commented Oct 9, 2017

I've tried this and the @pyimport pip fails on Travis, it then tries to install PIP and decides it already exists. Any idea?

@samuela
Copy link

samuela commented Oct 20, 2020

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