pypa / virtualenv

Virtual Python Environment builder
https://virtualenv.pypa.io
MIT License
4.82k stars 1.03k forks source link

ImportError: cannot import name 'Path' from 'virtualenv.util.path' #2397

Closed amtoine closed 2 years ago

amtoine commented 2 years ago

Issue

I was trying to create a new python virtual environment to install some isolated dependencies, but then i get

$ virtualenv
ImportError: cannot import name 'Path' from 'virtualenv.util.path' (/home/ants/.local/lib/python3.10/site-packages/virtualenv/util/path/__init__.py)

which never happened before :thinking:

Environment

version:

$ virtualenv --version
virtualenv 20.16.3.dev3+gb18f733 from /home/ants/.local/lib/python3.10/site-packages/virtualenv/__init__.py

OS:

$ uname -a
Linux archasus 5.18.14-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 23 Jul 2022 11:46:17 +0000 x86_64 GNU/Linux

host python where virtualenv is installed:

Click to expand pip list ```bash $ pip list Package Version ----------------------------- --------------------- agate 1.6.3 agate-dbf 0.2.2 agate-excel 0.2.5 agate-sql 0.5.8 alabaster 0.7.12 anyio 3.6.1 apipkg 2.1.1 appdirs 1.4.4 arandr 0.1.10 argcomplete 2.0.0 argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 asciidoc 10.2.0 asciinema 2.2.0 astroid 2.12.2 asttokens 2.0.5 async-generator 1.10 attrs 21.4.0 autopep8 1.6.0 awscli 1.25.17 Babel 2.10.3 backcall 0.2.0 beautifulsoup4 4.11.1 black 22.6.0 bleach 4.1.0 botocore 1.27.27 Brlapi 0.8.4 btrfsutil 5.18.1 build 0.8.0 CacheControl 0.12.6 cachetools 4.2.4 cachy 0.3.0 cairocffi 1.3.0 certifi 2022.6.15 cffi 1.15.1 chardet 4.0.0 cleo 0.8.1 click 8.1.3 clikit 0.6.2 cloudpickle 2.1.0 colorama 0.4.5 commonmark 0.9.1 contextlib2 21.6.0 crashtest 0.3.1 cryptography 37.0.4 csvkit 1.0.6 dbfread 2.0.7 dbus-next 0.2.3 debugpy 1.5.1 decorator 5.1.1 defusedxml 0.7.1 deprecation 2.1.0 dill 0.3.4 distlib 0.3.5 distro 1.7.0 dmenu-extended 0.1.0 dnspython 2.2.1 docopt 0.6.2 docutils 0.19 entrypoints 0.4 et-xmlfile 1.1.0 ewmh 0.1.6 executing 0.8.2 fastjsonschema 2.16.1 filelock 3.6.0 flake8 4.0.1 flake8-black 0.3.3 Flask 2.0.2 future 0.18.2 fuzzywuzzy 0.18.0 gcalcli 4.3.0 google-api-core 2.3.2 google-api-python-client 2.34.0 google-auth 2.3.3 google-auth-httplib2 0.1.0 googleapis-common-protos 1.54.0 greenlet 1.1.2 grip 4.5.2 guake 3.9.1.dev0 html5lib 1.1 httplib2 0.20.2 idna 3.3 imagesize 1.4.1 importlib-metadata 4.8.1 iniconfig 1.1.1 installer 0.5.1 ipdb 0.13.9 ipykernel 6.7.0 ipython 8.0.0 ipython-genutils 0.2.0 ipywidgets 7.6.5 isodate 0.6.1 isort 5.10.1 itsdangerous 2.0.1 iwlib 1.7.0 jaraco.context 4.1.1 jaraco.functools 3.5.1 jaraco.text 3.8.1 jedi 0.18.1 jeepney 0.7.1 Jinja2 3.1.2 jmespath 1.0.0 json5 0.9.8 jsonschema 4.3.3 jupyter-client 7.1.2 jupyter-console 6.4.3 jupyter-core 4.9.2 jupyter-packaging 0.12.2 jupyter-server 1.18.1 jupyterlab 3.4.4 jupyterlab-pygments 0.1.2 jupyterlab-server 2.15.0 jupyterlab-widgets 1.0.2 keyring 23.4.0 keyutils 0.6 lark-parser 0.12.0 lazy-object-proxy 1.7.1 leather 0.3.4 lensfun 0.3.3 libfdt 1.6.1 lit 14.0.6.dev0 lockfile 0.12.2 louis 3.22.0 Markdown 3.4.1 MarkupSafe 2.1.1 matplotlib-inline 0.1.3 mccabe 0.7.0 meson 0.63.0 mistune 0.8.4 more-itertools 8.13.0 mpi4py 3.1.3 msgpack 1.0.3 mypy-extensions 0.4.3 nbclient 0.5.10 nbconvert 6.4.0 nbformat 5.1.3 nest-asyncio 1.5.4 netsnmp-python 1.0a1 nose 1.3.7 notebook 6.4.12 notebook-shim 0.1.0 nspektr 0.4.0 numpy 1.23.1 oauth2client 4.1.3 olefile 0.46 openpyxl 3.0.10 ordered-set 4.0.2 packaging 21.3 pandocfilters 1.5.0 parsedatetime 2.6 parso 0.8.3 pass-import 3.2 pastel 0.2.1 path-and-address 2.0.1 pathspec 0.9.0 pbr 5.8.0 pep517 0.12.0 pexpect 4.8.0 pickleshare 0.7.5 Pillow 9.2.0 pip 22.2.1 pipenv 2022.7.4 pkgconfig 1.5.5 pkginfo 1.8.3 platformdirs 2.5.1 pluggy 1.0.0 ply 3.11 poetry 1.1.14 poetry-core 1.0.8 progress 1.6 prometheus-client 0.12.0 prompt-toolkit 3.0.24 protobuf 3.19.1 psutil 5.9.0 ptipython 1.0.1 ptpython 3.0.20 ptyprocess 0.7.0 pulsemixer 1.5.1 pure-eval 0.2.1 py 1.11.0 pyaml 21.10.1 pyasn1 0.4.8 pyasn1-modules 0.2.8 pycairo 1.21.0 pycodestyle 2.8.0 pycparser 2.21 pyfiglet 0.8.post1 pyflakes 2.4.0 Pygments 2.12.0 PyGObject 3.42.2 PyICU 2.9 pylev 1.4.0 pylint 2.14.5 pynvim 0.4.3 PyOpenGL 3.1.6 pyOpenSSL 22.0.0 pyparsing 3.0.9 PyQt5 5.15.7 PyQt5-sip 12.11.0 PyQtWebEngine 5.15.6 pyrsistent 0.18.0 pyserial 3.5 PySide2 5.15.5 pyte 0.8.0 pytest 7.1.2 python-dateutil 2.8.2 python-distutils-extra 2.39 python-slugify 6.1.2 python-telegram 0.15.0 python-xlib 0.31 pytimeparse 1.1.8 pytz 2022.1 pyxdg 0.28 PyYAML 6.0 pyzmq 22.3.0 qtconsole 5.2.2 qtile 0.21.0 qtile-extras 0.1.dev140+g159eb19 QtPy 2.0.0 qutebrowser 2.5.2 ranger-fm 1.9.3 requests 2.27.1 requests-toolbelt 0.9.1 requests-unixsocket 0.3.0 resolvelib 0.8.1 retrying 1.3.3 rich 12.5.0 rsa 4.8 ruamel.yaml 0.16.13 s3transfer 0.6.0 screenkey 1.5 SecretStorage 3.3.2 Send2Trash 1.8.0 setproctitle 1.2.3 setuptools 61.3.1 shellingham 1.4.0 shiboken2 5.15.5 shtab 1.5.5 six 1.16.0 sniffio 1.2.0 snowballstemmer 2.2.0 soupsieve 2.3.2.post1 Sphinx 5.1.1 sphinx-rtd-theme 1.0.0 sphinxcontrib-applehelp 1.0.2 sphinxcontrib-devhelp 1.0.2 sphinxcontrib-htmlhelp 2.0.0 sphinxcontrib-jsmath 1.0.1 sphinxcontrib-qthelp 1.0.3 sphinxcontrib-serializinghtml 1.1.5 SQLAlchemy 1.4.39 stack-data 0.1.4 stevedore 3.5.0 TBB 0.2 team 1.0 tenacity 8.0.1 termcolor 1.1.0 termdown 1.18.0 terminado 0.12.1 testpath 0.5.0 text-unidecode 1.3 tg 0.19.0 tinycss2 1.1.1 tldr 3.1.0 toml 0.10.2 tomli 2.0.1 tomlkit 0.11.1 tornado 6.1 tqdm 4.64.0 traitlets 5.1.1 trove-classifiers 2022.7.22 typed-argument-parser 1.7.2 typing-extensions 3.10.0.2 typing-inspect 0.7.1 udiskie 2.4.2 ueberzug 18.1.9 ufw 0.36.1 uritemplate 4.1.1 urllib3 1.26.9 validate-pyproject 0.9 virtualenv 20.16.3.dev3+gb18f733 virtualenv-clone 0.5.7 virtualenvwrapper 4.8.4 virtualfish 2.5.4 wcwidth 0.2.5 webencodings 0.5.1 websocket-client 1.3.3 Werkzeug 2.0.2 wheel 0.37.1 widgetsnbextension 3.5.2 wrapt 1.14.1 xcffib 0.11.1 xlrd 2.0.1 xmltodict 0.13.0 xonsh 0.13.0 xontrib-fzf-widgets 0.0.4 xontrib-z 0.4 youtube-dl 2021.12.17 zipp 3.8.1 ```

Output of the virtual environment creation

$ virtualenv -vvv --with-traceback
544 setup logging to NOTSET [DEBUG report:35]
556 find interpreter for spec PythonSpec(path=/usr/bin/python) [INFO builtin:56]
556 proposed PythonInfo(spec=CPython3.10.5.final.0-64, exe=/usr/bin/python, platform=linux, version='3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0]', encoding_fs_io=utf-8-utf-8) [INFO builtin:63]
556 accepted PythonInfo(spec=CPython3.10.5.final.0-64, exe=/usr/bin/python, platform=linux, version='3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0]', encoding_fs_io=utf-8-utf-8) [DEBUG builtin:65]
557 filesystem is case-sensitive [DEBUG info:24]
Traceback (most recent call last):
  File "/home/ants/.local/bin/virtualenv", line 8, in <module>
    sys.exit(run_with_catch())
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/__main__.py", line 54, in run_with_catch
    run(args, options, env)
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/__main__.py", line 16, in run
    session = cli_run(args, options, env)
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/__init__.py", line 28, in cli_run
    of_session = session_via_cli(args, options, setup_logging, env)
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/__init__.py", line 46, in session_via_cli
    parser, elements = build_parser(args, options, setup_logging, env)
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/__init__.py", line 74, in build_parser
    ActivationSelector(interpreter, parser),
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/plugin/activators.py", line 11, in __init__
    (k, v) for k, v in self.options("virtualenv.activate").items() if v.supports(interpreter)
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/plugin/base.py", line 44, in options
    cls._OPTIONS = cls.entry_points_for(key)
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/plugin/base.py", line 21, in entry_points_for
    return OrderedDict((e.name, e.load()) for e in cls.entry_points().select(group=key))
  File "/home/ants/.local/lib/python3.10/site-packages/virtualenv/run/plugin/base.py", line 21, in <genexpr>
    return OrderedDict((e.name, e.load()) for e in cls.entry_points().select(group=key))
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/lib/python3.10/site-packages/xonsh/virtualenv/__init__.py", line 2, in <module>
    from virtualenv.util.path import Path  # type: ignore
ImportError: cannot import name 'Path' from 'virtualenv.util.path' (/home/ants/.local/lib/python3.10/site-packages/virtualenv/util/path/__init__.py)
gaborbernat commented 2 years ago

This seems like a broken virtualenv installation. How did you install it?

shahs65 commented 2 years ago

@amtoine Hi, I have also faced a similar issue. I don't think the issue is directly related to the virtualenv package itself...in my case, I had an Anaconda installation, which somehow also had remnants of virtualenv v20.0.35, in addition to the latest v20.16.3. A side-effect of this was the presence of the file virtualenv-20.0.35.dist-info in the python site-packages directory, which was then causing the virtualenv call to execute virtualenv/activation/xonsh/__init__.py. This actually does not exist in v20.16.3, but existed in v20.0.35...no idea why or how this is being called, but I think it is related to a broken Anaconda installation. image

In my case, the "fix" was quite simple: just remove the virtualenv-20.0.35.dist-info from the python site-packages directory, and then all works fine...

PS: I am running Python 3.7.4 on Linux (Ubuntu), so you may not have the same diagnosis or fix, but worth checking for any offending "dist-info" directories not belonging to the latest version of virtualenv.

Hope this helps! Regards.

gaborbernat commented 2 years ago

Closing per above.+

amtoine commented 2 years ago

This seems like a broken virtualenv installation.

yep i agree :thinking:

How did you install it?

in a shell, i've got

$ whereis virtualenv
virtualenv: /usr/bin/virtualenv /home/ants/.local/bin/virtualenv /usr/share/man/man1/virtualenv.1.gz
  • /usr/bin/virtualenv has been installed with pacman and comes from the python-virtualenv package on ArchLinux
  • ~/.local/bin/virtualenv has been installed with pip install virtualenv, which was at version 20.11.0 at the time, and i bumped up the version to 20.16.3.dev3+gb18f733 today with pip install git+https://github.com/pypa/virtualenv.git@main

None of them work :confused:

amtoine commented 2 years ago

Closing per above.+

so i should rm ~/.local/lib/python3.10/site-packages/virtualenv-20.16.3.dist-info?

gaborbernat commented 2 years ago

Use pipx to install virtualenv is the best path ahead instead of installing it into your user profile.

amtoine commented 2 years ago

Use pipx to install virtualenv is the best path ahead instead of installing it into your user profile.

ooooh i did not know pipx!!

so i can run virtualenv inside pipx' virtual environments and that works fine :ok_hand: :open_mouth:

amtoine commented 2 years ago

thanks @gaborbernat :yum:

shahs65 commented 2 years ago

Closing per above.+

so i should rm ~/.local/lib/python3.10/site-packages/virtualenv-20.16.3.dist-info?

Actually, no, remove any dist-info directories for an older release (if they exist). If you do not have these, then your issue/diagnosis is different to what I have...

yaxollum commented 2 years ago

This issue is actually caused by a bug in the xonsh shell, which was fixed in https://github.com/xonsh/xonsh/pull/4903

amtoine commented 2 years ago

This issue is actually caused by a bug in the xonsh shell, which was fixed in https://github.com/xonsh/xonsh/pull/4903

nice to hear :relieved:

HydrogenSulfate commented 1 year ago

This seems like a broken virtualenv installation.

yep i agree 🤔

How did you install it?

in a shell, i've got

$ whereis virtualenv
virtualenv: /usr/bin/virtualenv /home/ants/.local/bin/virtualenv /usr/share/man/man1/virtualenv.1.gz
  • /usr/bin/virtualenv has been installed with pacman and comes from the python-virtualenv package on ArchLinux
  • ~/.local/bin/virtualenv has been installed with pip install virtualenv, which was at version 20.11.0 at the time, and i bumped up the version to 20.16.3.dev3+gb18f733 today with pip install git+https://github.com/pypa/virtualenv.git@main

None of them work 😕

I guess mv virtualenv-xx.x.xx.dist-info/ backup_virtualenv-xx.x.xx.dist-info/ is an safe way to solve this problem. thanks for this problem and your answers, really useful