简体   繁体   中英

How to use in Julia a Python package that is not available in Anaconda and needs to be installed via pip

I use Julia 1.6.0 (beta as of today) and would like to use a Python package plfit via PyCall .

Unfortunately, plfit is not available in Anaconda and hence I cannot install it using Conda module:

julia> using Conda

julia> Conda.add("plfit")
[ Info: Running `conda install -y plfit` in root environment
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - plfit

...

How can I install this package from Julia?

You can run pip from Julia via the PyCall module.

Additionally, the package plfit has a reference to a cython package that is not present in Python in-built Julia and does not get installed automatically. Hence you need to do:

using PyCall
run(`$(PyCall.python) -m pip install --upgrade cython`)
run(`$(PyCall.python) -m pip install plfit`)

Now the plfit package will be available for use from Julia:

julia> plfit = pyimport("plfit");

julia> x=rand(1000);

julia> myplfit = plfit.plfit(x)
PYTHON plfit executed in 0.064029 seconds
xmin: 0.397582 n(>xmin): 625 alpha: 2.92546 +/- 0.0770183   Log-Likelihood: 36.3511   ks: 0.197384 p(ks): 4.09676e-22
PyObject <plfit.plfit.plfit object at 0x0000000064871A90>

Finally, note that sometimes some packages installed this way might have problems finding binary Python dependencies (not the case with plfit). In that case you need to add the folder %HOMEPATH%\.julia\conda\3\Library\bin to your PATH environment variable (replace %HOMEPATH%\.julia with JULIA_DEPOT_PATH or an appropriate system path depending on your configuration and platform).

You can use pip also with Conda.jl (comes with PyCall):

https://github.com/JuliaPy/Conda.jl#conda-and-pip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM