Python environment construction (pyenv + poetry + pipx)

What's this? Python environment for macbook

Since it was built in, it is a memo. The environment is ** mac OS 10.14.6 Mojave **. We will proceed on the assumption that homebrew is already included. (If you do not have homebrew, please refer to Homebrew Official and add it) I think I will update it from time to time.

Install Python3

Install system python3 with homebrew. If you already have it, please skip it. Also, please skip people who say, "I'll put pyenv later. I'll put it in pyenv." Installing python3 on the system is my hobby. (~~ Also, I don't know what it is, but when I try to link numpy and scipy with mkl with python on pyenv, it fails ... ~~ For more information here)

$ brew install python

Install pyenv

Enter pyenv according to Official. I think brew install pyenv is easy, but I don't really like PYENV_ROOT = / usr / local / var / pyenv (it's a complete hobby. I also want to make it as similar as possible to a Linux environment. ), So enter it with git (brew install pyenv is no problem at all).

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

After installing with, edit .bash_profile to run & pyenv init through PATH.

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Restart the shell and you will be able to use pyenv.

If you don't have python3 installed on your system, do pyenv install 3.7.6, pyenv global 3.7.6 here.

If pyenv install fails, installing the latest commandlinetools from https://developer.apple.com/download/more/?=command%20line%20tools may work.

installation of poetry

Follow Official.

$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

Also my hobby is to make .venv in the project directory

$ poetry config virtualenvs.in-project true

To run. The settings around here

$ poetry config --list

You can see it at.

pipx installation

If you roughly explain to people "What is pipx in the first place?" ...

There is an application (command) to be installed with pip (such as sphinx-quickstart). I use that kind of thing in almost any virtual environment. Burns It's a hassle to install in all virtual environments. Therefore, the one that makes it possible to use such applications (commands) that can be entered with pip globally.

is. The official is here.

Ignore those people and move on.

For those who installed python with homebrew

$ brew install pipx
$ pipx ensurepath

For those who make python global in pyenv

$ python3 -m pip install --user pipx
$ python3 -m pipx ensurepath

Add the following to .bash_profile to enable tab completion.

eval "$(register-python-argcomplete pipx)"

This is the end. Thank you for your hard work.

Creating an environment with poetry

There are two ways to create an environment with poetry, poetry new and poetry init.

my-package
├── pyproject.toml
├── README.rst
├── my_package
│   └── __init__.py
└── tests
    ├── __init__.py
    └── test_my_package.py

If you want to create a package, you should recognize it as poetry new <my-package>, otherwise you should recognize it as poetry init.

Probably the latter is often the case, so here we will use poetry init to create the environment.

First, install python used in the project with pyenv. Let's use 3.7.4 here.

$ pyenv install 3.7.4

Create a directory for your project.

$ mkdir /foo/bar/project

After specifying the version of python to use with pyenv local, poetry init.

$ cd /foo/bar/project
$ pyenv local 3.7.4
$ poetry init
(If you answer what you are asked, pyproject.toml is generated)
(Ignore the question and later pyproject.You may edit toml)
(First of all, pyproject by yourself.You may make toml)

You can install the package as written in pyproject.toml with poetry install.

$ poetry install
(This is pyproject.The package written in toml is installed)
(at this point`.venv`Is also made)

You can also add a package with poetry add <package>.

For more information on how to use poetry, see Official documentation.

The virtual environment (.venv) is created automatically when you first use poetry add or poetry install, but before that, create a virtual environment such as python -m venv .venv. Is also good. You might think that's necessary, but it seems like this is the only way to do it when you need pip.conf (I don't know of any other way so far). pip.conf should be under .venv (it will be .venv / pip.conf). By the way, in my case, pip.conf ($ HOME / .pip / pip.conf) of system python3

[install]
no-binary = numpy,scipy

[wheel]
no-binary = numpy,scipy

If you do nothing, you will be affected by this. So, when using numpy, scipy, I purposely create .venv / pip.conf with the following contents.

[install]
no-binary = 

[wheel]
no-binary = 

If you do not devise in relation to mkl, the build of numpy, scipy will fail, so I will do this (~~ I will write about this later ~~ I made an article → numpy, scipy under pyenv + poetry environment to mkl I want to use it).

(Example of using pipx) Install sphinx with pipx

For pipx, I think it's better to see the usage example, so I will introduce the procedure to install sphinx using pipx. As mentioned above, it is troublesome (or never done) to put sphinx in all virtual environments, so pipx comes into play.

$ pipx install sphinx

If you want to use sphinx_rtd_theme or sphinx contrib-blockdiag,

$ pipx inject sphinx sphinx_rtd_theme
$ pipx inject sphinx sphinxcontrib-blockdiag

You can do it.

Finally

I used to use pipenv, but poetry should install the package faster than pipenv. However, I don't make many packages, so when asked if it needs to be poetry, it's not so. If there is something else that looks good, I may switch again (I've been repeating this all the time ... Is there a goal?)

reference

Recommended Posts

Python environment construction (pyenv + poetry + pipx)
Python environment construction
Environment construction (python)
Python environment construction (pyenv, anaconda, tensorflow)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
Get a clean Python development environment with pyenv + pipx + Poetry
Windows + gVim + Poetry python development environment construction
homebrew python environment construction
Python development environment construction
pyenv + fish environment construction
python2.7 development environment construction
Mac environment construction Python
Python environment construction @ Win7
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
Python environment construction For Mac
Anaconda3 python environment construction procedure
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
Python environment construction under Windows7 environment
[MEMO] [Development environment construction] Python
Environment construction of python2 & 3 (OSX)
Python development environment construction 2020 [From Python installation to poetry introduction]
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
Environment construction of python and opencv
Get started with Python! ~ ① Environment construction ~
Anaconda python environment construction on Windows 10
Python + Unity Reinforcement learning environment construction
I checked Mac Python environment construction
Python environment construction memo on Mac
[Python3] Development environment construction << Windows edition >>
Environment construction memo of pyenv + conda
Python development environment construction on macOS
Environment construction of python3.8 on mac
Smooth Python development environment for teams [Poetry + pyenv + black + isort]
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Python3 TensorFlow for Mac environment construction
Emacs Python development environment construction memo
pytorch @ python3.8 environment construction with pipenv
[docker] python3.5 + numpy + matplotlib environment construction
Python3.6 environment construction (using Win environment Anaconda)
OpenCV3 & Python3 environment construction on Ubuntu
Environment construction with pyenv and pyenv-virtualenv
Construction of Python local development environment Part 1 (pyenv, pyenv-virtualenv, pip installation)
[Pyenv] Building a python environment with ubuntu 16.04
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Use smbus with python3 under pyenv environment
Python project environment construction procedure (for windows)
Python3 + venv + VSCode + macOS development environment construction
VScode environment construction (Windows10, Python, C ++, C, Git)
Environment construction procedure: Ubuntu + Apache2 + Python + Pyramid
Python and machine learning environment construction (macOS)
Build a python virtual environment with pyenv
Let's get along with Python # 0 (Environment construction)
Django environment construction