pipenv Shell无法创建虚拟环境

时间:2019-11-20 17:01:57

标签: python django macos pipenv

我正在尝试使用pipenv shell运行Django项目。但是当我输入命令pipenv shell时,它将失败。

❯ pipenv shell
Creating a virtualenv for this project…
Pipfile: /Users/juyeong/Desktop/django_workspace/Pipfile
Using /usr/local/bin/python3 (3.7.3) to create virtualenv…
⠸ Creating virtual environment...Already using interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
Running virtualenv with interpreter /usr/local/bin/python3

✘ Failed creating virtual environment
[pipenv.exceptions.VirtualenvCreationException]:   File "/usr/local/lib/python3.7/site-packages/pipenv/cli/command.py", line 390, in shell
[pipenv.exceptions.VirtualenvCreationException]:       pypi_mirror=state.pypi_mirror,
[pipenv.exceptions.VirtualenvCreationException]:   File "/usr/local/lib/python3.7/site-packages/pipenv/core.py", line 2156, in do_shell
[pipenv.exceptions.VirtualenvCreationException]:       three=three, python=python, validate=False, pypi_mirror=pypi_mirror,
[pipenv.exceptions.VirtualenvCreationException]:   File "/usr/local/lib/python3.7/site-packages/pipenv/core.py", line 574, in ensure_project
[pipenv.exceptions.VirtualenvCreationException]:       pypi_mirror=pypi_mirror,
[pipenv.exceptions.VirtualenvCreationException]:   File "/usr/local/lib/python3.7/site-packages/pipenv/core.py", line 506, in ensure_virtualenv
[pipenv.exceptions.VirtualenvCreationException]:       python=python, site_packages=site_packages, pypi_mirror=pypi_mirror
[pipenv.exceptions.VirtualenvCreationException]:   File "/usr/local/lib/python3.7/site-packages/pipenv/core.py", line 935, in do_create_virtualenv
[pipenv.exceptions.VirtualenvCreationException]:       extra=[crayons.blue("{0}".format(c.err)),]
[pipenv.exceptions.VirtualenvCreationException]: Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 417, in copyfile
    os.symlink(os.path.realpath(src), dest)
FileExistsError: [Errno 17] File exists: '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin' -> '/Users/juyeong/.local/share/virtualenvs/django_workspace-CfQ2tbWB/lib/python3.7/config-3.7m-darwin'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 2611, in <module>
    main()
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 862, in main
    symlink=options.symlink,
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 1137, in create_environment
    install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 1421, in install_python
    copy_required_files(stdlib_dir, lib_dir, symlink)
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 1331, in copy_required_files
    copyfile(join(src_dir, fn), join(lib_dir, fn), symlink)
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 420, in copyfile
    copy_file_or_folder(src, dest, symlink)
  File "/usr/local/lib/python3.7/site-packages/virtualenv.py", line 398, in copy_file_or_folder
    shutil.copytree(src, dest, symlink)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 321, in copytree
    os.makedirs(dst)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 221, in makedirs
    mkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/Users/juyeong/.local/share/virtualenvs/django_workspace-CfQ2tbWB/lib/python3.7/config-3.7m-darwin'

Failed to create virtual environment.

当我在macOS pipenv shell中使用Mojave时,一切都还好,现在我正在使用Catalina version 10.15.1,但根本无法正常工作。我曾尝试升级pip,删除并重新安装pipenv,但同样的事情也会发生。 :'(

有人可以帮我解决这个问题吗?

P.S。我想使用pipenv shell而不是python -m venv

的虚拟环境

3 个答案:

答案 0 :(得分:0)

pipenv似乎崩溃了,因为它试图创建一个已经存在的文件夹。尝试使用

清除虚拟环境
rm -rf `pipenv --venv`

如果由于pipenv抱怨没有关联的virtualenv而失败,则您必须跳过快捷方式,查看错误消息中不喜欢的文件夹,然后将其删除(确保删除了一个/virtualenvs的子文件夹,而不是重要的东西,例如pipenv bin)

例如,在这里您将执行以下操作:

rm -rf /Users/juyeong/.local/share/virtualenvs/django_workspace-CfQ2tbWB/

答案 1 :(得分:0)

> pipenv --python 3.8                                                   
> ─╯ Creating a virtualenv for this project... Pipfile:
> /Users/nathan.benton/testing/python/lambda_cs/Pipfile Using
> /Users/nathan.benton/.pyenv/versions/3.8.6/bin/python3.8 (None) to
> create virtualenv... ⠹ Creating virtual environment...RuntimeError:
> failed to query
> /Users/nathan.benton/.pyenv/versions/3.8.6/bin/python3.8 with code 8
> err: 'Exec format error'
> 
> ✘ Failed creating virtual environment
> 
> [pipenv.exceptions.VirtualenvCreationException]: Failed to create
> virtual environment.

使用@arshbot 提供的解决方案,我做到了:

rm -rf /Users/nathan.benton/.pyenv/versions/3.8.6/bin/python3.8

答案 2 :(得分:0)

删除virtualenv,帮助我解决了这个问题。我认为存在一些内部冲突。

pip uninstall virtualenv