Repository: conda/conda-build

Issue:
When building with  Python 3.10, SP_DIR says 3.1
### Actual Behavior

When building with Python 3.10 in the `host` environment, `SP_DIR` should point to:

```
.../lib/python3.10/site-packages
```

But instead, it points to:

```
.../lib/python3.1/site-packages
```

<!-- What actually happens? -->

### Expected Behavior

The `SP_DIR` environment variable should be `$PREFIX/lib/python3.10/site-packages`

<!-- What do you think should happen? -->

### Steps to Reproduce

<!-- Steps to reproduce issue. -->

As demonstration, here's a test recipe:

```yaml
# meta.yaml
package:
  name: test-spdir
  version: 0.1

requirements:
  host:
    - python 3.10
```

```bash
# build.sh
set -x
echo ${PREFIX}
echo ${PYTHON}
echo ${SP_DIR}
```

In the build output, it prints the following:

```
$PREFIX
$PREFIX/bin/python
$PREFIX/lib/python3.1/site-packages
```


##### Output of `conda info`
<!-- Between the ticks below, paste the output of 'conda info' -->

<details>

<summary>click here to see conda info</summary>

```
$ conda info

     active environment : base
    active env location : /opt/miniconda
            shell level : 1
       user config file : /Users/bergs/.condarc
 populated config files : /opt/miniconda/.condarc
                          /Users/bergs/.condarc
          conda version : 4.11.0
    conda-build version : 3.21.8
         python version : 3.7.6.final.0
       virtual packages : __osx=10.15.7=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /opt/miniconda  (writable)
      conda av data dir : /opt/miniconda/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/flyem-forge/osx-64
                          https://conda.anaconda.org/flyem-forge/noarch
                          https://conda.anaconda.org/conda-forge/osx-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/osx-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/osx-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://conda.anaconda.org/numba/osx-64
                          https://conda.anaconda.org/numba/noarch
          package cache : /opt/miniconda/pkgs
                          /Users/bergs/.conda/pkgs
       envs directories : /opt/miniconda/envs
                          /Users/bergs/.conda/envs
               platform : osx-64
             user-agent : conda/4.11.0 requests/2.25.1 CPython/3.7.6 Darwin/19.6.0 OSX/10.15.7
                UID:GID : 503:20
             netrc file : None
           offline mode : False

```

</details>


Discussion:
Hi @stuarteberg , thank you for filing this issue. This bug you've spotted is related to what is discussed in [this issue/comment](https://github.com/conda/conda/issues/11423#issuecomment-1104253815); we've added this ticket to our backlog and will be looking into a fix!

Resolving patch:
diff --git a/conda_build/utils.py b/conda_build/utils.py
--- a/conda_build/utils.py
+++ b/conda_build/utils.py
@@ -2,6 +2,7 @@
 import contextlib
 import fnmatch
 import hashlib
+from itertools import filterfalse
 import json
 from locale import getpreferredencoding
 import libarchive
@@ -1008,6 +1009,7 @@ def get_stdlib_dir(prefix, py_ver):
     else:
         lib_dir = os.path.join(prefix, 'lib')
         python_folder = glob(os.path.join(lib_dir, 'python?.*'))
+        python_folder = sorted(filterfalse(islink, python_folder))
         if python_folder:
             lib_dir = os.path.join(lib_dir, python_folder[0])
         else:


Test patch:
diff --git a/tests/test-recipes/metadata/python_build/build.sh b/tests/test-recipes/metadata/python_build/build.sh
--- a/tests/test-recipes/metadata/python_build/build.sh
+++ b/tests/test-recipes/metadata/python_build/build.sh
@@ -1,6 +1,9 @@
 if [ -z "${STDLIB_DIR:-}" ]; then
     echo "STDLIB_DIR is unset";
     exit 1
+elif [[ ${STDLIB_DIR} =~ '.*python3\.(1|2)$' ]]; then
+    echo "STDLIB_DIR is set to wrong path: $STDLIB_DIR"
+    exit 1
 else
     echo "STDLIB_DIR is set to $STDLIB_DIR";
     echo "STDLIB_DIR has $(ls -l $STDLIB_DIR | wc -l) lines"