Search code examples
pythondjangomacospipenv

pipenv shell fails to create virtual environment


I am trying to run a Django project using pipenv shell. But when I enter the command pipenv shell, it fails.

❯ 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.

Everything was okay when I was using pipenv shell in macOS Mojave, now I'm using Catalina version 10.15.1 and it's not working at all. I've tried upgrading pip, removing and reinstalling pipenv but same thing happens. :'(

Can anyone help me solve this problem?

P.S. I want to use virtual environment with pipenv shell, not with python -m venv


Solution

  • It appears pipenv is crashing because it attempts to create a folder that already exists. Try clearing the virtual env with

    rm -rf `pipenv --venv`
    

    If that fails because pipenv complains there isn't an associated virtualenv, you'll have to skip the shortcuts and look at the error message for what folder it doesn't like and delete it ( make sure you're deleting a subfolder of /virtualenvs and not something important like the pipenv bin )

    For example here you'd do the following:

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