# Any working way to call Python from Julia compiled app?

**URL:** <https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423>\
**Category:** General Usage\
**Tags:** pycall, compilation, python, package-compiler, pythoncall\
**Created:** [April 13, 2022, 9:32am UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423 "2022-04-13T09:32:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [April 13, 2022, 9:32am UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/1 "2022-04-13T09:32:06Z")

</div>

I tried to compile a relocatable app with `PackageCompiler.create_app` and call Python from it.

1. With PyCall.jl - there is a problem that compiled binary has an absolute path to python library on dev machine (on my Windows machine it is `C:\Users\gvg\.julia\conda\3\python39.dll`), so when I’m on another machine with different user - it can’t find the python by this path. See issue here: [https://github.com/JuliaPy/PyCall.jl/issues/981](https://github.com/JuliaPy/PyCall.jl/issues/981)
2. With PythonCall.jl - there is another error after compilation, looks like it can’t find `Manifes.toml` in compiled project files. See issue here: [https://github.com/cjdoris/PythonCall.jl/issues/146](https://github.com/cjdoris/PythonCall.jl/issues/146)

Any other ways I’ve missed?  
With option 1, maybe I can set another python path before compilation, so it is not bound to a specific user?

---

<div class="post-metadata">

**Author:** ![Kirby\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirby_zhang/32/36204_2.png) [@Kirby\_Zhang](https://discourse.julialang.org/u/Kirby_Zhang)\
**Post date:** [May 27, 2022, 7:42am UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/2 "2022-05-27T07:42:32Z")

</div>

I’m also finding PythonCall and PackageCompiler not working well together. It doesn’t use the JULIA\_PYTHONCALL\_EXE environment variable when compiled. For large sysimage with all packages included, it failed all together when starting julia with the compiled image:

> julia\>julia --sysimage=mysysimage.so  
> fatal: error thrown and no exception handler available.  
> InitError(mod=:C, error=AssertionError(msg=“CTX.which == :PyCall”))  
> #34 at ….julia\packages\PythonCall\XgP8G\src\cpython\context.jl:135  
> with\_gil at ….julia\packages\PythonCall\XgP8G\src\cpython\gil.jl:10 [inlined]  
> with\_gil at ….julia\packages\PythonCall\XgP8G\src\cpython\gil.jl:9 [inlined]  
> init\_context at ….julia\packages\PythonCall\XgP8G\src\cpython\context.jl:132  
> **init** at ….julia\packages\PythonCall\XgP8G\src\cpython\CPython.jl:21  
> jfptr\_\_\_init\_\_\_70514 at …\julia\mysysimage.so (unknown line)  
> jl\_apply at /cygdrive/c/buildbot/worker/package\_win64/build/src\julia.h:1788 [inlined]  
> jl\_module\_run\_initializer at /cygdrive/c/buildbot/worker/package\_win64/build/src\toplevel.c:73  
> \_finish\_julia\_init at /cygdrive/c/buildbot/worker/package\_win64/build/src\init.c:796  
> jl\_repl\_entrypoint at /cygdrive/c/buildbot/worker/package\_win64/build/src\jlapi.c:695  
> mainCRTStartup at /cygdrive/c/buildbot/worker/package\_win64/build/cli\loader\_exe.c:42  
> BaseThreadInitThunk at C:\WINDOWS\System32\KERNEL32.DLL (unknown line)  
> RtlUserThreadStart at C:\WINDOWS\SYSTEM32\ntdll.dll (unknown line)

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 27, 2022, 7:50am UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/3 "2022-05-27T07:50:10Z")

</div>

I’ve rencently got PyCall.jl portable by replacing the dll finding process with reading environment variables.

Conda.jl is harder to get work. I left it remain the wrong configs (local configs), but replacing all related variables with reading references which will be setup when ` __init__ ` PyCall module.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [June 28, 2022, 1:58am UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/4 "2022-06-28T01:58:51Z")

</div>

PythonCall now supports this.

I made [this small package](https://github.com/thautwarm/JuliaPythonAdaptor.jl) for configuration.

Things will work smoothly after [this PR](https://github.com/cjdoris/PythonCall.jl/pull/192) gets merges

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [June 28, 2022, 2:27am UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/5 "2022-06-28T02:27:10Z")

</div>

I made a [similar PR to PyCall.jl](https://github.com/JuliaPy/PyCall.jl/pull/991). It relies on another package as well, CachePath.jl. It would be great to put together a doc on the pros and cons of various approaches, including startup times, etc. Many of the important questions are not documented (or not clearly), so it will take a bit of work.

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [July 1, 2022, 12:27pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/6 "2022-07-01T12:27:27Z")

</div>

I’ve tried to add JuliaPythonAdaptor into this small package to test compilation, but it failed with the same error as in Readme.md:

> **[GitHub - sairus7/PythonCall\_tst.jl: MWP for PythonCall bug issue](https://github.com/sairus7/PythonCall_tst.jl)**
>
> MWP for PythonCall bug issue . Contribute to sairus7/PythonCall\_tst.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [July 1, 2022, 6:04pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/7 "2022-07-01T18:04:53Z")

</div>

The package has now run into some issue and fails at configuring when loaded as a precompiled package. If precompiled, JuliaPythonAdaptor is always loaded after PythonCall…

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [July 1, 2022, 6:23pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/8 "2022-07-01T18:23:43Z")

</div>

Install JuliaPythonAdaptor via pip and try

```julia
python -c "import JuliaPythonAdaptor;import os;del os.environ['JULIA_PYTHONCALL_LIBPTR'];os.system('path/to/you_app_exe')"

```

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [July 1, 2022, 6:26pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/9 "2022-07-01T18:26:25Z")

</div>

On the python side?  
Path to which executable should I provide at `os.system('path/to/you_exe')`, python or julia?

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [July 1, 2022, 6:31pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/10 "2022-07-01T18:31:04Z")

</div>

The path to the executable you built with PackageCompiler.

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [July 1, 2022, 6:48pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/11 "2022-07-01T18:48:02Z")

</div>

Seems like python doesn’t recognize package name:

```julia
PS I:\> python -c "import JuliaPythonAdaptor;import os;del os.environ['JULIA_PYTHONCALL_LIBPTR'];os.system('C:\\_KTAuto\\backend\\conda\\3\\python.exe')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'JuliaPythonAdaptor'

```

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [July 1, 2022, 7:01pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/12 "2022-07-01T19:01:54Z")

</div>

Ok, now I try to install it with pip and get the following:

```julia
PS I:\> python -m pip install https://github.com/thautwarm/JuliaPythonAdaptor.jl
Collecting https://github.com/thautwarm/JuliaPythonAdaptor.jl
  Downloading https://github.com/thautwarm/JuliaPythonAdaptor.jl
     \ 183.2 kB 381.8 kB/s 0:00:00
  ERROR: Cannot unpack file C:\Users\gvg\AppData\Local\Temp\pip-unpack-u150j3_z\JuliaPythonAdaptor.jl (downloaded from C:\Users\gvg\AppData\Local\Temp\pip-req-build-wl7iqp0o, content-type: text/html; charset=utf-8); cannot detect archive format
ERROR: Cannot determine archive format of C:\Users\gvg\AppData\Local\Temp\pip-req-build-wl7iqp0o

```

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [July 1, 2022, 7:09pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/13 "2022-07-01T19:09:35Z")

</div>

Have you tried `pip install JuliaPythonAdaptor`?

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [July 1, 2022, 9:36pm UTC](https://discourse.julialang.org/t/any-working-way-to-call-python-from-julia-compiled-app/79423/14 "2022-07-01T21:36:17Z")

</div>

Ok, I have installed it on python side and re-compiled app once more, but it keeps running with the same error. Should I change something in this particular script?

```julia
python -c "import JuliaPythonAdaptor;import os;del os.environ['JULIA_PYTHONCALL_LIBPTR'];os.system('C:\\_KTAuto\\backend\\conda\\3\\python.exe')"

julia --project=@. --startup-file=no -e '
ENV["JP_ADAPTOR_PY_EXE"] = "C:\\_KTAuto\\backend\\conda\\3\\python.exe";
using PackageCompiler;
PackageCompiler.create_app(pwd(), "PythonCallCompiled";
    cpu_target="generic;sandybridge,-xsaveopt,clone_all;haswell,-rdrnd,base(1)",
    include_transitive_dependencies=false,
    filter_stdlibs=true,
    precompile_execution_file=["test/runtests.jl"])
'

```

Or maybe there are some variables that I can check after importing Julia package?
