Import package from python

Hi,
I have a package installed in anaconda, and I want to use in Julia.
I did the following:

  • set python environment in Julia to be anaconda python
  • and rebuilt pycall
  • restart Julia
    I still have an importerror!
    Any suggession?

Thanks

Hi @nadaa and welcome to the julia discourse! Are you able to post the julia code you’re calling to import your python module and the exact error you’re getting? And are you unable to import any python modules into julia, or are you just having trouble with a particular module?

It’s hard to know what the problem might be without more information. For some general tips on getting help on discourse, check out: Please read: make it easier to help you

5 Likes

Hi @Pbellive,
Import works with python built in packages only. For example, I tried to import pandas but got an error.

     using PyCall
    pandas = pyimport("pandas")

“ERROR: LoadError: PyError (PyImport_ImportModule
The Python package pandas could not be found by pyimport. Usually this means
that you did not install pandas in the Python version being used by PyCall”

Thank you

You can try pandas = pyimport_conda("pandas").

1 Like

Probably you didn’t configure PyCall to use the python installation where you installed pandas. Look at PyCall.python to see what Python installation PyCall is configured to use.

1 Like

Thanks @tkf, I got an error
ERROR: LoadError: MethodError: no method matching pyimport_conda(::String)

Thanks. Python path in Julia is the same as Anaconda!

Was there more output from the pyimport error message?

If you run the Python from println(PyCall.python) — that is, paste that path exactly into the Terminal (don’t just run python) and type import pandas, does it work?

1 Like

I paste the path from println(PyCall.python) into the terminal (python Repl works).
This is the whole error:

The Python package pandas could not be found by pyimport. Usually this means
that you did not install pandas in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

C:\Users\na\AppData\Local\Continuum\anaconda3\python.exe

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the pandas 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 pandas module, you can use pyimport_conda("pandas", PKG),
where PKG is the Anaconda package the contains the module pandas,
or alternatively you can use the Conda package directly (via
using Conda followed by Conda.add etcetera).

) <class ‘ImportError’>
ImportError(‘Unable to import required dependencies:\nnumpy: DLL load failed: Das angegebene Modul wurde nicht gefunden.’)
File “C:\Users\na\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas_init_.py”, line 17, in
“Unable to import required dependencies:\n” + “\n”.join(missing_dependencies)

This is the key line. What is the error when you try to pyimport("numpy")?

ERROR: LoadError: PyError (PyImport_ImportModule

The Python package numpy could not be found by pyimport. Usually this means
that you did not install numpy in the Python version being used by PyCall.

Hi,

I had a similar issue, where I have different Python versions installed with Anaconda, and I needed to call them from within Julia. This is what I ended up with. If I remember OK, everytime I change the Python I use I have to rebuild PyCall. If you always use the same one, I guess you are ok building it only once.

ENV["PYTHON"]="/home/angelv/local/prog_langs/anaconda3/envs/chianti/bin/python"                                                                                                                                   
using Pkg
pkg"build PyCall"
using PyCall

chio = pyimport("ChiantiPy.tools.io")
ch   = pyimport("ChiantiPy.core")

Cheers,

2 Likes

Again, give the full error. The low-level error reported by Python will be at the end.

1 Like

Building Conda ─→ C:\Users\na\.julia\packages\Conda\3rPhK\deps\build.log
Building PyCall → C:\Users\na\.julia\packages\PyCall\zqDXB\deps\build.log
ERROR: LoadError: PyError (PyImport_ImportModule

The Python package numpy could not be found 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:\Users\na\AppData\Local\Continuum\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 the contains the module numpy,
or alternatively you can use the Conda package directly (via
using Conda followed by Conda.add etcetera).

) <class ‘ImportError’>
ImportError(‘DLL load failed: Das angegebene Modul wurde nicht gefunden.’)
File “C:\Users\na\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy_init_.py”, line 140, in
from . import _distributor_init
File “C:\Users\na\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy_distributor_init.py”, line 34, in
from . import _mklinit

Are you using MKL.jl, or some other MKL-enabled Julia version? In this case it’s possible that you are seeing a conflict between the MKL library loaded by Python and one in Julia?

1 Like

Hi, how can I check if MKL.jl exists?
and how to uninstall it?

Thanks

If you’re not using MKL, then it doesn’t matter whether it is installed.

1 Like

Hello, not sure if you have figured it out. I had a similar problem, this works for me. Uninstall Julia and reinstall it to the same folder where Anaconda is. Hope this helps!

1 Like

It worked for me too!