PyCall issues: possible to choose conda environment as path?

Hi,

i am struggling to get PyCall running properly.

My understanding is that PyCall uses its own internal julia conda environment. Using this was working initially. However, when I then tried to set an alternative python path (using ENV["PYTHON"] this did not work. Now I cannot get PyCall up and running anymore at all.

Below is my code (MWE) where I tried to set my own python path using an existing conda environment.

using Pkg 
ENV["PYTHON"] = "~/opt/anaconda3/envs/mlbasic/bin/python"
using PyCall
Pkg.build("PyCall")

# Some example code
so = pyimport("scipy.optimize")
so.newton(x -> cos(x) - x, 1)

Running this code gives me the following error:

The terminal process “/Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia ‘-i’, ‘–banner=no’, ‘–project=/Users/name/Coding/Finance’, ‘/Users/name/.vscode/extensions/julialang.language-julia-1.0.10/scripts/terminalserver/terminalserver.jl’, ‘/var/folders/cj/9s881x057_12d_qyp3jcmb380000gn/T/vsc-julia-repl-49636’, ‘/var/folders/cj/9s881x057_12d_qyp3jcmb380000gn/T/vsc-jl-cr-49636’, ‘USE_REVISE=true’, ‘USE_PLOTPANE=true’, ‘DEBUG_MODE=undefined’” terminated with exit code: 2.

Can someone help me and explain to me how to fix this?

Thanks!

This is the wrong order. You need to set the environment and build PyCall (in a fresh Julia session), and only then can you import the PyCall module with using PyCall.

Also, you should give the full path — you can’t use ~ for the home directory.

(Note that you only need to build PyCall once with a given Python configuration. After that, you can just do using PyCall in any Julia session or script.)

2 Likes

Thanks for your swift response. And thanks for the hint. I was indeed struggling with the documentation on the part of the order.

However, when I change the order and the path I still get the same error message. I restarted VS Code and used a new repl session.

Any idea how to fix this?

Alternatively, wow can I delete PyCall package again so that I can start from scratch again?

This is what I did:

using Pkg 
ENV["PYTHON"] = "/Users/name/opt/anaconda3/envs/mlbasic/bin/python"
Pkg.build("PyCall")
using PyCall

# Some example code
so = pyimport("scipy.optimize")
so.newton(x -> cos(x) - x, 1)

Thanks

What was the output of the Pkg.build command?

Where can I find the output? The build command seemed to work. It only crashes once I use PyCall functions.

I have now found that before crashing the following message flashes really quickly (which is why I didn’t see it before).

Intel MKL FATAL ERROR: Cannot load libmkl_intel_thread.dylib

On stackoverflow I found this regarding the issue. I will try this and see if it helps.

Thanks

Since you are using anaconda it might also be this issue. This fix worked for me.

1 Like

Thanks, the link was helpful! I have managed to fix the error if I use Julia via the terminal by adding

export LD_LIBRARY_PATH=/Users/name/.julia/conda/3/lib:$LD_LIBRARY_PATH

to the .zshrc file.

But: if I use VS Code the error still remains. Although my user settings have
"terminal.integrated.shell.osx": "/bin/zsh"

So my assumption is that the same terminal settings apply.

Can someone help me and let me know what I need to change setting wise in VS Code to ensure that the same terminal settings are used?

Thanks!

An alternative is to activate your conda environment before starting Julia:

conda activate my_conda_env
julia
] build PyCall # must be repeated if the conda env is changed
using PyCall

Not sure if this also solves your VSCode issue if you start VSCode from a terminal with your conda env activated.

Thanks for the suggestion. This is unfortunately not working but made me realize something.

Using Julia REPL in VS Code is actually not the same as typing Julia in integrated terminal in VS Code. While the latter works fine, the former does not.

So using Julia REPL in VS Code (i.e. cmd + shift + p and then “julia repl”) crashes when using PyCall (irrespective of the python path). All other options work fine (i.e. using Julia with external or internal temrinal).

Any idea how I can fix this?

Thanks!

1 Like