How to use PythonCall with a previous Conda environment

Hello,
I would be very gratefull to understand what is the problem. I followed the instructions:

julia > ENV["JULIA_CONDAPKG_BACKEND"] = "System"
julia > ENV["JULIA_CONDAPKG_EXE"] = "/home/mmestre/.conda/envs/cosmos/bin/python3.9"

Then I install PythonCall.

(RiosDelCosmos) pkg> st
      Status `~/casa/work/2022/RiosDelCosmos/Project.toml`
  [6099a3de] PythonCall v0.9.6

When I try to use it I get an error (I do have astropy installed in the cosmos environment):

julia> using PythonCall
julia> ast = pyimport("astropy")
ERROR: Python: ModuleNotFoundError: No module named 'astropy'
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/DqZCE/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/DqZCE/src/err.jl:10 [inlined]
 [3] pyimport(m::String)
   @ PythonCall ~/.julia/packages/PythonCall/DqZCE/src/concrete/import.jl:11
 [4] top-level scope
   @ REPL[5]:1

I also tried with
julia > ENV["JULIA_CONDAPKG_EXE"] = "/home/mmestre/.conda/envs/cosmos/bin/python"
obtaining the same result.

Another question, when I use previus Conda environment, does CondaPkg also takes care of everything?
Thank you very much in advance.

Have you tried changing these settings before starting Julia?

Here is the conda activate script for Julia that we use conda-forge:

In particular, we do the following:

export JULIA_CONDAPKG_BACKEND="System"
export JULIA_CONDAPKG_EXE=$CONDA_EXE

Thanks @mkitti. How do you execute the file activate.sh ? Every time you start a Julia sesion or only once?
Besides I would like to know what is the value of $CONDA_EXE in you case.

If you are using conda then this environment variable should be set. Thr activate script is typically used everytime you invoke conda activate

Sorry I have a mess. Could you tell which are the steps I should follow in order to use a conda environment in PythonCall within a Julia repl or VS code ?
Thank you very much.

Appart from that I would like learn how to use CondaPkg and create a fresh conda environment, but I need to install a package that uses a setup.py file : GitHub - cmateu/galstreams: Milky Way Streams Footprint Library and Toolkit for Python
Best.

If you were using the julia package in conda-forge, that activate script would be copied into $CONDA_PREFIX/etc/conda/activate.d and be run automatically. Because you are not using the julia package in conda-forge, you need copy that script into that folder. Then deactivate and activate the conda environment.

Then CondaPkg.jl should then pick up on the environment variables set, and use the existing conda environment. It will also configure Conda.jl and use a Julia depot within your conda environment.

If you were using the julia package in conda-forge, that activate script would be copied into $CONDA_PREFIX/etc/conda/activate.d and be run automatically. Because you are not using the julia package in conda-forge, you need copy that script into that folder. Then deactivate and activate the conda environment

Ok, I hadn’t realized that the ENV variables inside the julia repl are the same environment variables of a normal terminal. Now it is a bit clearer. However, I have followed this steps and I still cant use a python package:

julia> ENV["JULIA_CONDAPKG_EXE"]
"/opt/conda/bin/conda"
julia> ENV["JULIA_CONDAPKG_BACKEND"]
"System"

julia> using PythonCall

julia> astro =pyimport("astropy")
ERROR: Python: ModuleNotFoundError: No module named 'astropy'
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/DqZCE/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/DqZCE/src/err.jl:10 [inlined]
 [3] pyimport(m::String)
   @ PythonCall ~/.julia/packages/PythonCall/DqZCE/src/concrete/import.jl:11
 [4] top-level scope
   @ REPL[11]:1

And astropy is certainlly installed in the previously activated conda enviroment.

Should I install Conda.jl to use CondaPkg.jl ?

Thank you very much.

Conda.jl and CondaPkg.jl are separate entities. PyCall.jl uses Conda.jl and PythonCall.jl uses CondaPkg.jl by default.

Looking at it does seem that CondaPkg.jl tries to activate it’s own environment and doesn’t seem to recognize when it’s already in one.

One way to change the situation is to set JULIA_PYTHONCALL_EXE according to Guide · PythonCall & JuliaCall

If we set

ENV["JULIA_PYTHONCALL_EXE"] = "@PyCall"

or

ENV["JULIA_PYTHONCALL_EXE"] = joinpath(ENV["CONDA_PREFIX"], "bin", "python")

before launching PythonCall, then it seems to just use the current conda environment.

@cjdoris, is this the only way to use the currently activated conda environment rather than making a new one?

Thank you very much @mkitti. With your commands together with this issue I have been able to make it work and understand the problem.