I’ve setup PyCall (note I have an anaconda distribuiton)
ENV["PYTHON"] = "C:\Anaconda3\python.exe"
Pkg.build("PyCall")
In vscode when I open a new terminal (I’m on windows so a power shell opens) and then start julia
(base) PS C:\dev\Apollo> julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.9.0-rc1 (2023-03-07)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
and activate my environment with ] activate .
The following code works just fine
julia> using PyCall
julia> const np = pyimport("numpy")
PyObject <module 'numpy' from 'C:\\Anaconda3\\lib\\site-packages\\numpy\\__init__.py'>
However now if I put this code in a julia scipt, myfile.jl
myfile.jl:
using PyCall
const np = pyimport("numpy")
and run the script using the “Julia: Execute active File in REPL” command. This will start a new Julia REPL but now I get the following error
C:\Anaconda3\lib\site-packages\numpy\__init__.py:148: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
ERROR: PyError (PyImport_ImportModule
The Python package numpy could not be imported by pyimport. Usually this means
that you did not install numpy in the Python version being used by PyCall.
PyCall is currently configured to use the Python version at:
C:\Anaconda3\python.exe
and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the numpy module.
One alternative is to re-configure PyCall to use a different Python
version on your system: set ENV["PYTHON"] to the path/name of the python
executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.
Another alternative is to configure PyCall to use a Julia-specific Python
distribution via the Conda.jl package (which installs a private Anaconda
Python distribution), which has the advantage that packages can be installed
and kept up-to-date via Julia. As explained in the PyCall documentation,
set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then,
To install the numpy module, you can use `pyimport_conda("numpy", PKG)`,
where PKG is the Anaconda package that contains the module numpy,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).
) <class 'ImportError'>
ImportError('\n\nIMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!\n\nImporting the numpy C-extensions failed. This error can happen for\nmany reasons, often due to issues with your setup or how NumPy was\ninstalled.\n\nWe have compiled some common reasons and troubleshooting tips at:\n\n https://numpy.org/devdocs/user/troubleshooting-importerror.html\n\nPlease note and check the following:\n\n * The Python version is: Python3.9 from "C:\\Julia-1.9.0-rc1\\bin\\julia.exe"\n * The NumPy version is: "1.21.5"\n\nand make sure that they are the versions you expect.\nPlease carefully study the documentation linked above for further help.\n\nOriginal error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.\n')
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 150, in <module>
from . import core
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
raise ImportError(msg)
Stacktrace:
[1] pyimport(name::String)
@ PyCall C:\Users\myusername\.julia\packages\PyCall\twYvK\src\PyCall.jl:558
[2] top-level scope
@ c:\dev\Apollo\src\qvfx\myfile.jl:3
Now one thing I noticed between the two REPL’s is the environment variables that are loaded
println(ENV)
in the working REPL spits out all my environment variables including
CONDA_DEFAULT_ENV=base
CONDA_EXE=C:\Anaconda3\Scripts\conda.exe
CONDA_PREFIX=C:\Anaconda3
CONDA_PROMPT_MODIFIER=(base)
CONDA_PYTHON_EXE=C:\Anaconda3\python.exe
CONDA_SHLVL=1
…
_CONDA_EXE=C:\Anaconda3\Scripts\conda.exe
_CONDA_ROOT=C:\Anaconda3
But when I println(ENV)
from the non working REPL (started from “Julia: Execute active File in REPL”) these environment variables are not present. I’m thinking this could be the issue so I modified myfile.jl by adding all these additional environment variables (including the changes made to the path)
myfile:
ENV["CONDA_DEFAULT_ENV"]="base"
ENV["CONDA_EXE"]=raw"C:\Anaconda3\Scripts\conda.exe"
ENV["CONDA_PREFIX"]=raw"C:\Anaconda3"
ENV["CONDA_PROMPT_MODIFIER"]="(base)"
ENV["CONDA_PYTHON_EXE"]=raw"C:\Anaconda3\python.exe"
ENV["CONDA_SHLVL"]=1
ENV["_CONDA_EXE"]=raw"C:\Anaconda3\Scripts\conda.exe"
ENV["_CONDA_ROOT"]=raw"C:\Anaconda3"
ENV["PATH"] = raw"C:\Anaconda3;C:\Anaconda3\Library\mingw-w64\bin;C:\Anaconda3\Library\usr\bin;C:\Anaconda3\Library\bin;C:\Anaconda3\Scripts;C:\Anaconda3\bin;C:\Anaconda3\condabin;C:\Program Files\WindowsApps\Microsoft.PowerShell_7.3.3.0_x64__8wekyb3d8bbwe;" * ENV["PATH"]
using PyCall
const np = pyimport("numpy")
And viola it works. So my question is what is adding all these env variables to one REPL but not the other (the one using the “Julia: Execute active File in REPL” command) and how can I fix it so these variables are automatically added.
Thanks