Create a virtual environment management environment (?) Using pyenv and pipenv. Version control of Python itself is performed by pyenv, and virtual environment construction and package management for each project are performed by pipenv.
install.sh
#!/bin/bash
# Install dependencies
echo "User password" | sudo apt update && sudo apt install -y --no-install-recommends \
build-essential \
libffi-dev \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
git
# Download pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
# Update .bash_profile
touch ~/.bash_profile
echo -e "# pyenv paths" >> ~/.bash_profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
pyenv -v
# Install Python and set default
pyenv install 3.7.4
pyenv global 3.7.4
# Install pipenv
pip install pipenv
By executing this shell script as follows, pyenv / python / pipenv will be installed.
./install.sh
If you get angry that you don't have permission, use chmod + rx install.sh
to give read / execute permission.
Of course, there is no problem even if you hit the terminal line by line and execute it.
Create a project called hoge and start up a virtual environment of Python 3.6.
First install 3.6 version of Python with pyenv.
$ pyenv install 3.6.9
Next, create a virtual environment with pipenv.
$ mkdir hoge
$ cd hoge
$ pipenv install --python 3.6.9
Creating a virtualenv for this project…
Pipfile: /home/**/hoge/Pipfile
Using /home/**/.pyenv/versions/3.6.9/bin/python3 (3.6.9) to create virtualenv…
Python 3.6.9 installed with pyenv is used.
Success if there are Pipfile
and Pipfile.lock
under hoge /
.
--Enable virtual environment
$ pipenv shell
(hoge) user:~/hoge$
--Install library in virtual environment
$ pipenv install numpy
Use pipenv install
instead of pip install
. If you accidentally pip install
, it won't pollute the outside of the virtual environment, but be aware that the library is not managed by Pipfile / Pipfile.lock
. ..
--Get out of the virtual environment
$ exit
--Delete virtual environment
$ pipenv --rm
--Easy to install & easy to configure
--When you install the library with pipenv, Pipfile / Pipfile.lock
is also updated automatically, so unlike requirements.txt, you will not forget to update it.
――It goes well with git
--You can take over package management from requirements.txt
with pipenv install -r requirements.txt
--It is troublesome to enable / disable the virtual environment. The usability may be similar to virtualenv.
--It looks like you're going to pip install
by mistake.
--It takes time to create Pipfile.lock
By default, virtual environments created with pipenv are placed under ~ / .local / share / virtualenvs /
. pipenv --rm
deletes this virtual environment, but forget about it. If you delete only the project directory, the virtual environment itself will remain.
To create a virtual environment directly under the project, it is necessary to define the environment variable $ PIPENV_VENV_IN_PROJECT
.
echo -e "# pipenv property" >> ~/.bash_profile
echo 'export PIPENV_VENV_IN_PROJECT=1' >> ~/.bash_profile
source ~/.bash_profile
By doing this, the virtual environment .venv /
will be placed directly under the project, and if you delete the project directory, the virtual environment will also be deleted.
--The story of installing pyenv on ubuntu 18.04 --Qiita --Use virtual environment anywhere with pipenv --Qiita
Recommended Posts