Repository: pytorch/pytorch

Issue:
Importing scipy.misc after torch causes segfault
As shown in below. Is this something expected?
This seems most closely related to https://github.com/pytorch/pytorch/issues/595, but it is closed without any specific solutions.
```
(venv) $ pip freeze
appdirs==1.4.0
numpy==1.12.0
packaging==16.8
pyparsing==2.1.10
PyYAML==3.12
scipy==0.18.1
six==1.10.0
torch==0.1.9.post2

(venv) $ python -c 'import torch, scipy.misc'
Segmentation fault (core dumped)

(venv) $ python -c 'import scipy.misc, torch'

(venv) $ echo 'import torch, scipy.misc' > spam.py

(venv) $ python spam.py
Segmentation fault (core dumped)

(venv) $ gdb --args python spam.py
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...(no debugging symbols found)...done.
(gdb) run
Starting program: /tmp/venv/bin/python spam.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffc7088700 (LWP 5549)]
[New Thread 0x7fffc6887700 (LWP 5550)]
[New Thread 0x7fffc4086700 (LWP 5551)]
[New Thread 0x7fffc1885700 (LWP 5552)]
[New Thread 0x7fffbf084700 (LWP 5553)]
[New Thread 0x7fffbc883700 (LWP 5554)]
[New Thread 0x7fffba082700 (LWP 5555)]
[New Thread 0x7fffb7881700 (LWP 5556)]
[New Thread 0x7fffb5080700 (LWP 5557)]
[New Thread 0x7fffb287f700 (LWP 5558)]
[New Thread 0x7fffb007e700 (LWP 5559)]

Program received signal SIGSEGV, Segmentation fault.
0x00007fffc9d65fc0 in PyArray_API () from /tmp/venv/local/lib/python2.7/site-packages/numpy/core/multiarray.so
```


Discussion:
Importing `scipy.ndimage` after `torch` also causes segv:

```
(venv) $ python -c 'import torch, scipy.ndimage'
Segmentation fault (core dumped)
```
Happens with sklearn as well. 
`python -c 'import torch, sklearn' Segmentation fault (core dumped)`. 

Reversing the order of imports seems to fix things for me though.

Can you reinstall sklearn or scipy? I think I've seen a case where that helped.
I've tried reinstalling things. Doesn't fix it. Could it be because scikit seems to build using openblas and not MKL?
I don't think so, it's a problem with numpy initialization. AFAIK the same was happening with TF, not sure if it's solved now.
same. run import scipy/ import sklearn after torch will cause error.
but it's all right if load these two first.

Resolving patch:
diff --git a/torch/__init__.py b/torch/__init__.py
--- a/torch/__init__.py
+++ b/torch/__init__.py
@@ -31,6 +31,13 @@
 # automatically filled by the dynamic loader.
 import os as _dl_flags
 
+# if we have numpy, it *must* be imported before the call to setdlopenflags()
+# or there is risk that later c modules will segfault when importing numpy
+try:
+    import numpy as np
+except:
+    pass
+
 # first check if the os package has the required flags
 if not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):
     try:


Test patch: