Repository: python-pillow/Pillow

Issue:
Bug in RuntimeWarning for _imaging
### What did you do?
Install Pillow on Windows via pip in a MSYS2 enviroment
import image

### What actually happened?
```
RuntimeWarning: The _imaging extension was built for another version of Pillow or PIL: Core
Version: "4.2.1"Pillow Version:  4.2.1
```

### What versions of Pillow and Python are you using?
python 3.6

I think the check in https://github.com/python-pillow/Pillow/blob/master/PIL/Image.py#L57 fails because `"4.2.1"` != `4.2.1`



Discussion:
You could put a print(type(...)) for both to check what they are. 

4.2.1 can't be a float, with two decimals :) 
they are both strings, one has quotes in it the other dont.
What's `sys.platform` in your python?  Is it `win32`?  Is there anything in `sys` that tags it to MSYS2?

The essential change from 4.1 is that we've gone to a single specification of the version, and it's passed into the compilation process as a command line variable. In setup.py, there's some sniffing:

```
        if sys.platform == "win32" and not hasattr(sys, 'pypy_version_info'):
            defs.append(("PILLOW_VERSION", '"\\"%s\\""'%PILLOW_VERSION))
        else:
            defs.append(("PILLOW_VERSION", '"%s"'%PILLOW_VERSION))
```

It looks like you're getting the win32, nonpypy branch. But MSYS2 should give a `sys.platform == 'msys2'`
https://docs.python.org/3/library/sys.html#sys.platform

there is no value 'msys2'.


```
test@test MINGW32 ~
$ python3
Python 3.6.2rc1 (default, Jun 26 2017, 07:51:29)  [GCC 6.3.0 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.platform
'win32'
>>>
```

i install pillow with pip3

```
$ pip3 install pillow
Collecting pillow
  Using cached Pillow-4.2.1.tar.gz
Requirement already satisfied: olefile in c:/msys64/mingw32/lib/python3.6/site-packages (from pillow)
Building wheels for collected packages: pillow
  Running setup.py bdist_wheel for pillow: started
  Running setup.py bdist_wheel for pillow: finished with status 'done'
  Stored in directory: C:/Users/user/AppData/Local/pip/Cache/wheels/88/15/71/1970b5f85d14e373f3a71279ba7f6d3ced6a48179a5e8d44b3
Successfully built pillow
Installing collected packages: pillow
Successfully installed pillow-4.2.1
```

```
$ python3
Python 3.6.2rc1 (default, Jun 26 2017, 07:51:29)  [GCC 6.3.0 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.pypy_version_info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sys' has no attribute 'pypy_version_info'
>>>
```
The only way i found it this

sys.version reports:


`3.6.2rc1 (default, Jun 26 2017, 07:51:29)  [GCC 6.3.0 32 bit]` in msys


`3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)]` installed from python.org



Msys2 is a strange beast.  I've spun up a ec2 instance, and I'm getting `sys.platform` == `msys`, but just installing python (3.6.1) and python3-pip doesn't give a working pip command, python3-setuptools doesn't give a working setuptools. Also don't see virtualenv anywhere. 

Python2 gets a working pip instance, but I can't find the jpeg libraries anywhere. So disabling them just to test this with: `python setup.py build_ext --disable-jpeg install`, I'm able to install and the version check works properly. 

So, there's something else going on with your setup.
thats probably because you use the MSYS shell, but this is only used to update core packages i think

if you want to build software for windows, you usually use either

MSYS2 MinGW 32-bit
MSYS2 MinGW 64-bit

you should find shortcuts for that in your installation.
What msys2 packages do you have installed for the dependencies?
Sorry, i realized in this conversations that its a bit more complicated as to just say "i use a msys env"

so to be more exact

i update core packages first like it is described on http://www.msys2.org/

then i start the MSYS2 MinGW 32-bit shell

`pacman -S mingw32/mingw-w64-i686-python3`
`pacman -S mingw32/mingw-w64-i686-python3-pip`
`pacman -S mingw32/mingw-w64-i686-python3-setuptools`
`pacman -S mingw-w64-i686-toolchain`

then just `pip3 install pillow`

i think thats it


Does that get you libz and libjpeg, with headers?
hm im not sure

it seems this package is also installed
mingw-w64-i686-libjpeg-turbo

```
$ find . -name '*libjpeg*'
./home/user/gajim-gtk3-fork/win/_build_root/mingw32/bin/libjpeg-8.dll
./home/user/gajim-gtk3-fork/win/_build_root/var/lib/pacman/local/mingw-w64-i686-libjpeg-turbo-1.5.2-1
./home/user/gtk-windows-bin/bin/libjpeg-7.dll
./mingw32/bin/libjpeg-8.dll
./mingw32/lib/libjpeg.a
./mingw32/lib/libjpeg.dll.a
./mingw32/lib/pkgconfig/libjpeg.pc
./mingw32/share/doc/libjpeg-turbo
./mingw32/share/doc/libjpeg-turbo/libjpeg.txt
./mingw32/share/licenses/libjpeg-turbo
./mingw64/bin/libjpeg-8.dll
./mingw64/lib/libjpeg.a
./mingw64/lib/libjpeg.dll.a
./mingw64/lib/pkgconfig/libjpeg.pc
./mingw64/share/doc/libjpeg-turbo
./mingw64/share/doc/libjpeg-turbo/libjpeg.txt
./mingw64/share/licenses/libjpeg-turbo
./var/cache/pacman/pkg/mingw-w64-i686-libjpeg-turbo-1.5.1-1-any.pkg.tar.xz
./var/cache/pacman/pkg/mingw-w64-i686-libjpeg-turbo-1.5.2-1-any.pkg.tar.xz
./var/cache/pacman/pkg/mingw-w64-x86_64-libjpeg-turbo-1.5.1-1-any.pkg.tar.xz
./var/lib/pacman/local/mingw-w64-i686-libjpeg-turbo-1.5.1-1
./var/lib/pacman/local/mingw-w64-x86_64-libjpeg-turbo-1.5.1-1
```

```
$ find . -name '*libz*'
./mingw32/lib/libz.a
./mingw32/lib/libz.dll.a
./mingw64/lib/libz.a
./mingw64/lib/libz.dll.a
```

So could i modify that lines that tell what version goes into the compilation process? i dont care to be honest i just want to get it working.

if i try to install with
`python3 setup.py install`

```
from PIL import Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\msys64\mingw32\lib\python3.6\site-packages\pillow-4.3.0.dev0-py3.6-mingw.egg\PIL\Image.py", line 56, in <module>
ImportError: cannot import name '_imaging'
```

i read that is a common problem on windows, but i found no solution.

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -17,7 +17,7 @@
 import subprocess
 
 from distutils.command.build_ext import build_ext
-from distutils import sysconfig
+from distutils import sysconfig, ccompiler
 from setuptools import Extension, setup, find_packages
 
 # monkey patch import hook. Even though flake8 says it's not used, it is.
@@ -44,6 +44,9 @@
 class DependencyException(Exception): pass
 class RequiredDependencyException(Exception): pass
 
+PLATFORM_MINGW = 'mingw' in ccompiler.get_default_compiler()
+PLATFORM_PYPY = hasattr(sys, 'pypy_version_info')
+
 def _dbg(s, tp=None):
     if DEBUG:
         if tp:
@@ -607,7 +610,7 @@ def build_extensions(self):
         if struct.unpack("h", "\0\1".encode('ascii'))[0] == 1:
             defs.append(("WORDS_BIGENDIAN", None))
 
-        if sys.platform == "win32" and not hasattr(sys, 'pypy_version_info'):
+        if sys.platform == "win32" and not (PLATFORM_PYPY or PLATFORM_MINGW):
             defs.append(("PILLOW_VERSION", '"\\"%s\\""'%PILLOW_VERSION))
         else:
             defs.append(("PILLOW_VERSION", '"%s"'%PILLOW_VERSION))
@@ -781,7 +784,7 @@ def debug_build():
           test_suite='nose.collector',
           keywords=["Imaging", ],
           license='Standard PIL License',
-          zip_safe=not debug_build(), )
+          zip_safe=not (debug_build() or PLATFORM_MINGW), )
 except RequiredDependencyException as err:
     msg = """
 


Test patch: