pypa / setuptools

Official project repository for the Setuptools build system
https://pypi.org/project/setuptools/
MIT License
2.54k stars 1.19k forks source link

ImportError: cannot import name 'tarfile' from 'backports' and RemoveError: 'setuptools' is a dependency of conda and cannot be removed from conda's operating environment. #4747

Closed nguyenphucnamgiang closed 1 week ago

nguyenphucnamgiang commented 1 week ago

setuptools version

setuptools==75.2.0

Python version

Python 3.11.5

Environment information

Conda base environment

Description

I tried to update anaconda navigator using conda update -n base conda, but it then ran into this error

(base) C:\Users\namgi>conda update -n base conda
Channels:
 - conda-forge
 - defaults
 - anaconda
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\namgi\anaconda3

  added / updated specs:
    - conda

The following packages will be UPDATED:

  openssl                                  3.3.2-h2466b09_0 --> 3.4.0-h2466b09_0
  wordcloud                           1.9.3-py311he736701_2 --> 1.9.4-py311he736701_0

Proceed ([y]/n)? y

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: failed

RemoveError: 'setuptools' is a dependency of conda and cannot be removed from
conda's operating environment.

I tried debugging but I cannot find anything helpful. After that, I opened anaconda navigator but It didn't work, I then used anaconda-navigator in anaconda prompt then find this

(base) C:\Users\namgi>anaconda-navigator
Traceback (most recent call last):
  File "C:\Users\namgi\anaconda3\Scripts\anaconda-navigator-script.py", line 6, in <module>
    from anaconda_navigator.app.main import main
  File "C:\Users\namgi\anaconda3\Lib\site-packages\anaconda_navigator\app\main.py", line 19, in <module>
    from anaconda_navigator.app.start import start_app
  File "C:\Users\namgi\anaconda3\Lib\site-packages\anaconda_navigator\app\start.py", line 32, in <module>
    from anaconda_navigator.widgets.main_window import MainWindow
  File "C:\Users\namgi\anaconda3\Lib\site-packages\anaconda_navigator\widgets\main_window\__init__.py", line 28, in <module>
    from anaconda_navigator.api.anaconda_api import AnacondaAPI
  File "C:\Users\namgi\anaconda3\Lib\site-packages\anaconda_navigator\api\anaconda_api.py", line 29, in <module>
    from anaconda_navigator.api.client_api import ClientAPI
  File "C:\Users\namgi\anaconda3\Lib\site-packages\anaconda_navigator\api\client_api.py", line 21, in <module>
    import binstar_client
  File "C:\Users\namgi\anaconda3\Lib\site-packages\binstar_client\__init__.py", line 15, in <module>
    from pkg_resources import parse_version as pv
  File "C:\Users\namgi\anaconda3\Lib\site-packages\pkg_resources\__init__.py", line 95, in <module>
    from jaraco.text import drop_comment, join_continuation, yield_lines
  File "C:\Users\namgi\anaconda3\Lib\site-packages\setuptools\_vendor\jaraco\text\__init__.py", line 12, in <module>
    from jaraco.context import ExceptionTrap
  File "C:\Users\namgi\anaconda3\Lib\site-packages\setuptools\_vendor\jaraco\context.py", line 17, in <module>
    from backports import tarfile
ImportError: cannot import name 'tarfile' from 'backports' (C:\Users\namgi\anaconda3\Lib\site-packages\backports\__init__.py)

I search many issues ImportError: cannot import name 'tarfile' from 'backports' #5, [BUG] 71.0.0 fails with backports.tarfile on Python 3.8 when other backports imported #4476, Reset the backports module when enabling vendored packages. #4486, Setup: Fix package installation woes with setuptools/backports.tarfile, and PyInstaller #201, [BUG] 71.0.0 fails PyInstaller builds #4477, [BUG] 71.0.4 and 71.1.0 fail with ImportError: cannot import name 'tarfile' from 'backports' #4509

the common solution is to change version but what should i downgrade to

abravalheri commented 1 week ago

Hi @nguyenphucnamgiang could you please install the latest version of backports.tarfile>=1.2 in your enviornment and see if it works?

nguyenphucnamgiang commented 1 week ago

thank you for your reply! But this returns the same error

(base) C:\Users\namgi>conda install conda-forge::backports.tarfile
Channels:
 - conda-forge
 - defaults
 - anaconda
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\namgi\anaconda3

  added / updated specs:
    - conda-forge::backports.tarfile

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    backports.tarfile-1.2.0    |     pyhd8ed1ab_0          32 KB  conda-forge
    ------------------------------------------------------------
                                           Total:          32 KB

The following NEW packages will be INSTALLED:

  backports.tarfile  conda-forge/noarch::backports.tarfile-1.2.0-pyhd8ed1ab_0

The following packages will be UPDATED:

  openssl                                  3.3.2-h2466b09_0 --> 3.4.0-h2466b09_0
  wordcloud                           1.9.3-py311he736701_2 --> 1.9.4-py311he736701_0

Proceed ([y]/n)? y

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: failed

RemoveError: 'setuptools' is a dependency of conda and cannot be removed from
conda's operating environment.

(base) C:\Users\namgi>conda install conda-forge::backports.tarfile=1.2
Channels:
 - conda-forge
 - defaults
 - anaconda
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\namgi\anaconda3

  added / updated specs:
    - conda-forge::backports.tarfile=1.2

The following NEW packages will be INSTALLED:

  backports.tarfile  conda-forge/noarch::backports.tarfile-1.2.0-pyhd8ed1ab_0

The following packages will be UPDATED:

  openssl                                  3.3.2-h2466b09_0 --> 3.4.0-h2466b09_0
  wordcloud                           1.9.3-py311he736701_2 --> 1.9.4-py311he736701_0

Proceed ([y]/n)? y

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: failed

RemoveError: 'setuptools' is a dependency of conda and cannot be removed from
conda's operating environment.
abravalheri commented 1 week ago

Umm, is it a deadlock? You cannot install backports.tarfile>=1.2 because setuptools don't work and setuptools cannot work because you cannot install backports.tarfile>=1.2?

Can you try the following:

If that does not work, you might have to start a fresh virtual environment 😢.

nguyenphucnamgiang commented 1 week ago

I don't even have backports.tarfile in my base environment

(base) C:\Users\namgi>conda list backports.tarfile
# packages in environment at C:\Users\namgi\anaconda3:
#
# Name                    Version                   Build  Channel

(base) C:\Users\namgi>

unfortunately, my goal is to update the base environment, so I need to fix this

abravalheri commented 1 week ago

I don't even have backports.tarfile in my base environment

Umm, backports is supposed to be a namespace, so there might be other backports.* packages installed that are interfering with this.

I can see that you have a file C:\Users\namgi\anaconda3\Lib\site-packages\backports\__init__.py. That is weird and probably the origin of the problem because backports.tarfile expects backports to be a namespace.

I suppose you can try:

OR

  1. Temporarily renaming C:\Users\namgi\anaconda3\Lib\site-packages\backports\__init__.py to something else like _bkp.py
  2. Either installing backports.tarfile>=1.2 or setuptools[core]
  3. You may be able to return the file to its original name now that backports.tarfile>=1.2 is installed...
abravalheri commented 1 week ago

The specific mechanics of the problem are described https://github.com/pypa/setuptools/issues/4508#issuecomment-2249216574.

abravalheri commented 1 week ago

I suppose that you can also try to install backports.tarfile>=1.2 using pip instead of conda, because pip uses isolated build environments for the build dependencies and/or pre-built wheels (so the deadlock would not kick in).

nguyenphucnamgiang commented 1 week ago

it was unexpected, pip finally added an tarfile folder and fixed that import error, I can launch anaconda navigator, but the other setuptools remove error stays the same

(base) C:\Users\namgi>pip install backports.tarfile==1.2
Collecting backports.tarfile==1.2
  Downloading backports.tarfile-1.2.0-py3-none-any.whl.metadata (2.0 kB)
Downloading backports.tarfile-1.2.0-py3-none-any.whl (30 kB)
Installing collected packages: backports.tarfile
Successfully installed backports.tarfile-1.2.0

(base) C:\Users\namgi>conda update -n base conda
Channels:
 - conda-forge
 - defaults
 - anaconda
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\namgi\anaconda3

  added / updated specs:
    - conda

The following packages will be UPDATED:

  openssl                                  3.3.2-h2466b09_0 --> 3.4.0-h2466b09_0
  wordcloud                           1.9.3-py311he736701_2 --> 1.9.4-py311he736701_0

Proceed ([y]/n)? y

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: failed

RemoveError: 'setuptools' is a dependency of conda and cannot be removed from
conda's operating environment.

(base) C:\Users\namgi>anaconda-navigator
QMimeDatabase: Error loading internal MIME data
An error has been encountered at line 1 of <internal MIME data>: Premature end of document.:
nguyenphucnamgiang commented 1 week ago

conda can list setuptools but then I try to update it, it cannot be found, is this normal behaviour?

(base) C:\Users\namgi> conda update --force setuptools

WARNING: The --force flag will be removed in a future conda release.
         See 'conda update --help' for details about the --force-reinstall
         and --clobber flags.

PackageNotInstalledError: Package is not installed in prefix.
  prefix: C:\Users\namgi\anaconda3
  package name: setuptools

(base) C:\Users\namgi> conda list setuptools
# packages in environment at C:\Users\namgi\anaconda3:
#
# Name                    Version                   Build  Channel
setuptools                75.2.0                   pypi_0    pypi
abravalheri commented 1 week ago

You mean the error ImportError: cannot import name 'tarfile' from 'backports' (C:\Users\namgi\anaconda3\Lib\site-packages\backports\__init__.py)?

How come if backports.tarfile==1.2 is installed? Does the C:\Users\namgi\anaconda3\Lib\site-packages\backports\tarfile directory exist?

abravalheri commented 1 week ago

conda can list setuptools but then I try to update it, it cannot be found, is this normal behaviour?

I am not a specialist in conda, so I don't know how to answer that question.

abravalheri commented 1 week ago

This issue seems to be the same as #4508, so it may be worth exploring the options discussed there.

nguyenphucnamgiang commented 1 week ago

ImportError: cannot import name 'tarfile' from 'backports' is ok, like I mentioned above, using pip install, it successfully downloaded the tarfile folder and fixed that import error, now opening anaconda does not give import error anymore image but it does not solve the RemoveError: 'setuptools' is a dependency of conda and cannot be removed from conda's operating environment you can see this still output that error

it was unexpected, pip finally added an tarfile folder and fixed that import error, I can launch anaconda navigator, but the other setuptools remove error stays the same

(base) C:\Users\namgi>pip install backports.tarfile==1.2
Collecting backports.tarfile==1.2
  Downloading backports.tarfile-1.2.0-py3-none-any.whl.metadata (2.0 kB)
Downloading backports.tarfile-1.2.0-py3-none-any.whl (30 kB)
Installing collected packages: backports.tarfile
Successfully installed backports.tarfile-1.2.0

(base) C:\Users\namgi>conda update -n base conda
Channels:
 - conda-forge
 - defaults
 - anaconda
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\namgi\anaconda3

  added / updated specs:
    - conda

The following packages will be UPDATED:

  openssl                                  3.3.2-h2466b09_0 --> 3.4.0-h2466b09_0
  wordcloud                           1.9.3-py311he736701_2 --> 1.9.4-py311he736701_0

Proceed ([y]/n)? y

Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: failed

RemoveError: 'setuptools' is a dependency of conda and cannot be removed from
conda's operating environment.

(base) C:\Users\namgi>anaconda-navigator
QMimeDatabase: Error loading internal MIME data
An error has been encountered at line 1 of <internal MIME data>: Premature end of document.:
abravalheri commented 1 week ago

Ah ok, thank you. that clarified the problem.

In this case, it seems like something to be discussed with conda maintainers, as it looks like a behaviour introduced by conda itself.

nguyenphucnamgiang commented 1 week ago

thank you @abravalheri, I will move to conda now

abravalheri commented 1 week ago

Thank you. I will close the issue now as a duplicate of https://github.com/pypa/setuptools/issues/4508.