PyError using PyCall in Julia - python

I am using PyCall to call Python in Julia.
My environment is set to my Anaconda distribution (xxx in the following) and PyCall builds correctly.
If I use
using PyCall
np = pyimport("numpy")
I get
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:
xxx\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 "xxx\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "xxx\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
Despite numpy is installed in the named directory.

Related

Python package dependency is causing an error

The folder Python37\site-packages\scipy\sparse\linalg\isolve\ inside Scipy package in Python has the following file: _iterative.cp37-win_amd64.pyd.
When I run a Python file, I get the following error :-
File "C:\......\Python37\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 10, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found
(I am new to Python on Windows)
As I cannot comment due to a lack of reputation:
Have you installed scypy with all the mentioned dependencies on their website?
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
(Taken from https://www.scipy.org/install.html)
This is how I was able to resolve the issue (on Windows) :-
1. I uninstalled Python using python.exe program.
2. Installed anaconda for Windows - it installs python along with dependencies and many common libraries.

Julia PyPlot not working [Conda, Python issues]

This is the error I get when I run my program. I am using Ubuntu 16.04 and had Python 2.7.12 installed. I installed anaconda hoping that would help solve the problem but it didn't. Now python -V gives me Python 2.7.14 :: Anaconda, Inc.
When I run Conda.list() inside julia, I do get "matplotlib" and "basemap"
(includes mpl_toolkits, I think) and when I run conda list outside julia (bash?) I get a longer list which also contains "matplotlib" and "basemap".
sys:1: UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.
The backend was *originally* set to u'Qt5Agg' by the following code: File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
line for line in traceback.format_stack()
WARNING: No working GUI backend found for matplotlib WARNING: Couldn't initialize pyplot. (might need to install it?) INFO: To do a standard install of pyplot, copy and run this:
if !Plots.is_installed("PyPlot")
Pkg.add("PyPlot") end withenv("PYTHON" => "") do
Pkg.build("PyPlot") end
# now restart julia!
ERROR: LoadError: InitError: PyError (ccall(#pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)
The Python package matplotlib.pyplot could not be found by pyimport. Usually this means that you did not install matplotlib.pyplot in the Python version being used by PyCall.
PyCall is currently configured to use the Python version at:
/home/myusername/anaconda2/bin/python
and you should use whatever mechanism you usually use (apt-get, pip, conda, etcetera) to install the Python package containing the matplotlib.pyplot 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 matplotlib.pyplot module, you can use `pyimport_conda("matplotlib.pyplot", PKG)`, where PKG is the Anaconda package the contains the module matplotlib.pyplot, or alternatively you can use the Conda package directly (via `using Conda` followed by `Conda.add` etcetera).
) <type 'exceptions.ImportError'> ImportError("/lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/../../.././libpng16.so.16)",) File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 29, in <module>
import matplotlib.colorbar File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
import matplotlib.contour as contour File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
import matplotlib.font_manager as font_manager File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
Stacktrace: [1] pyerr_check at /home/myusername/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]....
LD_LIBRARY_PATH="/home/myusername/.julia/v0.6/Conda/deps/usr/lib/" julia
did it!
Thanks Github!
I am also using Ubuntu 16.04 and use pyplot with julia. I installed matplotlib with pip and use it with the TK backend (the most light-weight and easy to install backend). Please try these shell commands to install python-tk and matplotlib in your home directory:
sudo apt-get install python-tk
unset PYTHON # to use the default python interpreter /usr/bin/python
pip2 install --user --upgrade matplotlib
julia --eval 'Pkg.build("PyCall")'
julia --eval 'Pkg.build("PyPlot")'
To use the default python interpreter, make sure that you do not set the environment variable PYTHON. The following should return nothing:
julia --eval '#show get(ENV,"PYTHON",nothing)'
If PyCall are PyPlot are not already installed, then use add instead of build:
julia --eval 'Pkg.add("PyCall")'
julia --eval 'Pkg.add("PyPlot")'

using netcdf without installation

I need to use netcdf but do not have install permission for python modules. I have downloaded netcdf-0.1.2.tar.gz from here: https://pypi.python.org/simple/netcdf/ and unzipped the tar ball. I have been following this stack overflow post in an attempt to use the module but have had no luck so far:
(Python) Use a library locally instead of installing it
here is what I have tried:
Installing virtualenv:
I do not have permission to do this
python setup.py install -- user:
again, I don't have permission
running my script with netcdf as my current working directory:
I tried this as well, here are the issues I have run into:
first I went into netcdf-0.1.2 and made a new file called asdf.py
which contains the following:
import netcdf
print("testing")
running python asdf.py gives the following error:
Traceback (most recent call last):
File "asdf.py", line 1, in <module>
import netcdf
File "/.../Downloads/netcdf-0.1.2/netcdf/__init__.py", line 1, in <module>
from netcdf import *
File "/.../Downloads/netcdf-0.1.2/netcdf/netcdf.py", line 1, in <module>
from netCDF4 import Dataset, numpy
ImportError: No module named netCDF4
I'm not sure how to fix this error, any help would be greatly appreciated
in case this is somehow relevant, the version of Linux I am using is 3.2.0-23-generic
also I have numpy installed already
Easest would be to install Anaconda or Miniconda with your user rights.
Anaconda already as netCDF4installed. In case of Miniconda install with:
conda install netcdf4
If you have Python 3 installed, then you will have the venv package in the standard library, so you do not need "virtualenv" to be installed for you separately (as would be the case with Python 2). Instead use python3 -mvenv , in a similar way to how you would use virtualenv, for example:
python3 -mvenv /path/to/my_venv
or to include any non-standard packages already installed on the system:
python3 -mvenv --system-site-packages /path/to/my_venv
After that, you should be able to activate the environment and pip install packages, e.g.
source /path/to/my_venv/bin/activate # for csh use activate.csh instead
pip install netCDF4
Remember to source the activate script at run time as well as installation time:
source /path/to/my_venv/bin/activate
python
and you should then find that in your python session you have the netCDF4 package available, e.g.
import netCDF4
my_dataset = netCDF4.Dataset('myfile.nc')
Of course, substitute the actual path in place of /path/to/my_venv above.
None of this requires any root privileges.
(And as someone else has suggested, another option for you is to use conda.)
I'd also like to highlight that the package is imported using capitals
import netCDF4 as nc
This might not matter on a mac, but for Windows it is key.

import numba on mac Sierra

I'm trying to install NUMBA on a mac machine (10.12.6).
I've tried with CONDA:
conda install numba
I'm getting this:
Fetching package metadata ...........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /Users/lefaa/miniconda2:
#numba 0.35.0 np113py27_6
This seems good, however when I try (using python 2.7):
python -c "from numba import jit"
I'm getting that the numba module isn't installed:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named numba
Do I have to configure something else?
It is likely that your path is not set up correctly and the python that conda installed numba into is not the one you are running. If you run which python you should get the one that's in your miniconda2 if everything is working properly. If you get a different python executable then you probably need to set your PATH environment variable. See:
https://conda.io/docs/user-guide/install/index.html#installing-conda-on-a-system-that-has-other-python-installations-or-packages
I solved the issue. Indeed, when installing with conda, conda installs the target package in python associated to conda (i.e, in this python '/Users/lefaa/miniconda2/bin/python'). So to import numba, it is necessary to lunch the python of miniconda.
However, to install numba on the main python (python2.7), one solution will be to install like this '/usr/local/bin/pip2.7 install numba'.

how to automatically install dependent modules used in a python app

I just started learning Python and a bit confused about how packages are distributed and installed. I am aware of helper scripts easy_install and pip which can be used to install the dependent modules,howerver I am not clear how to do with programatically,can someone help me on this?
How to install dependent modules automatically when running python applications? I have a dependency on subprocess32 and other modules,I want to automatically install them if they are not present....
File "script.py", line 6, in <module>
import subprocess32 as subprocess
ImportError: No module named subprocess32
I have looked at some posts online below but not clear...really appreciate guidance here
locallyoptimal.com/blog/2014/03/14/executable-python-scripts-via-entry-points/
Python packages installation in Windows
Easiest way to handle module installs within a program is with pip:
import pip
pip.main(["install", "numpy"])
For example will install numpy and its dependencies. You can also specify versions using numpy=xxx, or upgrade by passing "-upgrade" between those two above.

Categories