pyenv + pipenv (on Mac)

Overview

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

pyenv is the version manager of Python(≒ nvm).

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)

1. Install pyenv

brew <span>install </span>pyenv
brew <span>install </span>pyenv
brew install pyenv

Enter fullscreen mode Exit fullscreen mode

pyenv | github.com

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…

<span># Add .pyenv/bin to $PATH to be able to run pyenv command anywhere</span>
<span>export </span><span>PATH</span><span>=</span><span>"</span><span>$HOME</span><span>/.pyenv/bin:</span><span>$PATH</span><span>"</span>
<span># Add .pyenv/shims to $PATH to switch python versions with pyenv command</span>
<span>eval</span> <span>"</span><span>$(</span>pyenv init -<span>)</span><span>"</span>
<span># Add .pyenv/bin to $PATH to be able to run pyenv command anywhere</span>
<span>export </span><span>PATH</span><span>=</span><span>"</span><span>$HOME</span><span>/.pyenv/bin:</span><span>$PATH</span><span>"</span>

<span># Add .pyenv/shims to $PATH to switch python versions with pyenv command</span>
<span>eval</span> <span>"</span><span>$(</span>pyenv init -<span>)</span><span>"</span>
# 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 -)"

Enter fullscreen mode Exit fullscreen mode

How to manage your python virtualenvs with Pipenv
Pipenv Installation
Where does pipenv install a package (ja)

3. Install a python version

<span># Show the list of version available for installation</span>
pyenv <span>install</span> <span>-l</span>
<span># Install the version</span>
pyenv <span>install</span> <version> <span># ex: pyenv install 3.12.0</span>
<span># Show the list of installed version</span>
pyenv versions
<span># Show the list of version available for installation</span>
pyenv <span>install</span> <span>-l</span>

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

<span># Show the list of installed version</span>
pyenv versions
# 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

Enter fullscreen mode Exit fullscreen mode

4. Set the python version in the local directory

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

<span># Set the version in local</span>
pyenv <span>local</span> <version>
<span># Show the current python version</span>
pyenv version
<span># Check whether python command is activated</span>
python <span>-V</span>
<span># Set the version in local</span>
pyenv <span>local</span> <version>

<span># Show the current python version</span>
pyenv version

<span># Check whether python command is activated</span>
python <span>-V</span>
# Set the version in local pyenv local <version> # Show the current python version pyenv version # Check whether python command is activated python -V

Enter fullscreen mode Exit fullscreen mode

5. Install pipenv

Pipfile (≒ package.json) is created after the installation command.

brew <span>install </span>pipenv
<span># Set the same python version as pyenv</span>
pipenv <span>--python</span> <version>
brew <span>install </span>pipenv

<span># Set the same python version as pyenv</span>
pipenv <span>--python</span> <version>
brew install pipenv # Set the same python version as pyenv pipenv --python <version>

Enter fullscreen mode Exit fullscreen mode

pipenv | github.com

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.

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

<span># Uninstall</span>
pipenv uninstall <PACKAGE_NAME>
# 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>

Enter fullscreen mode Exit fullscreen mode

7. Activate a virtual environment

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

<span># Activate the local directory's environment</span>
<span># You can exit from it by entering `exit` if you want do</span>
pipenv shell
<span># Activate the environment and run a command</span>
pipenv run <COMMAND> <span># pipenv run python main.py</span>
<span># Activate the local directory's environment</span>
<span># You can exit from it by entering `exit` if you want do</span>
pipenv shell

<span># Activate the environment and run a command</span>
pipenv run <COMMAND> <span># pipenv run python main.py</span>
# 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

Enter fullscreen mode Exit fullscreen mode

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

pipenv run <command>


Other commands

<span># Show the path of the virtual environment (directory)</span>
pipenv <span>--venv</span>
<span># Remove the local virtual environment with the whole installed packages</span>
pipenv <span>--rm</span>
<span># Show the path of the virtual environment (directory)</span>
pipenv <span>--venv</span>

<span># Remove the local virtual environment with the whole installed packages</span>
pipenv <span>--rm</span>
# Show the path of the virtual environment (directory) pipenv --venv # Remove the local virtual environment with the whole installed packages pipenv --rm

Enter fullscreen mode Exit fullscreen mode

Command list (ja)

原文链接:pyenv + pipenv (on Mac)

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
Never give up your dreams. Miracles happen everyday.
别放弃梦想,奇迹每天都在上演
评论 抢沙发

请登录后发表评论

    暂无评论内容