pyenv + pipenv (on Mac)
Tags: python
Published: 2023-10-18 17:06:04

# Overview
You can create a virtual environment of Python like nvm by installing **pyenv** and **pipenv**.

**pyenv** is the version manager of Python(≒ <u>nvm</u>).

**pipenv** is the library manager of Python.
This manages installed libraries for each directories(projects) because the libraries installed via pipenv are installed in `~/.virtualenvs/<YOUR_DIRECTORY_NAME>-<UUID>`.
`<YOUR_DIRECTORY_NAME>-<UUID>` directory is automatically generated when pipenv installs a library in the directory.

---

# Getting started

[Steps (ja)](https://www.wakuwakubank.com/posts/255-python-pipenv-pyenv/)

## 1. Install pyenv

```shell
brew install pyenv
```

[pyenv | github.com](https://github.com/pyenv/pyenv)

## 2. Add the paths to $PATH to be able to activate pyenv command and manage python versions with it


You need to add the following lines to .zprofile or .zshrc etc...

```shell
# Add .pyenv/bin to $PATH to be able to run pyenv command anywhere
export PATH="$HOME/.pyenv/bin:$PATH"

# Add .pyenv/shims to $PATH to switch python versions with pyenv command
eval "$(pyenv init -)"
```

[How to manage your python virtualenvs with Pipenv](https://medium.com/test-automation-university/how-to-manage-your-python-virtualenvs-with-pipenv-f1220ded702e)
[Pipenv Installation](https://pipenv.pypa.io/en/latest/installation/)
[Where does pipenv install a package (ja)](https://qiita.com/musatarosu/items/394603957ae41552718e#%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%97%E3%81%9F%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AF%E3%81%A9%E3%81%93%E3%81%B8)

## 3. Install a python version

```shell
# Show the list of version available for installation
pyenv install -l

# Install the version
pyenv install <version> # ex: pyenv install 3.12.0

# Show the list of installed version
pyenv versions
```

## 4. Set the python version in the local directory

The python version is managed by `.python-version` file created after the command.

```shell
# Set the version in local
pyenv local <version>

# Show the current python version
pyenv version

# Check whether python command is activated
python -V
```

## 5. Install pipenv

**Pipfile** (≒ <u>package.json</u>) is created after the installation command.

```shell
brew install pipenv

# Set the same python version as pyenv
pipenv --python <version>
```

[pipenv | github.com](https://github.com/pypa/pipenv)

## 6. Install a package and create a virtual environment

When a package is installed for the first time in your current directory, `~/.virtualenvs/<YOUR_DIRECTORY_NAME>-<UUID>` directory is created and then the package is installed there.

```shell
# Add the package information to **Pipfile** and **Pipfile.lock** (be created if it doesn't exist)
pipenv install <PACKAGE_NAME>

# Uninstall
pipenv uninstall <PACKAGE_NAME>
```

## 7. Activate a virtual environment

You need to enter the virtual environment created above to use the installed packages.

```shell
# Activate the local directory's environment
# You can exit from it by entering `exit` if you want do
pipenv shell

# Activate the environment and run a command
pipenv run <COMMAND> # pipenv run python main.py
```

`pipenv run <COMMAND>` means to run `pipenv shell` and then run `<COMMAND>`.

[pipenv run \<command\>](https://rinatz.github.io/python-book/ch04-05-pipenv/)

---

## Other commands

```shell
# Show the path of the virtual environment (directory)
pipenv --venv

# Remove the local virtual environment with the whole installed packages
pipenv --rm
```

[Command list (ja)](https://qiita.com/RW_876/items/ce42026884f1383e21da)