I am doing Python version control, library management, and virtual environment management with pyenv + pipenv on a mac, but one day when I tried to activate the virtual environment of pipenv, I got the following error and could not activate it. ..
$ pipenv shell
Traceback (most recent call last):
File "/usr/local/Cellar/pipenv/2018.11.26_4/libexec/bin/pipenv", line 5, in <module>
from pipenv import cli
ModuleNotFoundError: No module named 'pipenv'
The error is that pipenv cannot be found, but when I go to the directory where pipenv itself is stored, I do not know the cause of this error because it exists normally there. I got this error when I tried to activate the virtual environment in any other directory and realized that pipenv was broken. If you know the cause, it's been a while since you restarted your mac. It's unconfirmed, but maybe it happened after rebooting with some things left in the virtual environment. By the way, a few days ago, my junior also fell into this symptom, and while being supported by a university professor without knowing the cause, I solved it by reinstalling pyenv and pipenv, so I tried it myself and it was cured. I will keep a series of work as a memorandum.
First, delete the existing pyenv and pipenv. Here, it is not necessary to delete the Pipfile etc. that manages the library. Leave it ready for immediate rebuilding based on the information contained in the Pipfile. Since this article focuses on rebuilding the virtual environment of pyenv + pipenv, it is assumed that you are using these environments originally and you do not need to change the settings of Pipfile, Pifile.lock, .bash_profile and zshrc. write.
--Check the path of Python you are using --Delete the directory related to pyenv --Remove pyenv --Remove pipenv
$ which python
>>> /Users/xxx/.pyenv/shims/python
$ rm -rf /Users/xxx/.pyenv
$ brew uninstall pyenv
$ brew uninstall pipenv
Now it's time to reinstall the tools.
$ brew install pyenv
$ brew install pipenv
--Install Python by specifying the version you were originally using --Set to use 3.7.5 for the entire mac --Check the Python version (Is it over if it is reflected?)
$ pyenv install 3.6.7
$ pyenv install 3.7.5
$ pyenv global 3.7.5
$ python -V
>>> Python 2.7.10
Since it was not reflected, when I checked which Python I was using, it seems that I am using the mac default Python.
$ which python
>>> /usr/bin/python
Speaking of which, in order to be able to use Python of pyenv with a path specification such as / Users / xxx / .pyenv / shims / python
, write the following in `` `.bash_profile``` I remembered what I did. The zsh user is another file.
export PYENV_ROOT=${HOME}/.pyenv
if [ -d "${PYENV_ROOT}" ]; then
export PATH=${PYENV_ROOT}/bin:$PATH
eval "$(pyenv init -)"
fi
Reflect the above settings again and check which Python you are using again.
$ source .bash_profile
$ which python
>>> /Users/xxx/.pyenv/shims/python
It was confirmed that it supports Python of pyenv normally.
From here, it is about rebuilding the virtual environment managed by pipenv.
--Move to the directory you originally used and want to rebuild the virtual environment --Create a pipenv environment by specifying the Python version --Activate virtual environment --Batch installation of libraries based on existing Pipfile
cd The directory where you want to create a virtual environment
pipenv --python 3.7.5
pipenv shell
pipenv install
This completes the rebuilding of the virtual environment.
I searched for the error described at the beginning, including English literature, but I could not find a person with the same symptom, and the cause is still unknown, but there is symptomatic treatment, so do not panic and deal with it calmly.
Reinstalled pipenv and pyenv Erase pyenv properly and then put it back in
Recommended Posts