astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.77k stars 468 forks source link

Numpy installation issue in Rye project when changing Python version #1423

Open kubotty opened 2 hours ago

kubotty commented 2 hours ago

Steps to Reproduce

After installing numpy, if you change the Python version, rye continues to install the wrong version of numpy.

To initialize a project, follow the steps below:

> rye init
> rye add numpy
> rye pin 3.10
> rye sync

Rye still installs the wrong numpy version:

> rye run python
>>> import numpy
Traceback (most recent call last):
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\_core\__init__.py", line 23, in <module>
    from . import multiarray
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\_core\multiarray.py", line 10, in <module>
    from . import overrides
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\_core\overrides.py", line 8, in <module>
    from numpy._core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy._core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\__init__.py", line 128, in <module>
    from numpy.__config__ import show as show_config
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\__config__.py", line 4, in <module>
    from numpy._core._multiarray_umath import (
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\_core\__init__.py", line 49, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.10 from "D:\work\temp\rye-test3\.venv\Scripts\python.exe"
  * The NumPy version is: "2.1.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy._core._multiarray_umath'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\work\temp\rye-test3\.venv\lib\site-packages\numpy\__init__.py", line 133, in <module>
    raise ImportError(msg) from e
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

If you force a sync with rye sync -f, or even if you delete the .venv directory and sync again with rye sync, it still installs the wrong version.

Expected Result

I expect the correct version of numpy to be installed.

If the order is changed between rye pin and rye add numpy, I could get expected behavior:

> rye init
> rye pin 3.10
> rye add numpy
> rye run python
>>> import numpy
>>> np.__version__
'2.1.2'

Actual Result

You can confirm that the wrong version is installed by checking the following directory:

> ls .\.venv\Lib\site-packages\
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2024/10/23     13:47                numpy
d-----        2024/10/23     13:47                numpy-2.1.2.dist-info
d-----        2024/10/23     13:47                numpy.libs
d-----        2024/10/23     13:47                rye_test3-0.1.0.dist-info
d-----        2024/10/23     13:47                __pycache__
-a----        2024/10/23     13:19              0 numpy-2.1.2-cp312-cp312-win_amd64.whl
-a----        2024/10/23     13:39             26 _rye_test3.pth
-a----        2024/10/23     13:47             18 _virtualenv.pth
-a----        2024/10/23     13:47           4342 _virtualenv.py

Although the Python version is 3.10, the numpy distribution for Python 3.12 is mistakenly installed.

Version Info

rye 0.41.0 commit: 0.41.0 (10d691d16 2024-09-29) platform: windows (x86_64) self-python: cpython@3.12.3 symlink support: false uv enabled: true

Stacktrace

No response

kubotty commented 2 hours ago

When I deleted the project directory (the parent directory of .venv) and then recreated a directory with the same name in the same location, I was able to reproduce the issue as follows:

> rye init
> rye pin 3.10
> rye add numpy
> rye run python
>>> import numpy

It seems that the initially downloaded wheel file is cached with the path, causing it to be used regardless of the Python version.

Furthermore, I noticed that the issue also occurs when starting with Python 3.11 and then changing to Python 3.12.

This issue seems to be related to https://github.com/astral-sh/rye/issues/1400