Repository: piskvorky/gensim

Issue:
Install issue when installing with poetry
`poetry add gensim`

https://github.com/RaRe-Technologies/gensim/blob/ded78776284ad7b55b6626191eaa8dcea0dd3db0/setup.py#L107

getting `AttributeError: 'dict' object has no attribute '__NUMPY_SETUP__'` when trying to install gensim with poetry.

Seems to work on intel Macs, fails on M1

```pytb
       Traceback (most recent call last):
          File "/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/tmpgt9xpffh_in_process.py", line 363, in <module>
            main()
          File "/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/tmpgt9xpffh_in_process.py", line 345, in main
            json_out['return_val'] = hook(**hook_input['kwargs'])
          File "/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/tmpgt9xpffh_in_process.py", line 164, in prepare_metadata_for_build_wheel
            return hook(metadata_directory, config_settings)
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 188, in prepare_metadata_for_build_wheel
            self.run_setup()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 281, in run_setup
            super(_BuildMetaLegacyBackend,
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 174, in run_setup
            exec(compile(code, __file__, 'exec'), locals())
          File "setup.py", line 335, in <module>
            setup(
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/__init__.py", line 87, in setup
            return distutils.core.setup(**attrs)
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/core.py", line 177, in setup
            return run_commands(dist)
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/core.py", line 193, in run_commands
            dist.run_commands()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 968, in run_commands
            self.run_command(cmd)
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 1217, in run_command
            super().run_command(command)
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
            cmd_obj.run()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/command/dist_info.py", line 35, in run
            egg_info.run()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 307, in run
            self.find_sources()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 314, in find_sources
            mm.run()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 549, in run
            self.add_defaults()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 586, in add_defaults
            sdist.add_defaults(self)
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/command/sdist.py", line 252, in add_defaults
            self._add_defaults_ext()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/command/sdist.py", line 336, in _add_defaults_ext
            build_ext = self.get_finalized_command('build_ext')
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/cmd.py", line 304, in get_finalized_command
            cmd_obj.ensure_finalized()
          File "/private/var/folders/06/vq0z1ctd6yx5c3kgfz1bh4n00000gn/T/pip-build-env-yx8vfexi/overlay/lib/python3.9/site-packages/setuptools/_distutils/cmd.py", line 107, in ensure_finalized
            self.finalize_options()
          File "setup.py", line 107, in finalize_options
            __builtins__.__NUMPY_SETUP__ = False
        AttributeError: 'dict' object has no attribute '__NUMPY_SETUP__'
        [end of output]
```


Discussion:
Thanks for reporting. This must be some new development, installations have been working for a decade…

Are you able to check what changed? And if it's fix-able, open a PR?? Many thanks.
Yeah no idea, I have it failing on 3 colleague's M1 mbps also.

Seems other projects have used a workaround like:
https://www.programcreek.com/python/example/67000/builtins.__NUMPY_SETUP__

```py
def finalize_options(self):
    _build_ext.finalize_options(self)
    # Prevent numpy from thinking it is still in its setup process:
    try:
        __builtins__.__NUMPY_SETUP__ = False
    except:
        try:
            # For python 3
            import builtins
            builtins.__NUMPY_SETUP__ = False
        except:
            warn("Skipping numpy hack; if installation fails, try installing numpy first")

    import numpy
```

I could open a quick PR with this if u like?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -98,13 +98,14 @@ class CustomBuildExt(build_ext):
     importing them at module level, because they may not be available yet.
     """
     #
+    # Prevent numpy from thinking it is still in its setup process
     # http://stackoverflow.com/questions/19919905/how-to-bootstrap-numpy-installation-in-setup-py
     #
     def finalize_options(self):
         build_ext.finalize_options(self)
-        # Prevent numpy from thinking it is still in its setup process:
-        # https://docs.python.org/2/library/__builtin__.html#module-__builtin__
-        __builtins__.__NUMPY_SETUP__ = False
+
+        import builtins
+        builtins.__NUMPY_SETUP__ = False
 
         import numpy
         self.include_dirs.append(numpy.get_include())


Test patch: