# How to use PythonCall with a previous Conda environment

**URL:** <https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419>\
**Category:** General Usage\
**Created:** [September 17, 2022, 9:55pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419 "2022-09-17T21:55:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [September 17, 2022, 9:55pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/1 "2022-09-17T21:55:15Z")

</div>

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

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

```

Then I install PythonCall.

```julia
(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
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.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 18, 2022, 2:29am UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/2 "2022-09-18T02:29:00Z")

</div>

Have you tried changing these settings before starting Julia?

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

> <https://github.com/conda-forge/julia-feedstock/blob/a203b65f479414d941f44ff0f14def5955cb228b/recipe/scripts/activate.sh#L38-L40>

In particular, we do the following:

```julia
export JULIA_CONDAPKG_BACKEND="System"
export JULIA_CONDAPKG_EXE=$CONDA_EXE

```

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [September 19, 2022, 5:49pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/3 "2022-09-19T17:49:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 19, 2022, 6:01pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/4 "2022-09-19T18:01:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [September 19, 2022, 6:07pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/5 "2022-09-19T18:07:04Z")

</div>

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](https://github.com/cmateu/galstreams)  
Best.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 19, 2022, 6:28pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/6 "2022-09-19T18:28:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [September 19, 2022, 7:00pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/7 "2022-09-19T19:00:15Z")

</div>

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

> [@mkitti](#):
>
> 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.

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

Thank you very much.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 20, 2022, 5:54am UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/8 "2022-09-20T05:54:27Z")

</div>

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](https://cjdoris.github.io/PythonCall.jl/stable/pythoncall/#If-you-don't-want-to-use-Conda)

If we set

```julia
ENV["JULIA_PYTHONCALL_EXE"] = "@PyCall"

```

or

```julia
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?

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [September 20, 2022, 11:55am UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/9 "2022-09-20T11:55:06Z")

</div>

Thank you very much @mkitti. With your commands together with this [issue](https://github.com/cjdoris/PythonCall.jl/issues/224) I have been able to make it work and understand the problem.

---

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [September 27, 2025, 2:35pm UTC](https://discourse.julialang.org/t/how-to-use-pythoncall-with-a-previous-conda-environment/87419/10 "2025-09-27T14:35:57Z")

</div>

Cheers,

I thought worth sharing my findings on a recent struggle for making a Conda environment to work with PythonCall. Specifically, I have a “tf” environment with TensorFlow installed on my system. Solution is as follows:

```julia-auto
homedir = ENV["HOME"] *"/"

ENV["JULIA_CONDAPKG_BACKEND"] = "Null"

ENV["JULIA_PYTHONCALL_EXE"] = homedir * "miniconda3/envs/tf/bin/python" # does NOT accept path with ~

ENV["JULIA_CONDAPKG_EXE"] = homedir * "miniconda3/envs/tf/bin" # accepts path with ~

```

To emphasize, the JULIA\_PYTHONCALL\_EXE does not accept paths with ~. The JULIA\_CONDAPKG\_EXE is not that demanding, and accepts paths with ~. As a best practice, make both paths explicit.

Regards,
