Unable to compare versions for numpy>=1.17: need=1.17 found=None.
Author: Hanochhu
Pull request: no

### System Info

Ubuntu 18.04.6
transformers version : 4.18.0 
pytorch version : 2.0.0
numpy   version : 1.24.3
conda env



### Who can help?

@sgugger 

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

from transformers import pipeline
text_classifier = pipeline('text-classification', model='distilbert-base-uncased-finetuned-sst-2-english')
text = "This movie is good！"
result = text_classifier(text)
print(result)

**when I run a code using transformers, there will be an error:**

Traceback (most recent call last):
  File "/home/hyx/hhq/hugging_face/test.py", line 1, in <module>
    from transformers import pipeline
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/__init__.py", line 26, in <module>
    from . import dependency_versions_check
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/dependency_versions_check.py", line 41, in <module>
    require_version_core(deps[pkg])
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/utils/versions.py", line 123, in require_version_core
    return require_version(requirement, hint)
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/utils/versions.py", line 117, in require_version
    _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/utils/versions.py", line 45, in _compare_versions
    raise ValueError(
ValueError: Unable to compare versions for numpy>=1.17: need=1.17 found=None. This is unusual. Consider reinstalling numpy.

### Expected behavior

I have tried to reinstall numpy ,transformers, but it's not work

Comments:
Updating transformers to the latest version should fix the problem. 
You can run:
`pip install --upgrade transformers`
to update Transformers to the latest version.

> I have tried  conda update transformers , but when it finished ,there is no error and the version didn't change. still is 4.18.0. 
Then,   I also tried the following command，nothing changed
```
~$ conda install transformers==4.28.1
Collecting package metadata (current_repodata.json): done
Solving environment: done
# All requested packages already installed.

$ conda list transformers
# packages in environment at /home/miniconda3:
#
# Name                    Version                   Build  Channel
sentence-transformers     2.2.2                    pypi_0    pypi
transformers              4.18.0                   pypi_0    pypi   
```

It seems that the conda channel has not been updated, hence it pulls in the old version.
Can you try running:
`conda install -c huggingface transformers`


Conda environments also support installs using pip, so you could also run:
```bash
conda install pip
pip install --upgrade transformers
``` 

**I have update transformers to latest version** 
```
$ conda list transformers                                                                                       
# packages in environment at /home/miniconda3:                                                                                                             
#                                                                                                                                                          
# Name                    Version                   Build  Channel                                                                                         
sentence-transformers     2.2.2                    pypi_0    pypi                                                                                          
transformers              4.28.1                     py_0    huggingface    
```
**But the problem still** 
```
Traceback (most recent call last):
  File "/home/hyx/hhq/hugging_face/test.py", line 1, in <module>
    from transformers import pipeline
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/__init__.py", line 26, in <module>
    from . import dependency_versions_check
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/dependency_versions_check.py", line 41, in <module>
    require_version_core(deps[pkg])
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/utils/versions.py", line 123, in require_version_core
    return require_version(requirement, hint)
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/utils/versions.py", line 117, in require_version
    _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)
  File "/home/miniconda3/lib/python3.9/site-packages/transformers/utils/versions.py", line 45, in _compare_versions
    raise ValueError(
ValueError: Unable to compare versions for numpy>=1.17: need=1.17 found=None. This is unusual. Consider reinstalling numpy.
```

What version of Numpy are you using? Can you update that as well?
You can use:
`conda update numpy`

```
$ conda list numpy
# packages in environment at /home/miniconda3:
#
# Name                    Version                   Build  Channel
numpy                     1.24.3           py39h14f4228_0    defaults
numpy-base                1.24.3           py39h31eccc5_0    defaults
numpy-quaternion          2022.4.1                 pypi_0    pypi
```
as you can see, numpy is also latest 

Same thing ...
```
 % ipython
Python 3.9.16 (main, Mar  8 2023, 04:29:44) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import transformers
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 1
----> 1 import transformers

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/transformers/__init__.py:26
     23 from typing import TYPE_CHECKING
     25 # Check the dependencies satisfy the minimal versions required.
---> 26 from . import dependency_versions_check
     27 from .utils import (
     28     OptionalDependencyNotAvailable,
     29     _LazyModule,
   (...)
     42     logging,
     43 )
     46 logger = logging.get_logger(__name__)  # pylint: disable=invalid-name

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/transformers/dependency_versions_check.py:41
     38         if not is_tokenizers_available():
     39             continue  # not required, check version only if installed
---> 41     require_version_core(deps[pkg])
     42 else:
     43     raise ValueError(f"can't find {pkg} in {deps.keys()}, check dependency_versions_table.py")

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/transformers/utils/versions.py:123, in require_version_core(requirement)
    121 """require_version wrapper which emits a core-specific hint on failure"""
    122 hint = "Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main"
--> 123 return require_version(requirement, hint)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/transformers/utils/versions.py:117, in require_version(requirement, hint)
    115 if want_ver is not None:
    116     for op, want_ver in wanted.items():
--> 117         _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/transformers/utils/versions.py:45, in _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)
     43 def _compare_versions(op, got_ver, want_ver, requirement, pkg, hint):
     44     if got_ver is None or want_ver is None:
---> 45         raise ValueError(
     46             f"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider"
     47             f" reinstalling {pkg}."
     48         )
     49     if not ops[op](version.parse(got_ver), version.parse(want_ver)):
     50         raise ImportError(
     51             f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}"
     52         )

ValueError: Unable to compare versions for numpy>=1.17: need=1.17 found=None. This is unusual. Consider reinstalling numpy.

In [2]: quit
(pysr) davidlaxer@bluediamond julia % conda list numpy           
# packages in environment at /Users/davidlaxer/anaconda3/envs/pysr:
#
# Name                    Version                   Build  Channel
numpy                     1.24.3           py39he696674_0  
numpy-base                1.24.3           py39h9cd3388_0  
```

If there is no way to solve this problem, I would change a env.  I will close this issue in few days

```
 % ipython            
Python 3.9.16 (main, Mar  8 2023, 04:29:44) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import importlib_metadata

In [2]: import numpy

In [3]: importlib_metadata.version(numpy)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 importlib_metadata.version(numpy)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:832, in version(distribution_name)
    825 def version(distribution_name):
    826     """Get the version string for the named package.
    827 
    828     :param distribution_name: The name of the distribution package to query.
    829     :return: The version string for the package as defined in the package's
    830         "Version" metadata key.
    831     """
--> 832     return distribution(distribution_name).version

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:805, in distribution(distribution_name)
    799 def distribution(distribution_name):
    800     """Get the ``Distribution`` instance for the named package.
    801 
    802     :param distribution_name: The name of the distribution package as a string.
    803     :return: A ``Distribution`` instance (or subclass thereof).
    804     """
--> 805     return Distribution.from_name(distribution_name)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:381, in Distribution.from_name(cls, name)
    379     raise ValueError("A distribution name is required.")
    380 try:
--> 381     return next(cls.discover(name=name))
    382 except StopIteration:
    383     raise PackageNotFoundError(name)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:400, in <genexpr>(.0)
    397     raise ValueError("cannot accept context and kwargs")
    398 context = context or DistributionFinder.Context(**kwargs)
    399 return itertools.chain.from_iterable(
--> 400     resolver(context) for resolver in cls._discover_resolvers()
    401 )

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:731, in MetadataPathFinder.find_distributions(self, context)
    722 def find_distributions(self, context=DistributionFinder.Context()):
    723     """
    724     Find distributions.
    725 
   (...)
    729     of directories ``context.path``.
    730     """
--> 731     found = self._search_paths(context.name, context.path)
    732     return map(PathDistribution, found)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:737, in MetadataPathFinder._search_paths(cls, name, paths)
    734 @classmethod
    735 def _search_paths(cls, name, paths):
    736     """Find metadata directories in paths heuristically."""
--> 737     prepared = Prepared(name)
    738     return itertools.chain.from_iterable(
    739         path.search(prepared) for path in map(FastPath, paths)
    740     )

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:692, in Prepared.__init__(self, name)
    690 if name is None:
    691     return
--> 692 self.normalized = self.normalize(name)
    693 self.legacy_normalized = self.legacy_normalize(name)

File ~/anaconda3/envs/pysr/lib/python3.9/site-packages/importlib_metadata/__init__.py:700, in Prepared.normalize(name)
    695 @staticmethod
    696 def normalize(name):
    697     """
    698     PEP 503 normalization plus dashes as underscores.
    699     """
--> 700     return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')

File ~/anaconda3/envs/pysr/lib/python3.9/re.py:210, in sub(pattern, repl, string, count, flags)
    203 def sub(pattern, repl, string, count=0, flags=0):
    204     """Return the string obtained by replacing the leftmost
    205     non-overlapping occurrences of the pattern in string by the
    206     replacement repl.  repl can be either a string or a callable;
    207     if a string, backslash escapes in it are processed.  If it is
    208     a callable, it's passed the Match object and must return
    209     a replacement string to be used."""
--> 210     return _compile(pattern, flags).sub(repl, string, count)

TypeError: expected string or bytes-like object
```

I created a new Conda virtual environment and installed the requisite packages ... no issue.
So, something was wrong in original Conda virtual environment (which I removed).

I got around it by modifying `transformers/utils/versions.py` :

line 102, from:
`got_ver = importlib.metadata.version(pkg)`

to:
```
got_ver = importlib.metadata.version(pkg)
        if got_ver is None:
            import pkg_resources
            got_ver = pkg_resources.get_distribution(pkg).version
```

For some reason, `importlib.metadata.version("numpy")` returned None, but pkg_resources works

> I got around it by modifying `transformers/utils/versions.py` :
> 
> line 102, from: `got_ver = importlib.metadata.version(pkg)`
> 
> to:
> 
> ```
> got_ver = importlib.metadata.version(pkg)
>         if got_ver is None:
>             import pkg_resources
>             got_ver = pkg_resources.get_distribution(pkg).version
> ```
> 
> For some reason, `importlib.metadata.version("numpy")` returned None, but pkg_resources works

:+1: **Awesome**

This stackoverflow issue fixed my problem: https://stackoverflow.com/questions/74817641/openai-whisper-cannot-import-numpy

This issue should be reopened. `importlib.metadata.version("numpy")` returns None despite numpy is correctly installed and working. The way transformers detecting **runtime** package versions seems not correct. It's checking *installed* metadata, not actual runtime version.

> So, something was wrong in original Conda virtual environment (which I removed).

BTW I got this workaround working, clean up and remove all the (possibly outdated) metadata from site-packages:

```
site_packages=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
rm -rf "$site_packages"/numpy
pip install --upgrade --force-reinstall numpy
```

BTW just reinstalling via `pip -I --force-reinstall install numpy` did not work.

This stackoverflow answer worked for me: https://stackoverflow.com/a/74817984/5189462, but it's also what @wookayin mentions