-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@pyimport numpy fails #65
Comments
Do you have some kind of virtualenv setup, or multiple versions of Python? |
No virtualenv, only one version of python on the harddrive. Any suggestions what I could try? |
Try
to see if this is related to #48. |
I tried this, but no change. I also tried numpy 1.8.0, same behaviour. |
Hello, I still did not find the reason for this problem, because on other machines the newest version of numpy and matplotlib are working fine. Perhaps I should mention that this laptop uses a haswell CPU. |
I tried using a clean virtual environment: sudo pip install virtualenv virtualenv /home/$USER/python-julia --no-site-packages source /home/ufechner/python-julia/bin/activate This is working, but any newer version of numpy does not work. So yes, I have a workaround but no, the problem itself is not solved yet. |
Just wanted to add that I am seeing the same with numpy 1.8. |
Same error here, on my desktop at work, which has the Canopy python distribution with latest version of all packages (numpy, scipy, matplotlib, ...). On my laptop I have installed python via homebrew and everything is working fine. |
PyCall doesn't work with Canopy (#42), so that's probably a separate issue. |
I noticed this afterwards; my apologies for polluting this issue. However, on my specific machine, the rest of Canopy's python seemed to do ok, it was only this exact same error "ImportError('cannot import name scimath',)" that was making PyPlot fail. I have removed Canopy and switched to Homebrew and everything works, as expected. |
If you google |
A workaround for the google app engine is described here: I do not know how to apply this for an Ubuntu/ pip based environment, though. |
@stevengj The issue appears to be about circular imports. Here is how circular imports are supposed to work: http://stackoverflow.com/a/744403/744071 To demonstrate the issue: Make a virtualenv and install numpy==1.9.1. Then go to where it was installed, and add the following line near the top of
That looks good. Now run the same from julia (v0.3.2) using PyCall (version 0.7.3):
As you can see, the line I don't know enough about Python's import mechanism to debug this further, but I hope it leads you to a solution. Update: Things get weirder. Consider this: a.py:
b.py:
If I run this from the python shell, things work as expected:
But if I run it as a script, it does something else (I don't understand why):
And Julia / PyCall behaves like the former:
So in this case Julia / PyCall seems to handle the circular imports properly. |
My first guess was that you have multiple versions of numpy etc. installed and it is getting confused about which one to import... but that doesn't explain the multiple print lines. I dunno... |
The thing is, it is not PyCall that is "handling" the circular imports. PyCall just calls |
Can you try |
@stevengj I found the cause of the problem. First I realized that
Then I realized that
The culprit is the line BTW, if anyone is getting Yay, I can finally use PyPlot from julia! |
Thanks for looking into this. (The need for the numpy ufunc thing will go away in Julia 0.4; see #101.) However, I don't understand the problem, since Python has been initialized when the Could it possibly be that Python is reloading the global const numpy_module = try
pyimport("numpy")
catch
PyObject() # NumPy not available
end
global const ufuncType = numpy_module.o == C_NULL ? PyObject() : numpy_module["ufunc"] (Although I don't really believe this explanation — because Julia keeps a reference to You could also just move the (I'm not sure if anything can be done about the libgfortran thing; it's always a problem to have multiple incompatible Fortran compilers on your system; you have to be very careful about consistently linking the same one.) |
@stevengj I take back everything I said; it was a red herring. I just realized that |
Also ran into this, |
Just to add to the documentation of this: As of yesterday, running 04.-rc1 on my Ubuntu 14.10 (running Anaconda), I am having this issue. My particular version is that I cannot
Note that if I comment out the scimath import in |
I will also note that a simple C version #include <Python.h>
int
main(int argc, char *argv[])
{
PyObject *pModule;
char *pName = "numpy";
Py_Initialize();
pModule = PyImport_ImportModule(pName);
if (pModule != NULL) {
printf("Success!\n");
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", pName);
return 1;
}
Py_Finalize();
return 0;
} Runs successfully. |
Having same issue as @jmxpearson. Ubuntu 15.04 + pip2 installed libraries + Julia 0.4-rc1. |
Just for reference, I created a minimal Julia example that reproduces the issue.
works, while
fails. What could it be here, except ############## pyimport_test.jl ######################################
const stub = homedir() * "/anaconda"
const libname = @osx? "libpython2.7.dylib" : "libpython2.7.so"
const libpython = stub * "/lib/" * libname
################# from PyCall.jl #####################################
immutable PyObject_struct
ob_refcnt::Int
ob_type::Ptr{Void}
end
typealias PyPtr Ptr{PyObject_struct} # type for PythonObject* in ccall
macro pysym(func)
:(($func, libpython))
end
################# from PyCall.jl #####################################
# now, do some ccalling to initialize
ccall((@pysym :Py_InitializeEx), Void, (Cint,), 0)
modname = ARGS[1]
println("About to try loading library $modname")
modptr = ccall((@pysym :PyImport_ImportModule), PyPtr, (Ptr{Uint8},),
bytestring(modname))
if modptr == C_NULL
println("Could not load library.")
else
println("Success!")
end
ccall((@pysym :Py_Finalize), Void, ()) |
@jmxpearson, I really think it is some Python path thingy that is missing; it sounds like (It sucks that the hardest thing about PyCall has been figuring out how to initialize Python correctly.) |
Agree. That really does seem like the most likely solution, except that PyCall is correctly inferring the location of libpython on my system, correctly setting PYTHONHOME, and having run the code more or less line by line, I can't find the failure until you try to import numpy for loading ufunc. At every point where I have been able to interrogate path settings, they've been the same between my mac (working) and linux (not). |
You could try setting |
@stevengj, |
@staticfloat, is there anything that can be done about the Ubuntu package? |
@stevengj I've tried to follow the conversation here but I'm having a little trouble. What is the fix that is needed? When running on Ubuntu 14.04, with the following packages installed:
Everything works just fine, as far as I can tell:
|
A little bit more information:
|
@staticfloat, it seems like you are not using the Ubuntu numpy package, which would normally be in |
Ah, you are correct, I installed numpy from source. I'll try installing the package and see what happens. |
Even after installing the debian package and removing everything in |
I'm using Python from Anaconda if that matters. |
OK, we've talked a bit more with @staticfloat and here is what we found:
So when I try to import Anaconda-provided version of NumPy through PyCall, it fails to find needed LAPACK symbol ("zgelsd_") because Solutions:
Libdl.dlopen("/usr/lib/liblapack.so.3", Libdl.RTLD_GLOBAL)
using PyCall
@pyimport numpy This way, NumPy is loading needed LAPACK symbols from Ubuntu's |
@stevengj Do you think there is a way we can auto-detect this condition and load |
@staticfloat, on most systems the Julia binaries load the ILP64 The problems arise when both numpy and julia both want to link to a library called |
That is one problem. This problem is slightly different. OpenBLAS has an option to statically build LAPACK and embed LAPACK within it; thereby creating a single .so file ( The issue arises because |
Back in the day Atlas etc didn't contain any lapack implementation pieces, so you could keep the netlib reference lapack and just swap out libblas dynamically and still get a speedup. MKL typically put blas and lapack into the same library but is not open source so linux distributions didn't really accomodate it here. Open source implementations that follow the mkl organization of having everything in the same library are a bit newer and distros haven't really caught up yet. Always dlopening liblapack on linux, or maybe only when lapack_name doesn't match blas_name, would make sense here. SCS.jl has been doing so. |
I guess my question is why isn't |
Guessing, but probably because the name/ILP64 conflicts used to be an issue, and it hasn't been adjusted since that was rectified? |
I don't know what the numbers look like on openblas' lapack vs netlib lapack linked against openblas for just libblas, but we could raise this with anaconda and see whether it would make sense for them to build blas-only openblas the way ubuntu is doing? |
I don't think there's any reason to do that; in fact, we build openblas-with-lapack when we do a from-source build. |
Right, but we also do ILP64 and tend to err on the side of performance over compatibility in source builds and tarball binaries. Anaconda doesn't do ILP64 and may care more strongly about compatibility than we do. |
One solution here would be to get Julia into Anaconda and then just tell people like me that they should install Julia from Anaconda as well if they want it to play nicely with Anaconda's Python. Right now I can see Julia there in some 3rd party channels, but none of them has 0.4.1 or even a release version of 0.4.0. |
Maybe. There are several ways of going about that, some of which would be much more challenging than others. Another option is for you to use the tarball binaries of Julia rather than the Ubuntu PPA, which would be more isolated in terms of library naming. |
Right. Right now I've switched to building Julia from source, so I don't really have an issue on my end. This is more about what to do when someone else runs into the same setup (Julia package from PPA + Anaconda) |
If we are linking to the system LP64 blas and lapack, I think we should load them with RTLD_GLOBAL. If we are linking to our own ILP64 openblas+lapack, it doesn't matter because the symbols are suffixed with |
@staticfloat, would it be enough to add: if startswith(basename(Base.liblapack_name), "liblapack")
Libdl.dlopen(Base.liblapack_name, Libdl.RTLD_LAZY|Libdl.RTLD_DEEPBIND|Libdl.RTLD_GLOBAL)
end to PyCall's |
@maximsch2 could you try putting the following snippet just below this line in if basename(Base.liblapack_name) != basename(Base.libblas_name)
Libdl.dlopen(Base.liblapack_name, Libdl.RTLD_LAZY|Libdl.RTLD_DEEPBIND|Libdl.RTLD_GLOBAL)
end Let's see if that works on your machine. @stevengj just to make this a little more general, I would do this anytime |
Hello,
I use Ubuntu 12.04, 64 bit. I installed numpy 1.7.1 with pip.
Importing numpy fails:
julia> using PyCall
julia> @pyimport numpy
ERROR: PyError (PyImport_ImportModule) <type 'exceptions.ImportError'>
ImportError('cannot import name scimath',)
File "/usr/local/lib/python2.7/dist-packages/numpy/init.py", line 153, in
from . import add_newdocs
File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in
from numpy.lib import add_newdoc
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/init.py", line 17, in
from . import scimath as emath
in pyerr_check at /home/ufechner/.julia/PyCall/src/exception.jl:58
in pyimport at /home/ufechner/.julia/PyCall/src/PyCall.jl:85
Importing numpy from python works fine, importing other packages from julia
also works.
Any idea?
Uwe Fechner
The text was updated successfully, but these errors were encountered: